Answer the question
In order to leave comments, you need to log in
In an array of real numbers, find the minimum element among the negative elements?
Write a program to solve this problem in Java
Answer the question
In order to leave comments, you need to log in
public class minFinder {
public static void main(String[] args) {
float[] array = {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,10,9,8,7,6,5,4,3,2,1};
float minVal = array[0];
for (int i = 1; i < array.length; i++) {
if(array[i] < minVal & array[i] < 0) minVal = array[i];
}
System.out.println(minVal < 0?"Min in array is " + minVal:"Min in array has not found");
}
}
Assign min = 0, and then by a simple pass through the array, check the negative condition on the array elements and compare with the current smallest (if less, reassign min)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question