Finding largest value in an array
int nElements = 5;
int array[] = new int[nElements];
for (int i=0; i<nElements; i++){
array[i] = (int)random(0,100);
}
println(array);
int indexOfLargest = -1;
int largestVal = -1;
for (int i=0; i< array.length; i++){
if (array[i] > largestVal){
largestVal = array[i];
indexOfLargest = i;
}
}
println("Index " + indexOfLargest + " contains " + largestVal); |
This entry was posted
on Monday, October 27th, 2008 at 06:17 and is filed under Uncategorized.
You can follow any responses to this entry through the RSS 2.0 feed.
Both comments and pings are currently closed.