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 & HelpSyntax Questions › get array max value
Page Index Toggle Pages: 1
get array max value (Read 893 times)
get array max value
Nov 30th, 2005, 5:55pm
 
sorry for my english and question about processing, but i am, in both, a beginner.

how i can get the max value and his position from an array?

thanxs
Re: get array max value
Reply #1 - Nov 30th, 2005, 8:08pm
 
If you know that the values in the array are all greater than, 0 (or some other known value) then you can easily use the following:

Code:

float maxValue=0; //or int, if it's an array of ints
int position=-1;
for(int i=0;i<myArray.length;i++) //check all values in array
{
if(myArray[i]>maxValue) // check to see if current value is highest so far
{
position=i; // if so, store what number it is
maxValue=myArray[i]; // and what its value is
}
}
if(position==-1)
{
println("Oops, all values are less than 0");
}
else
{
println("Max value:"+maxValue+", at number:"+position);
}
Re: get array max value
Reply #2 - Nov 30th, 2005, 9:03pm
 
or even a little tweak:

Code:
if (array.length == 0) {
println("nothing in the array.");

} else {
float maxValue = array[0];
int maxIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > maxValue) {
maxValue = array[i];
maxIndex = i;
}
}
print("max value is " + maxValue);
println(" at position " + maxIndex);
}
Re: get array max value
Reply #3 - Dec 1st, 2005, 4:06pm
 
thank you very much.
it takes a little time to understand it, but it works well.

it serves in order to make me to move one ball with the sound of one voice, with the LiveInput.getSpectrum array of sonia.

Page Index Toggle Pages: 1