Using C++ Consider we have our own “Programming League” of four teams, where eac
ID: 3816268 • Letter: U
Question
Using C++
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- 91Team 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 element of the table shows how many points a Team 1cross the rows scored more than the Team 1cross 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.
HINT:Create a nested loop, assign inner loop to columns and outer loop to rows. Ask the user for the scores of the match between the two teams specified by the two loop control variables. After the user enters the scores, calculate the difference and store in appropriate location in the array. Remember the user will have to enter the score of the same match twice—for example to calculate Row 1 Column 2 and Row 2 Column 1 in the above table. Can you avoid this requirement? Think about it, but you don’t have to implement this to get full grade for this problem. Remember a Team 3annot play against itself, so don’t ask the user for the score in that case.
Team 1 Team 2 Team 3 Team 4 Team 1 Team 2 Team 3 -1 C Team 4 -2-2-2-0 Tu--4-1 0-2 T-4 O T40 Tu-0 |-4-4-2Explanation / Answer
Here is your code.. I have added the optional functionality also for you i.e. avoiding Row 2 Column 1 if row 1 Column 2 is already there..
#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[1][0] + scoresArr[2][0]+ scoresArr[3][0];
winner = 1;
for(int m =1;m<4;m++) {
sum = 0;
for(int n=0;n<4;n++) {
sum = sum + scoresArr[n][m]; // 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;
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 :92
Enter scores for Match between Team1 and Team4: -
Score for Team1 :94
Score for Team4 :98
Enter scores for Match between Team2 and Team3: -
Score for Team2 :99
Score for Team3 :97
Enter scores for Match between Team2 and Team4: -
Score for Team2 :89
Score for Team4 :91
Enter scores for Match between Team3 and Team4: -
Score for Team3 :99
Score for Team4 :98
Scores array for the players:
0 4 2 -4
-4 0 2 -2
-2 -2 0 1
4 2 -1 0
Champion Team of the game is Team2 with score:4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.