how do you do a nested loop?

im doing a code that has to include a nested loop...,how would i do that..

example.

a bowling team consists of five players.Each player bowls three games. use a nested loop to enter each player's individual scores and then computes and displays the average score for each bowler.

p.s this isnt my homework this was just a question i found and i wanted to see how it would work
closed account (Lv0f92yv)
A nested 'for' loop, for example, is just a for loop inside the body of another for loop. IE:

1
2
3
4
5
6
7
8
9
10
...
for ( int i = 0; i < 5; i++ )
{
   for ( int j = 0; j < 3; j++ )
   {
       //inner body
       cout << i << " " << j << "\n"; //print values of both variables
   }
}
...
Last edited on
And a bit more specific for the 'assignment' :P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int playerScore = new int[totalPlayers];
int playerGames = new int[totalPlayers];

for (int player = 1; player <= totalPlayers; player++)
{
    int tempScore;
    for (int game = 1; game <= 3; game++)
    {
        cout << "What score did player " << player << " score in game " << game << "?:\t";
        cin >> tempScore;
        playerScore[player - 1] += tempScore;
        playerGames[player - 1]++;
        cout << "At this point, player " << player << has a total of << playerScore[player - 1] << " and an average score of " << ( playerScore[player - 1] / playerGames[player - 1]) << ".\n";
    }
}

for (int player = 1; player <= totalPlayers; player++)
wouldnt this be repeated x 5?
closed account (Lv0f92yv)
Depends on what totalPlayers is defined as.

for ( int i = 0; i < 6; i++ )

executes 6 times, including the 0th.
well if i am doin it for like 5 or 6 people....and then do my total players no?
i got it
:)
Topic archived. No new replies allowed.