Problem with a 2 dimensional array

In the array, it is very simple I need to write code that displays the total salary for each of the 3 teams whose player salaries are stored in the above array.
It is taking in that with no problem, but there is two things that it is doing wrong

1: it is displaying an average of the salary entered for the team

2: at the end it displays a long negative number like "Team Salaries: -572662206.7"

can you offer any insight.

here is my code

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
const int numberOfteams = 3;
const int numberOfplayers = 4;

int player_salary[numberOfplayers][numberOfteams];
int team,
player,
salary;
double salary_sum,
team_sum;

cout << setprecision(1)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

// Obtain and store salaries for each player

cout << "Enter exactly " << numberOfplayers
<< " salaries for each team." << endl;
cout << "Separate the salaries by one or more spaces." << endl;

for (team = 0; team < numberOfteams; ++team)
{
cout << endl << endl;
cout << "Salaries for Players on Team " << team + 1 << ": ";
for (salary = 0; salary < numberOfplayers; ++salary)
cin >> player_salary[team][salary];
}

// Calculate and display the salaries for each team.

for (player = 0; player < numberOfplayers; ++player)
{
salary_sum = 0;
for (salary = 0; salary < numberOfteams; ++salary)
salary_sum += player_salary[player][salary];
team_sum = (double) salary_sum / numberOfteams;

cout << endl << endl;
cout << "Team Salaries: " << setw(3) << team_sum;
}

cout << endl;

return 0;
}




thanks for any help ..
Last edited on
Pay attention:
int player_salary[numberOfplayers][numberOfteams];

and when you use it
cin >> player_salary[team][salary];

That writes to a unknown position:
If the first index reffers to player and the second to teams you CAN NOT exchange them. This cause bad things:
you are writing in a position which is not allowed (player_salary[3][2] is somewhere but is out of boundaries).

Your code is confusing: why are you using the variable salary to cycle over players and (later) over teams?
Or better: why do you need it?

Your for cycle should always look like
1
2
3
4
5
6
for (player = 0; player<numberOfplayers;++player)
{
   for (team = 0; team<numberOfteams;++player)
   {//do something on player_salary[player][team]
   }
}

Last edited on
it`s too difficult to point out errors without line numbers.
please place code tags round your code
01. #include <iostream>
03. #include <iomanip>
03.
04. using namespace std;

05. int main()
06. {
07. const int numberOfteams = 3;
08. const int numberOfplayers = 4;

09. int player_salary[numberOfplayers][numberOfteams];
10. int team,
11. player,
12. salary;
13. double salary_sum,
14. team_sum;
15.
16. cout << setprecision(1)
17. << setiosflags(ios::fixed)
18. << setiosflags(ios::showpoint);
19.
20. // Obtain and store salaries for each player

21. cout << "Enter exactly " << numberOfplayers
22. << " salaries for each team." << endl;
23. cout << "Separate the salaries by one or more spaces." << endl;
25.
26. for (team = 0; team < numberOfteams; ++team)
27. {
28. cout << endl << endl;
29. cout << "Salaries for Players on Team " << team + 1 << ": ";
30. for (salary = 0; salary < numberOfplayers; ++salary)
31. cin >> player_salary[team][salary];
32. }
33.
34.// Calculate and display the salaries for each team.
35.
36. for (player = 0; player < numberOfplayers; ++player)
37. {
38. salary_sum = 0;
39. for (salary = 0; salary < numberOfteams; ++salary)
40. salary_sum += player_salary[player][salary];
41. team_sum = (double) salary_sum / numberOfteams;
42.
43. cout << endl << endl;
44. cout << "Team Salaries: " << setw(3) << team_sum;
45. }
46.
47. cout << endl;
48.
49. return 0;
50. }
51.


thanks for the assistance...
Last edited on
Topic archived. No new replies allowed.