Cannot get an array's index to output correctly

Mar 30, 2016 at 10:49pm
Title.

Every time I try to get the largest number in an array's index to display, it just puts out 0. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;



int main()
{


   int fmax[10] = {0};
   int counter;
   int max = 0;
   
   for (counter = 0; counter < 9; counter++)
   {
     cout << "\nEnter a number: ";
     cin >> fmax[counter];


     if (fmax[counter] > max)
     {
    

     max = fmax[counter];
     }

   }



   cout << "The maximum value is: " << max << endl;
      cout << "This is element number " << fmax[counter] << " in the list of numbers" << endl;


return 0;
}
Mar 30, 2016 at 10:52pm
cout << "This is element number " << fmax[counter] << " in the list of numbers" << endl;

counter = 9, so you are outputting fmax[9]. fmax[9] is zero, so you output zero.
Mar 30, 2016 at 11:47pm
I cannot, for the life of me, figure out how to display the index of the largest number entered here.
Mar 31, 2016 at 9:01am
You're not storing that information anywhere. On line 27, you need to store the value of the index. Then, on line 37, you can output the value of the index.
Topic archived. No new replies allowed.