rob211

Hi @all!
I have a array with three numbers:
num[0] = 5
num[1] = 2
num[2] = 5
I need to find out which of these numbers are the largest ones, it could be one, two or all of them, and need the index. Here for example "0 and 2". What's the best (most compact) way for that?
Thanks a lot.
rob

NormR1 563

Use a for loop to look at the numbers one at a time and save the largest ones.
If you are saving the indexes and there can be more than one, you will need another array to save the indexes in.

kolibrizas 12

``````int max = num[0];
for ( i=1; i<=2; i++ )
{
if(num[i] > max)
{
max = num[i];
}
}

for ( i=0; i<=2; i++ )
{
if(num[i] == max)
{
System.out.println(i+": "+num[i]);
}
}``````

JamesCherrill 3,661

Hi kolibrizas
Your help, advice, and assistance are very welcome here. But please don't just post solutions to people's homework for them to copy & paste. All they learn from that is how to cheat. Give them guidance that allows them to learn how to do it for themselves. Thanks.

kolibrizas 12

Hi kolibrizas
Your help, advice, and assistance are very welcome here. But please don't just post solutions to people's homework for them to copy & paste. All they learn from that is how to cheat. Give them guidance that allows them to learn how to do it for themselves. Thanks.

Sorry about that. Didn't think of that. Should I edit/remove my post? :)

JamesCherrill 3,661

Maybe an edit to explain why/how that code works - but don't worry, there's no harm done this time. :)

kolibrizas 12

For some reason I cannot see Edit button at my post :( Guess I will leave it like that. Thanks for a lesson, will try to do better from now :)

P.S. For this post I do see an edit button o.O

JamesCherrill 3,661

Yeah, the edit button is only available for (I think) 30 mins after the original post. Anyway, don't worry, it's OK.

rob211

Thank you very much, it was very helpful for me, and it is no homework, it was just for fun...