• Status: Solved
• Priority: Medium
• Security: Public
• Views: 238

# Finding the element of an array with the highest value

In other words, suppose you have declared a variable:
float Array[10]
And suppose the elements of the array have been assigned some random numbers.
I am looking for code that can identify which element of the array has the highest value.  That is, in math notation, I need to get a number j between 0 and 9 such that Array[j] is the highest value assumed by any element in the array.
0
mm162
1 Solution

Commented:
float Array[MAX_ARRAY];

int nHighestIdx = 0;
float fHighestVal = 0.0;

//...

for ( int i = 0 ; i < MAX_ARRAY; i++)
{
if ( Array [ i] > fHighestVal)
{
fHighestVal = Array [ i];
nHighestIdx = i;
}
}

When the loop terminates, 'nHighestIdx' contains the array index of the highest value in the array...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.