
March 6th, 2013, 12:32 AM
|
 |
Daniel Schildsky
|
|
Join Date: Mar 2004
Location: KL, Malaysia.
|
|
|
Alternative without sorting
As an alternative, you may simply declare a variable to store the max value (initialised to 0) and a variable to store its index before the loop starts.
When the loop starts, while going through each element in the array, compare the value stored at each position in the array with the value stored in the variable declared and initialised earlier. If the array element at that position stores the greater value, replace the value of that variable with the value from the element and assign the subscript i to the index variable you declared earlier.
At the end of the loop, you will eventually get the max value assigned to the variable at the same time acquiring the index of the element storing this maximum value.
__________________
When the programming world turns decent, the real world will turn upside down.
|