Array and function

So the function has to sum up and average the values entered in the array but its just displaying the value of the last entered value and I can't seem to figure out why. I'm not sure if its something with my array or the function.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  int main()
{
    //use decimal output, to two decimal places and show decimal
    cout << fixed << setprecision(1) << showpoint;
    
    int numGames;
    double avgInnings = 0.0;
    
    cout << "Please enter the number of games: ";
    cin >> numGames;
    
    while (numGames < 0)
    {
        cout << "The number of games must be greater than 0, please re-enter: ";
        cin >> numGames;
    }
    
    double *innings = nullptr;
    innings = new double [numGames];
    
    if (innings == 0)
    {
        cout << "ERROR - no memory available.";
        exit(1);
    }
    
    for (int i = 0; i < numGames; i++)
    {
        
            cout << "Enter the number of innings pitched for game " << i + 1 << " : ";
            do
        {
            cin >> innings[0];
        
            if (innings[0] < 0)
            {
                cout << "The number entered must be greater than 0, please re-enter: ";
            }
            else if (innings[0] > 5)
            {
                cout << "The maximum innings allowed in one game is 5, please re-enter: ";
            }
        
        }
        while (innings[0] < 0 || innings[0] > 5);

    }

    avgInnings = average(innings, numGames);
}

//Function to calc avg
double average (double* innings, int numGames)
{
    double average = 0.0;
    double sum = 0.0;
    
    for (int i = 0; i < numGames; i++)
    {
        sum += *innings;
    }

    cout << "\nThe grandtotal sum of innings is: " << sum << endl;
    average = sum / numGames;
    
    return average;
}
Hello sadshordie,

Please include all the code that can compile and run the program. It makes it easier to use the gear icon on the top right of the code box.

The problem in in the function "average". sum += *innings; is the same as sum += innings[0]; you are always adding the same value because you are de-referencing the address pointed to by "innings" which is element (0) zero.

I also had problems getting the program to compile an run here. You should compile and check the program to make sure it works before you post it. Add any error messages to your question that you do not understand.

The line avgInnings = average(innings, numGames); looks good, but "avgInnings" is never used for anything.

Hope that helps,

Andy
Hello sadshordie,

When I gor the program to compile and run I found that you defined the function after "main", which is fine, but did not put a prototype before "main", so when line 49 was reached the compiler did not know what "average" is. Also you should use a better than "average" to avoid any possible conflicts. Something like "averageInnings" could work.

When running the program I found in the for loop when you enter the number of innings pinched you are storing all the values entered into element (0) zero of the array. That is not going to work I think you meant cin >> innings[i]; and the same down to and including the while condition.

Lastly you used "new" to create an array. You need to use "delete" to free the memory and not create a memory leak.

Almost forgot avgInnings = average(...);. "avgInnings" receives the returned value, but is never used after that.

Hope that helps,

Andy
Topic archived. No new replies allowed.