FOR LOOP HELP????

closed account (z6f9LyTq)
The assignment is calculating the bowler's average. But I can't seem to figure out how to calculate the average because I don't know how to add the individual bowling scores yet. This is my code so far:

#include <iostream.h>
#include <math.h>
#include <iomanip.h>

int main ()

{

int games;
int score;

cout << "Enter the number of games played:" << endl;
cin >> games;



{cout << " " << endl;
for (int x = 1; x<=games; x = x + 1)
{
cout << "What was the score of the game?" << endl;
cin >> score;
}
}

cout << " " << endl;

cout << setw(10) << "Total Points" << setw(20) << "Total Games" << setw(30) << "Average" << endl;
cout << "______________________________________________________________________" << endl;
cout << setw(10) << score << setw(20) << games << setw(30) << games << endl;


return 0;

Any help would be kind:)
closed account (j3Rz8vqX)
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
int main ()

{

    int games;
    int score;
    int total=0;//?

    cout << "Enter the number of games played:" << endl;
    cin >> games;



//{---comment: ?
    cout << " " << endl; 
    for (int x = 1; x<=games; x = x + 1)
    {
        cout << "What was the score of the game?" << endl;
        cin >> score;
        total=total+score;//score must be valid?
    }
//}---comment: ?

    cout << " " << endl;

    cout << setw(10) << "Total Points" << setw(20) << "Total Games" << setw(30) << "Average" << endl;
    cout << "______________________________________________________________________" << endl;
    cout << setw(10) << total<< setw(20) << games << setw(30) << games << endl;


    return 0;
}


Your code with code brackets <>; including a few changes?

Not sure why they were inserted there, unless you were attempting to narrow scope.

Average = total/number of games?

I've already done too much with the implementation of total...
Last edited on
closed account (z6f9LyTq)
Thank you so much! But in like "total+=grade", is there another way to write that (simpler) without the whole += thing? But thank you anyway!
closed account (j3Rz8vqX)
Simpler? I believe not.

We are literally assigning the current total plus the current score to itself.

You can abstract(complicate its look) it to be total+=score;, literally being the same thing.

What was done was the implementation of an accumulator, for total score.

Now if you divide it by the number of games, you'll have the average.

Have a good day.
Topic archived. No new replies allowed.