finding the average using an array?

hi i need help finding the average using numbers from an array. this is what i have so far. please help. thank you.

the problem is on line 30 the formula on how to find the average.

it keeps telling me that: no math for 'operator+=' in etc...

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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    cout << fixed;
    string integers[21];
    float nums;
    float result = 0;
    float average = 0;

    cout << "How many numbers do you want? (maz 20)" << endl;
    cin >> nums;

    if (nums > 20)
    {
        cout << "Invalid size. Ending." << endl;
        return 0;
    }

    for (int counter = 0; counter < nums; counter++)
    {
        cout << "Enter value " << counter << ":" << endl;
        cin >> integers[counter];
    }

    for (int i = 0; i < 20; i++)
    {
        result+=integers[i]
        average = result / nums;
        cout << "Average: " << setprecision(1) << average << endl;
    }



    cout << "You entered: " << endl;

    for (int x = 0; x < 20; x++)
    {
        cout << integers[x] << endl;
    }

}
Last edited on
10
11
string int integers[21];
  
Last edited on
i would do this cause you only want to loop your addition.

1
2
3
4
5
6
7
8
for (int i = 0; i < 20; i++)
    {
        result+=integers[i]
    }

        average = result / nums;
        cout << "Average: " << setprecision(1) << average << endl;


or do it in your counter and not do a second for loop.

1
2
3
4
5
6
7
8
9
10
11
for (int counter = 0; counter < nums; counter++)
    {
        cout << "Enter value " << counter << ":" << endl;
        cin >> integers[counter];
        result+=integers[counter];
    }


        average = result / nums;
        cout << "Average: " << setprecision(1) << average << endl;
Last edited on
ok just finished it up thanks a lot
Topic archived. No new replies allowed.