0

I would like to modify this program so that it also prints out the location of the element in the array that contains the maximum, so in System.out.println("The maximum is " + maximum ); it will be more like -- System.out.println("The maximum is " + maximum + "its location is " + location);

class maximum {

    public static void main(String[] args) {
        double maximum;
        int i;
        double[] a = {1, 2, 3, 4, 5};
        maximum = a[0];
        for (i = 1; i <= 10; i++) {
            if (a[i] > maximum) {
                maximum = a[i];
            }
        }
        System.out.println("The maximum is " + maximum );
    }
}
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by mjv89
0

or, you just set an extra variable within the

if (a[i] > maximum) {
maximum = a[i];
}

like:

int maxLocation = 0;
if (a[i] > maximum) {
maximum = a[i];
maxLocation = i;
}
0

Thanks for the replies!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.