Consider we have our own \"Programming League\" of four teams, where each Team 3
ID: 3818742 • Letter: C
Question
Consider we have our own "Programming League" of four teams, where each Team 3ompetes among each other. At the end of the league, each team will have had a "Programming Match" with its opponent. In a match, each team is provided a score between 1 and 100. For example here are sample scores: Team 1 100-96 Team 2 Team 3 94 - 92 Team 4 Team 1 94-98 Team 3 Team 2 99-97 Team 4 Team 1 89- 91 Team 4 Team 2 99-98 Team 3 The difference of the scores in a match will be used to determine the champions. In the table below using the above sample scores, each clement of the table shows how many points a Team 1 cross the rows scored more than the 1'eam 1 cross the columns in their respective match. For example, since Team 1 scored 4 points more than Team 2 in their match, Row 1 Column 2 is 4. For the same reason. Row 2 Column 1 is -4. Since Team 3annot play with itself, that corresponding value is zero. To find the winner, we will sum across the columns. In this example, since the Row 3 will have the highest sum across the columns, Team 3 are the champions. Write a program to implement this requirement. In your program, declare a two-dimensional array. You will need to read the scores of each match from the user, calculate the difference and store in your two dimensional array. Now, using the same logic as in the above example, your program needs to determine who won the championship. Display on the screen the elements of the two-dimensional array along with the team that won the championship.Explanation / Answer
Here is your code.. I have added comments to help you understand this better
#include <iostream>
using namespace std;
int main() {
int scoresArr[4][4]; // array for storing scores for 4 teams
int a,b;
for(int k =0;k<4;k++) {
for(int l=0;l<4;l++) {
scoresArr[k][l] = 0; // Initializing the scores to 0
}
}
// getting the scores from user and storing in array
for(int i =0;i<4;i++) {
for(int j=0;j<4;j++) {
if(i != j) { // Condition to check if team is not playing with itself
if(scoresArr[j][i] == 0) { // Condition to check if both teams have already played
cout<<"Enter scores for Match between Team"<<i+1<<" and Team"<<j+1<<": -"<<endl;
cout<<"Score for Team"<<i+1<<" :"<<endl;
cin>>a;
cout<<"Score for Team"<<j+1<<" :"<<endl;
cin>>b;
scoresArr[i][j] = a-b; // calculating difference and storing
} else {
scoresArr[i][j] = -scoresArr[j][i]; // if both teams have already played then score will be negative of previous score.
// eg. Team1 and Team2 previous diff was 2, than Team 2 and Team1 difference will be -2.
}
}
}
}
// Printing the scores array
cout <<"Scores array for the players:"<<endl;
for(int m =0;m<4;m++) {
for(int n=0;n<4;n++) {
cout<< scoresArr[m][n]<<" ";
}
cout<<endl;
}
int sum = 0,maxSum,winner;
// Score of first team taken as maximum for first iteration
maxSum = scoresArr[0][0] + scoresArr[0][1] + scoresArr[0][2]+ scoresArr[0][3];
winner = 1;
for(int m =1;m<4;m++) {
sum = 0;
for(int n=0;n<4;n++) {
sum = sum + scoresArr[m][n]; // Calculating total score of next team
}
if (sum > maxSum) { // if score is new maximum than updating the maximum score and winner team details.
maxSum = sum;
winner = m+1;
}
}
cout << "Champion Team of the game is Team"<<winner<<" with score:"<<maxSum<<endl;
system("pause");
return 0;
}
Output: -
Enter scores for Match between Team1 and Team2: -
Score for Team1 :
100
Score for Team2 :
96
Enter scores for Match between Team1 and Team3: -
Score for Team1 :
94
Score for Team3 :
98
Enter scores for Match between Team1 and Team4: -
Score for Team1 :
89
Score for Team4 :
91
Enter scores for Match between Team2 and Team3: -
Score for Team2 :
99
Score for Team3 :
98
Enter scores for Match between Team2 and Team4: -
Score for Team2 :
99
Score for Team4 :
97
Enter scores for Match between Team3 and Team4: -
Score for Team3 :
94
Score for Team4 :
92
Scores array for the players:
0 4 -4 -2
-4 0 1 2
4 -1 0 2
2 -2 -2 0
Champion Team of the game is Team3 with score:5
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.