Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

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
Asked:
mm162
1 Solution
 
jkrCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now