119k views
9 votes
(Apply) Students will write a function and loop to iterate until a condition is met LO: (Analyze) Students will identify edge cases from a problem statement. In this game of volleyball, two teams compete to be the first team to score 21 points. However, to win the game, a team must also lead by two points. For example, if team A is tied with team B (20-20) and then scores, they will only be ahead by one point (21-20). Then, if team B scores a point, the game will be tied again (21-21). Then, if either team can score two consecutive points, they will win. Write a program that reads data about volleyball matches from standard input and then determines the outcome of the game. If the input word is "A" it means that team A scored a point. If the input word is "B" it means that team B scored a point. If the input word is "end" it means that the game ended. Write a function that can be used in a loop as part of the condition. It should evaluate if either team is a winner. Call the function weHaveAWinner. In main, calculate the scores and announce the team that won or that the game was a draw. Display the game result in this format:

Team A won! (21-14)
Game ended as a draw. (21-21)
#include
using namespace std;
/*
* Determines the outcome of a volleyball game.
*/
int main() {
return 0;
}

1 Answer

13 votes

Answer:

The program is as follows:

#include<iostream>

using namespace std;

void weHaveAWinner(int A, int B){

if(A-2>=B){

cout<<"Team A won! ("<<A<<"-"<<B<<")"; }

else if(B-2>=A){

cout<<"Team B won! ("<<A<<"-"<<B<<")";}

else if(B == A){

cout<<"Game ended as a draw. ("<<A<<"-"<<B<<")";}

else{

cout<<"Team A: "<<A<<" - Team B: "<<B;}

}

int main() {

int teamA = 0; int teamB = 0;

string score;

cout<<"Score: "; cin>>score;

while(score != "end"){

if(score == "A"){

teamA++;}

else if(score == "B"){

teamB++;}

cout<<"Score: "; cin>>score;

}

weHaveAWinner(teamA,teamB);

return 0;

}

Step-by-step explanation:

This defines the weHaveAWinner function, which receives the scores of both teams as its parameters

void weHaveAWinner(int A, int B){

This checks if team A score at least 2 more than team B. If yes, it declares team A the winner

if(A-2>=B){

cout<<"Team A won! ("<<A<<"-"<<B<<")"; }

This checks if team B score at least 2 more than team A. If yes, it declares team B the winner

else if(B-2>=A){

cout<<"Team B won! ("<<A<<"-"<<B<<")";}

This checks for a draw

else if(B == A){

cout<<"Game ended as a draw. ("<<A<<"-"<<B<<")";}

This checks if both team are within 1 point of one another

else{

cout<<"Team A: "<<A<<" - Team B: "<<B;}

}

The main begins here

int main() {

This declares and initializes the score of both teams to 0

int teamA = 0; int teamB = 0;

This declares score as string

string score;

This prompts the user for score

cout<<"Score: "; cin>>score;

This loop is repeated until the user inputs "end" for score

while(score != "end"){

If score is A, then teamA' score is incremented by 1

if(score == "A"){

teamA++;}

If score is B, then teamB' score is incremented by 1

else if(score == "B"){

teamB++;}

This prompts the user for score

cout<<"Score: "; cin>>score;

}

This calls the weHaveAWinner function at the end of the loop

weHaveAWinner(teamA,teamB);