We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › find the index of a specific number in an array
Page Index Toggle Pages: 1
find the index of a specific number in an array (Read 1211 times)
find the index of a specific number in an array
Mar 11th, 2009, 12:06pm
 
Hi

my problem is this:
i have two arrays. i want to use max() function to find the highest number in the first array, and then find out the index of this number within the array. so if array[15]=216 or something, how can i find out that 216 is on the 15th place?

or is there a better solution than having two seperate arrays, where each item corresponds with the item in the other array on the same place?
Re: find the index of a specific number in an arra
Reply #1 - Mar 11th, 2009, 1:28pm
 
You can write your own version of the max function that'll give you the number and position:

Code:

int pos=-1;
int max=Integer.MIN_VALUE; //lowest possible value of an int.
for(int i=0;i<myArray.length;i++)
{
if(myArray[i]>max)
{
pos=i;
max=myArray[i];
}
}
Re: find the index of a specific number in an arra
Reply #2 - Mar 11th, 2009, 3:42pm
 
Thank you very much for that hint! that worked perfectly!

Page Index Toggle Pages: 1