L
L
LittleMontrose2014-10-26 13:30:32
Java
LittleMontrose, 2014-10-26 13:30:32

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

3 answer(s)
I
IgorBond, 2014-10-26
@IgorBond

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");
    }
}

B
Barnie Savington, 2014-10-26
@iwex

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)

V
Vasily, 2014-10-26
@Applez

Use the appropriate container types. For example treeset, it is sorted. Reload Comparator if needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question