Question

In: Computer Science

Suppose that in a game that you are making, the player wins if her score is...

  1. Suppose that in a game that you are making, the player wins if her score is at least 100. Write a function called hasWon that takes as a parameter the player score and prints “Game won!” if she has won, but nothing otherwise.

  2. In the future, everyone will be able to wear sensors that monitor their health. One task of these sensors is to monitor body temperature: if it falls outside the range 97.9F to 99.3F the person may be getting sick. Write a function called monitor that takes a temperature (in Fahrenheit) as a parameter and prints out a warning if it is not within this range.

  3. Write a function called winner that takes as parameters two players’ scores and prints out who is the winner (Player 1 or Player 2). The higher score wins and you can assume that there are no ties.

  4. Repeat the previous question, but now assume that ties are possible and that you should report them.

  5. Your firm is looking to buy computers from a distributor for $1500 per machine. The distributor will give you a 5% discount if you purchase more than 20 computers. Write a function called cost that takes as a parameter the number of computers you wish to buy and prints the cost of buying them from this distributor.

  6. Repeat the previous question, except now you should assume that the cost per machine ($1500 above), the number of computers to get a discount (20 above), and the discount (5% above) are all passed as parameters of the function.

  7. The speeding ticket policy in a nearby town is $50 plus $5 for each mph over the posted speed limit. In addition, there is an extra penalty of $200 for all speeds above 90 mph. Write a program that accepts a speed limit and a clocked speed and returns the fine amount (and 0 if within the speed limit).

Solutions

Expert Solution

The functions are in C++.

All the functions are in the same code.

Code:

#include <iostream>

using namespace std;

// takes as a parameter the player score
// and prints “Game won!” if she has won, but nothing otherwise.
void hasWon(int score)
{
// the player wins if her score is at least 100
if(score >= 100)
cout << "Game won!" << endl;
}

// takes a temperature (in Fahrenheit) as a parameter
// and prints out a warning if it is not within the range.
void monitor(double temp)
{
// if temperature falls outside the range 97.9F to 99.3F,
// the person may be getting sick
if(!(temp >= 97.9 && temp <= 99.3))
cout << "Warning: temperature not within the range of 97.9 F - 99.3 F" << endl;
}

// takes as parameters two players’ scores
// and prints out who is the winner (Player 1 or Player 2).
// assumption - no ties
void winner(int score_a, int score_b)
{
if(score_a > score_b)
cout << "Winner - Player 1" << endl;
else
cout << "Winner - Player 2" << endl;
}

// Repeat the previous question, but now assume that ties are possible
// and that you should report them.
void winnerWithTies(int score_a, int score_b)
{
if(score_a > score_b)
cout << "Winner - Player 1" << endl;
else if(score_b > score_a)
cout << "Winner - Player 2" << endl;
else // tie
cout << "Players are tied" << endl;
}

// Your firm is looking to buy computers from a distributor for $1500 per machine.
// The distributor will give you a 5% discount if you purchase more than 20 computers.
// takes as a parameter the number of computers you wish to buy
// and prints the cost of buying them from this distributor.
void cost(int number)
{
double c;
// give discount
if(number > 20)
{
c = 1500.0 * number * 0.95;
}
else
{
c = 1500.0 * number;
}
// output
cout << "Cost of buying " << number << " machines: $" << c << endl;
}

// Repeat the previous question,
// except now you should assume that the cost per machine ($1500 above),
// the number of computers to get a discount (20 above),
// and the discount (5% above) are all passed as parameters of the function.
void cost1(int number, double costPerM, int nD, double discount)
{
double c;
  
// give discount
if(number > nD)
{
c = costPerM * number * (1-discount/100);
}
else
{
c = costPerM * number;
}
// output
cout << "Cost of buying " << number << " machines: $" << c << endl;
}

// The speeding ticket policy in a nearby town is
// $50 plus $5 for each mph over the posted speed limit.
// In addition, there is an extra penalty of $200 for all speeds above 90 mph.
// accepts a speed limit and a clocked speed
// and returns the fine amount (and 0 if within the speed limit).
int speedLimit(int speed_limit, int clocked_speed)
{
int fine = 0;
  
// no fine if speed within limit
if(clocked_speed <= speed_limit)
return fine;
  
fine = 50 + (clocked_speed - speed_limit) * 5;
  
// check if clocked_speed > 90
if(clocked_speed > 90)
fine += 200;
  
return fine;
}

int main()
{
// sample run
  
// function 1
hasWon(20);
hasWon(230);
  
// function 2
monitor(98.5);
monitor(100.3);
  
// function 3
winner(150, 160);
winnerWithTies(150, 160);
winnerWithTies(150, 150);
  
// function 4
cost(22);
cost1(22, 1500, 20, 5);
  
// function 5
cout << "Fine for speed 100 mph: $" << speedLimit(40, 100) << endl;

return 0;
}

Sample run:

Code screenshots:


Related Solutions

Suppose you are playing a dice game and you have three options to find a score....
Suppose you are playing a dice game and you have three options to find a score. The options are: A. Rolling an 11-sided die and using the outcome as your score.* B. Rolling two 4-sided dice, adding 1 to their sum, and using that number as your score. C. Rolling two 4-sided dice, doubling the result of the first die and adding it to the result of the second, subtracting 1 from this result, and using this number as your...
Suppose you are playing a dice game and you have three options to find a score....
Suppose you are playing a dice game and you have three options to find a score. The options are: Rolling an 11-sided die and using the outcome as your score.* Rolling two 4-sided dice, adding 1 to their sum, and using that number as your score. Rolling two 4-sided dice, doubling the result of the first die and adding it to the result of the second, subtracting 1from this result, and using this number as your score. Assume that each...
Question 02: Game theory is the study of multi- player decision making in situation where the...
Question 02: Game theory is the study of multi- player decision making in situation where the choices of each player may affect the pay-offs received by other players. Arrange how many types you can categorized Game Theory. ( 10 Marks) . Note: Plagiarism is strictly prohibited please do not copy paste from internet
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
A) A basketball player is an 80% foul shooter. Find the probability of her making: 1....
A) A basketball player is an 80% foul shooter. Find the probability of her making: 1. 7 of 10 foul shots 2. 8 of 10 foul shots 3. 10 for 10 foul shots B) Over several games the shooter is given 100 foul shots. Find (i.e. estimate): 1. She makes more than 80 shots 2. She makes less than 80 shots 3. She makes less than 50 shots Please show all work and graphs
Study E. A basketball player is practicing making free throws. On average, the probability of her...
Study E. A basketball player is practicing making free throws. On average, the probability of her making a free throw is 0.78. She finds that in the next 50 free throws, she makes 31. Is this outcome significantly more than what would be expected? Assume a = 0.052-tail and independence between shots. Refer to Study E. What is the null hypothesis in terms of the variables in the study Refer to Study E. What is the alternative hypothesis in terms...
Coin taking game This game is played between 2 players, player 1 and player 2. There...
Coin taking game This game is played between 2 players, player 1 and player 2. There are two piles of coins. The values of a coin can be any integer. Both players know the values of all coins in both piles. Player 1 makes the first move, and play alternates between the players. A move consists of taking a coin from the top of either of the piles (either player can take from either pile). The game ends when both...
Connect Four is a game that alternates player 1 and player 2. You should keep track...
Connect Four is a game that alternates player 1 and player 2. You should keep track of whose turn it is next. For this program we will just be building the structure not the full program. Your assignment: Create a two player Connect Four C language program that follows the requirements below. Create functions: Initialization – print “Setting up the game”. Ask each player their name. Teardown – print “Destroying the game” Accept Input – accept a letter for which...
Sally plays a game and wins with probability p. Every week, she plays until she wins...
Sally plays a game and wins with probability p. Every week, she plays until she wins two games, and then stops for the week. Sally calls it a "lucky week" if she manages to achieve her goal in 7 or less games. a) If p = 0.2, what's the probability that Sally will have a "lucky week" next week? b) What's the probability of exactly 3 "lucky weeks" in the next 5 weeks? What's the expected number of "lucky weeks"...
Sally plays a game and wins with probability p. Every week, she plays until she wins...
Sally plays a game and wins with probability p. Every week, she plays until she wins two games, and then stops for the week. Sally calls it a "lucky week" if she manages to achieve her goal in 7 or less games. a) If p = 0.2, what's the probability that Sally will have a "lucky week" next week? b) What's the probability of exactly 3 "lucky weeks" in the next 5 weeks? What's the expected number of "lucky weeks"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT