Chapter 6 Homework Code

advertisement
Programming Challenge 6
By Phillip Lee
1. Markup
Write a program that asks the user to enter an item’s wholesale cost and its markup
percentage. It should then display the item’s retail price. For example:
• If an item’s wholesale cost is 5.00 and its markup percentage is 100%, then the item’s
retail price is 10.00.
• If an item’s wholesale cost is 5.00 and its markup percentage is 50%, then the item’s
retail price is 7.50.
The program should have a function named calculateRetail that receives the wholesale cost and the
markup percentage as arguments and returns the retail price of the item.
//main
Input
Cost, Percentage,
Process
Call calculateRetail
Output
retail
Process
Cost = cost + cost * percentage
Output
retailPrice
//calculateRetail
Input
Cost, Percentage
#include <iostream>
using namespace std;
double calculateRetail(double c, double mUp);
int main()
{
double cost, markup, retail;
cout << "Please enter retail price: \n";
cin >> cost;
cout << "Enter markup percent: \n";
cin >> markup;
retail = calculateRetail(cost, markup);
cout << "The retail price for the item is " << retail <<
endl;
system("pause");
return 0;
}
double calculateRetail(double c, double mUp)
{
return c = c + c*mUp;
}
2. Celsius Temperature Table
The formula for converting a temperature from Fahrenheit to Celsius is
C = 5/9 ( F – 32)
where F is the Fahrenheit temperature and C is the Celsius temperature. Write a function named celsius
that accepts a Fahrenheit temperature as an argument. The function should return the temperature,
converted to Celsius. Demonstrate the function by calling it in a loop that displays a table of the
Fahrenheit temperatures 0 through 20 and their Celsius equivalents.
Input
Double F
Process
C = 5/9 (F – 32)
//function prototype
Double Celsius, double F();
#include <iostream>
#include <iomanip>
using namespace std;
double celsius(double f);
int main()
{
Output
Double C
double Cdegree;
cout << setw(10) << "Fahrenheit" << setw(10) <<
"Celsius"
<< endl;
for (int i = 0; i < 21; i++)
{
Cdegree = celsius(i);
cout << fixed;
cout << setprecision(2);
cout << setw(10) << i << setw(10) << Cdegree <<
endl;
}
system("pause");
return 0;
}
double celsius(double f)
{
return (f - 32) * 5 / 9;
}
3. Falling Distance
The following formula can be used to determine the distance an object falls due to gravity in a speciļ¬c
time period:
d
1⁄ 2 gt2
The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the time in
seconds that the object has been falling.
Write a function named fallingDistance that accepts an object’s falling time (in seconds) as an argument.
The function should return the distance, in meters, that the object has fallen during that time interval.
Write a program that demonstrates the function by calling it in a loop that passes the values 1 through
10 as arguments and displays the return value.
Input
Time
Processing
D = 1/2 gt^2
Output
distance
//double fallingDistance(int t);
#include <iostream>
#include <iomanip>
using namespace std;
double fallingDistance(int t);
int main()
{
double time;
cout << setw(10) << "Time" << setw(10) << "Distance"
<< endl;
for (int i = 0; i < 11; i++)
{
time = fallingDistance(i);
cout << fixed;
cout << setprecision(2);
cout << setw(10) << i << setw(10) << time << endl;
}
system("pause");
return 0;
}
double fallingDistance(int t)
{
return t = 0.5 * 9.8 * t * t;
}
5. Winning Division
Write a program that determines which of a company’s four divisions (Northeast, Southeast, Northwest,
and Southwest) had the greatest sales for a quarter. It should include the following two functions, which
are called by main.
• double getSales() is passed the name of a division. It asks the user for a division’s quarterly sales figure,
validates that the input is not less than 0, then returns it. It should be called once for each division.
• void findHighest() is passed the four sales totals. It determines which is the largest and prints the name
of the high grossing division, along with its sales figure.
Input
NE_Sales, SE_Sales, NW_Sales,
SW_Sales
Processing
Double getSales();
findHighest();
Output
highestSales;
//double getSales();
//double findHighest();
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function Prototypes
double getSales(string);
void findHighest(double, double, double, double);
int main()
{
double salesNE, salesSE, salesNW, salesSW;
salesNE
salesSE
salesNW
salesSW
=
=
=
=
getSales("Northeast");
getSales("Southeast");
getSales("Northwest");
getSales("Southwest");
findHighest(salesNE, salesSE, salesNW, salesSW);
system("pause");
return 0;
}
double getSales(string divName)
{
double sales;
cout << "Enter the quarterly sales for the " << divName
<< " division: ";
cin >> sales;
while (sales < 0.0)
{
cout << "Sales figures cannot be negative. Please
re-enter. \n";
cout << "Enter the quarterly sales for the " <<
divName << " division: \n";
cin >> sales;
}
return sales;
}
void findHighest(double nEast, double sEast, double nWest,
double sWest)
{
string divName = "Northeast";
double highest = nEast;
if (sEast > highest)
{
highest = sEast;
divName = "Southeast";
}
if (nWest > highest)
{
highest = sWest;
divName = "Southwest";
}
cout << fixed << showpoint << setprecision(2);
cout << "\nThe " << divName
<< " division had the highest sales this quarter.
\n";
cout << "Their sales were $" << highest << endl;
}
Download