[try Beta version]
Not logged in

 
For Loop and Creating Averages

Apr 12, 2009 at 9:50pm
If I want to make program that calculates the averages I have a problem with the averaging part. If I have 5 different people on each team and there are three teams how would I average the score of the three teams as well individually average each player.

Part of my code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(PLAYER=1; PLAYER <=5; PLAYER++)
{
count++;


cout<<"Please enter the score of player <<PLAYER<<":\n";
cin>>score1;


cout<<"Please enter the score of player <<PLAYER<<":\n";
cin>>score2;

cout<<"Please enter the score of player <<PLAYER<<":\n";
cin>>score3;

} 

When I enter the scores in an calculate the average for each player it averages the same amount for each player.

Apr 12, 2009 at 10:44pm
You can try this for the above code and you will have to modify the varibles and other stuff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for(i=1; i <=5; ++)
{
count++;



cout<<"Please enter the scores of player  <<PLAYER[i]<<":\n";
cin>>score1;


cout<<"Please enter the score of player <<PLAYER[i]<<":\n";
cin>>score2;

cout<<"Please enter the score of player <<PLAYER[i]<<":\n";
cin>>score3;

totalscore =score1+score2+score3;
teamscore[i]=score1+score2+score3;
score1=0;
score2=0;
score3=0;


}  

this would mean you have to have arrays for the team scores and the player scores this should give you a start
Apr 12, 2009 at 11:44pm
Is there any way to store these values without out using arrays. Or do I have to create separate way of calculating the averages with separate statements and for statements.
Apr 13, 2009 at 12:22am
bear in mind that if you declare you are using namespace std; like I assume you are as you aren't using std::cout << etc then you cannot use 'count' as a variable as there is already something in the std namespace called count.

http://www.cplusplus.com/forum/beginner/8335/
Apr 13, 2009 at 3:26am
let me try to understand this better:

1. you have 3 teams with 5 players you need to have averages for each player, each team d total avg. for all teams

the for loop you have need to include a variable to increment the total team score

a variable to calculate the aver of each player each of the 5 times you go through the for loop

2. first for loop will need to be for(team=1,team ,team <=3,team++)

in that for loop will be the for(PLAYER=1,PLAYER<=5,PLAYER++)
and calculate that players average and add the scores to the team score variable.

3. before going to the next team you need to calculate that current teams average

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
for team=1,team<=3,team++)
{
    for(i=1; i <=5; ++)
{
count++;



cout<<"Please enter the scores of player  <<PLAYER[i]<<":\n";
cin>>score1;


cout<<"Please enter the score of player <<PLAYER[i]<<":\n";
cin>>score2;

cout<<"Please enter the score of player <<PLAYER[i]<<":\n";
cin>>score3;

totalscore =score1+score2+score3;
teamavg=(score1+score2+score3)/3;
score1=0;
score2=0;
score3=0;

   }  
totalavg=totalscore/15;
}    

somewhere in there you will need to print the avgs. out but i think storing the teams scores and players scores in two arrays would be your best bet then you can use use for loops to print out a nice report of the teams avg and players for each team avgs. at the end of the program
i hope this helps you out
Apr 13, 2009 at 11:49am
closed account (48T7M4Gy)
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
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int maxTeam = 5, maxPlayer = 3;
	double totalAllScores = 0 ;
	double score[5][3];
	double totalTeamScores[5];

	for ( int team = 0; team < maxTeam; team++ )
	{
		totalTeamScores[ team ] = 0;
		cout << "Team no. " << team ;

		for ( int player = 0; player < maxPlayer; player++ )
		{
			cout << " - Player no. " << player << endl;
			cin >> score[ team ][ player ];
			totalTeamScores[ team ] += score[ team ][ player ];
			totalAllScores += score[ team ][ player ];
		}
	}

	for ( int team = 0; team < maxTeam; team++ )
	{
		cout << "Average for team " << team << " points " << totalTeamScores[ team ] / maxPlayer << endl;
	}

	cout << "Average all teams " << totalAllScores / ( maxPlayer * maxTeam ) << endl;

	return 0;
}
Topic archived. No new replies allowed.