0

I'm trying to display the position(index) numbers of largest values in an array. It's an array with 100 random numbers. For some reason the output is correct after the first a few numbers. I always get wrong numbers in the beginning of the output. This is the code I have; Thanks

public static void main(String[] args) {
    int max = 0;
    int array[] = new int[100];
    System.out.println();

    // code to display student responses
    randomNum(array);

    System.out.println(" Survey Responses - 100 Students: ");
    response(array);

    System.out.println("Positions of the highest numbers");
    System.out.println();

    for (int i = 0; i < array.length; i++) {
        max = Math.max(max, array[i]);
        if (array[i] == max) {
            System.out.println(i);
        }
    }
}

private static void randomNum(int A[]) {
    for (int i = 0; i < A.length; i++) {
        A[i] = (int) (Math.random() * 10000) % 10 + 1;
    }

}

public static void response(int[] resp) {
    for (int x = 0; x < resp.length; x++) {
        System.out.print(resp[x] + " ");
    }
    System.out.println();
}
1
  • Can you please clarify what you mean by "the positions of the largest numbers in the array"? Do you want to print a fixed number of indexes? Your current code prints the indexes of the "largest number seen so far" as it makes a left-to-right pass over the array. Commented Feb 12, 2012 at 17:02

3 Answers 3

1

Run two cycles, like this:

for (int i = 0; i < array.length; i++) {
    max = Math.max(max, array[i]);
}
for (int i = 0; i < array.length; i++) {
    if (array[i] == max) {
        System.out.println(i);
    }
}

So far, you output indices of the maximum number seen in the prefix rather than indices of values equal to total maximum.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use this snippet in a routine instead, it would be more convenient. Take a look, I modified your code.
0

Wouldn't it be easier for you to sort the array (just for clarification)? Then you have the min element at index 0 and the max at arr.length-1, something like this:

java.util.Arrays.sort(arr);
final int min = (arr.length > 0) ? arr[0] : 0;
final int max = (arr.length > 0) ? arr[arr.length-1] : 0;

Comments

0

Your algorithm is wrong. When you print the values out, you're looping through the array and then comparing the current value of 'max' to the current element of the array. So if you had an array of only 10 numbers (for simplicity) which was

1,2,3,4,5,6,7,8,9,10

In the first trip through the loop you'd compare the current value of 'max'(0) to the first element of the array (1) and print 1 and set max to 1. The second time through you'd the current value of max (1) to the second element of the array (2) and print 2 and set max to 2. The same happens for each iteration through the loop and you'd end up printing out every value.

In the opposite case of

10,9,8,7,6,5,4,3,2,1

which contains the same numbers in a different order, you'd print out 10 only, because you set max to 10 on the very first time through the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.