A basketball game has four quarters. Write a program to do the following. Use a for loop to input scores of team A and team B in each of the four quarters. Every time a score is entered, update and display the current total score of that team. After all four quarters, compare the total scores of the two teams and display the outcome of the game (“Team A has won”, “Team B has won” or “It is a tie”)

Enter team A score in this quarter: 22
Team A score so far: 22
Enter team B score in this quarter: 24
Team B score so far: 24
Enter team A score in this quarter: 19
Team A score so far: 41
Enter team B score in this quarter: 26
Team B score so far: 50
Enter team A score in this quarter: 25
Team A score so far: 66
Enter team B score in this quarter: 22
Team B score so far: 72
Enter team A score in this quarter: 21
Team A score so far: 87
Enter team B score in this quarter: 20
Team B score so far: 92
Team B has won

Enter team A score in this quarter: 22
Team A score so far: 22
Enter team B score in this quarter: 20
Team B score so far: 20
Enter team A score in this quarter: 26
Team A score so far: 48
Enter team B score in this quarter: 21
Team B score so far: 41
Enter team A score in this quarter: 19
Team A score so far: 67
Enter team B score in this quarter: 28
Team B score so far: 69
Enter team A score in this quarter: 25
Team A score so far: 92
Enter team B score in this quarter: 23
Team B score so far: 92
It is a tie


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.

Ver imagen bucuresti