Does anyone know why I'm not getting the right element output? I keep getting a 0 or a 10. When I use fmax[i] I get hexadecimal. Thank you.
int _tmain(int argc, _TCHAR* argv[])
{
const int MAX = 10;
int i, maxNum = 0, fmax[MAX];
for (i = 0; i < MAX; i++)
{
cout << "Enter a number: ";
cin >> fmax[i];
if (fmax[i] > maxNum)
maxNum = fmax[i];
}
cout << endl;
cout << "The maximum value is: " << maxNum << endl;
cout << "This is element number " << i << " in the list of number" << endl;
return 0;
}
fmax[i] is out of bounds since the 10th element is fmax[9] or i - 1.
@Junald
You aren't keeping track of the high number location in the array. Declare another variable after main. Maybe
int num_loc=0;
Then in your checking section
1 2
|
if (fmax[i] > maxNum)
maxNum = fmax[i];
|
, make it like this..
1 2 3 4 5
|
if (fmax[i] > maxNum)
{
maxNum = fmax[i];
num_loc = i;
}
|
Then change the last cout line, to..
cout << "This is element number " << num_loc << " in the list of number" << endl;
Last edited on