# A probably simple problem.

• 11-20-2001
Nate2430
A probably simple problem.
Here it is. I am trying to get an output that shows the highest number in an array and what number this number is in the array. When you run it it shows the correct output forthe second two arrays but it is off on the first one. Even when I switched it to find the lowest it came out correct I don't know why it doesn't work for the first array. If you can determine why it would be greatly appreciated. Here is the code.

#include <iostream>
#include <iomanip>
using namespace std;

int FindIndexHighest(double [],const unsigned int );

int main()
{
double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

cout << fixed << setprecision(2);

int index = FindIndexHighest (x, sizeof (x) / sizeof (x[0]));
cout << "Array x: element index = " << index
<< " element contents = " << x[index] << '\n';
index = FindIndexHighest (y, sizeof (y) / sizeof (y[0]));
cout << "Array y: element index = " << index
<< " element contents = " << y[index] << '\n';
cout << "Array z: element index = "
<< FindIndexHighest (z, sizeof (z) / sizeof (z[0]))
<< " element contents = "
<< z[FindIndexHighest (z, sizeof (z) / sizeof (z[0])) ] << '\n';
return 0;
}
int FindIndexHighest( double num[], const unsigned int SZ )
{

int a = 0;
double highest = num[ 0 ];

for( int i = 1; i < SZ; i++ )

{
if( highest < num[ i ])
a = i;

}

return a;
}

Thanks
Nate2430
• 11-21-2001
gliptic
You must put 'highest = num[i]' in the comparing if-statement.