Respuesta :
Answer:
The program in cpp for the given scenario is shown.
#include <iostream>
using namespace std;
int main()
{
//variables declared to hold individual and total scores
int score_a=0, a;
int score_b=0, b;
//user input taken for score
for(int i=0; i<4; i++)
{
std::cout << "Enter team A score in quarter " <<(i+1)<<" : ";
cin>>a;
score_a = score_a+a;
std::cout << "Team A score so far: "<< score_a <<std::endl;
std::cout << "Enter team B score in quarter " <<(i+1)<<" : ";
cin>>b;
score_b = score_b+b;
std::cout << "Team B score so far: "<< score_b <<std::endl;
}
//winner team decided by comparing scores
if(score_a>score_b)
std::cout << "Team A has won" << std::endl;
if(score_a==score_b)
std::cout << "It is a tie." << std::endl;
if(score_a<score_b)
std::cout << "Team B has won" << std::endl;
//program ends
return 0;
}
Explanation:
1. The integer variables are declared to hold both individual scores and the total score for both the teams.
int score_a=0, a;
int score_b=0, b;
2. The variables to hold total scores are initialized to 0.
3. Inside for loop, scores for both teams for all 4 quarters are taken from the user.
4. Inside the same for loop, a running total for the score of each team is computed and displayed.
score_a = score_a+a;
score_b = score_b+b;
5. Outside the loop, the total scores of both the teams are compared and the winner team is displayed. This is done using multiple if statements.
if(score_a>score_b)
std::cout << "Team A has won" << std::endl;
if(score_a==score_b)
std::cout << "It is a tie." << std::endl;
if(score_a<score_b)
std::cout << "Team B has won" << std::endl;
6. After each message is displayed, a new line is inserted at the end using keyword, endl.
7. The program ends with a return statement.
8. The whole code is written inside main(). In cpp, it is not mandatory to write the code inside a class since cpp is not a purely object-oriented language.
9. The output is attached in an image.