Member Avatar for mjv89

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

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;
}
Member Avatar for mjv89

Thanks for the replies!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.