0

Here's what I have so far

 double[] num = {1.9, 2.9, 3.4, 3.5};
 double max = num [0];
    for (int i = 1 ; i < num.length ; i++)
    {
        if (num [i] > max)
            max = num [i]; 
    }
    System.out.println ("Max is " + max);

I need to find the index of the greatest value. I've tried printing the index by putting a sentence inside the if statement, and I've also tried by storing i into another variable. None of them worked.

6
  • Why do you have a space between num and [i]? Perhaps that is causing it. Are you getting compilation errors or what? Commented Apr 26, 2016 at 23:28
  • @Gendarme nope, the space isn't causing nay problems. The problem isnt with the array, I just need to find a way to output the index of the greatest value. Commented Apr 26, 2016 at 23:30
  • So, you want the index of the max entry, and you are storing the value of the [i] to the max variable. Perhaps consider setting an` int idx = 0, and then in the loop instead of max = num[i]`, set the idx to to num[i]? Commented Apr 26, 2016 at 23:31
  • The space won't cause any issues but is good practice to not put extra spaces. You need to start at i=0, not i=1 as array indexes start at 0, not 1. Commented Apr 26, 2016 at 23:31
  • 1
    @Gremash There is no need to start at 0. max = num[0]; takes care of that. Commented Apr 26, 2016 at 23:34

2 Answers 2

2

You need to keep track of the max value and its index.

public class GetMaxIndex {
    public static void main(final String[] args) {
        final double[] numbers = { 1.9, 2.9, 3.4, 3.5 };
        double maxNumber = numbers[0];
        int maxIndex = 0;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maxNumber) {
                maxNumber = numbers[i];
                maxIndex = i;
            }
        }
        System.out.println("Max is " + maxNumber);
        System.out.println("Index of max is " + maxIndex);
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

@Gremash It does not.
Oh my god guys, don't u get it? As it already is initialized by the index 0, you can skip that.
Hmm... why do you call the variable maxNumer instead of maxNumber? Some secret sorcery you are not allowed to share with us mortals?
@JSox If Java is your first language, it's a great one to learn on because of the strong typing. Have fun transitioning to Javascript!
@Gendarme No it's because I'm using Eclipse's Refactoring, it renames all of its occurences, so one typo becomes a 'typo' everywhere.
|
0
double[] num = {1.9, 2.9, 3.4, 3.5};
double max = num[0];
int    maxIndex = 0;
for (int i = 1 ; i < num.length ; i++)
{
   if (num[i] > max)
  {
    maxIndex = i
    max = num[i]; 
  }
}
System.out.println("Max is " + max);
System.out.println("Max index is + maxIndex);

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.