In: Computer Science
Programming language: C++
suggested software: Code::Blocks
Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward.
Submit your design, code, and execution result
via file, if possible
The design/algorithm of the given problem is as follows:-
start
totA=0,totB=0
print "Enter score of both team during each innings:"
for i=1 to 9 do
print "Inning "+i+":"
print "Team A: "
input A
print "Team B: "
input B
totA=totA+A
totB=totB+B
end-for
print "Final score of basketball game:"
print "Team A\tTeam B"
print totA+"\t"+totB
stop
The C++ code is as follows:-
#include<iostream>
using namespace std;
int main()
{
int A,B,totA=0,totB=0;
cout<<"Enter score of both team during each
innings:"<<endl;
for(int i=1;i<=9;i++)
{
cout<<"Inning "<<i<<":"<<endl;
cout<<"Team A: ";
cin>>A;
cout<<"Team B: ";
cin>>B;
totA+=A;
totB+=B;
}
cout<<"Final score of basketball game:"<<endl;
cout<<"Team A\tTeam B"<<endl;
cout<<totA<<"\t"<<totB<<endl;
return 0;
}
The output of the above code is as follows:-
Enter score of both team during each innings:
Inning 1:
Team A: 12
Team B: 21
Inning 2:
Team A: 23
Team B: 20
Inning 3:
Team A: 16
Team B: 23
Inning 4:
Team A: 28
Team B: 25
Inning 5:
Team A: 28
Team B: 22
Inning 6:
Team A: 21
Team B: 16
Inning 7:
Team A: 25
Team B: 20
Inning 8:
Team A: 26
Team B: 28
Inning 9:
Team A: 18
Team B: 19
Final score of basketball game:
Team A Team B
197 194
The screenshot of the above code is as follows:-