In: Computer Science
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.
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.
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.
Repeat the previous question, but now assume that ties are possible and that you should report them.
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.
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.
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).
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: