Expression Exercises
Invent something like the following.
Each program will get input
from
cin
,
and, if the input was successful,
will store the input into one or more variables.
Decide if each variable should be an
int
or a
double
,
and pick a good name for each variable,
Write an expression to compute the answer.
Store the value of the expression into another variable,
and then feed this other variable to
cout
.
-
[1 human year = 7 dog years.
Good names for the two variables might be
humanYears
and
dogYears
.]
How old are you? 20
That’s 140 dog years!
-
[Cook the turkey for 13 minutes per pound.]
How many pounds does the turkey weigh? 12
Then you have to cook it for 146 minutes.
That’s 2 hours and 26 minutes.
-
[Define four variables to hold the four input numbers.]
How many pennies do you have? 3
How many nickles do you have? 5
How many dimes do you have? 10
How many quarters do you have? 6
Then you have a total of 268 cents.
That’s 2 dollars and 68 cents.
-
[Dolbear’s law: chirps per 15 seconds, plus 40,
equals the temperature in Fahrenheit.]
How many times did the cricket chirp in 15 seconds? 30
Then the temperature is 70 degrees Fahrenheit.
That’s 21.1111 degrees Celsius.
-
How many quarters do you need for the washer? 20
How many quarters do you need for the dryer? 5
Then you need a total of 25 quarters.
That’s 6 dollars, and maybe some change.
[If we had already covered
if
statements,
I could ask you to put the “and maybe some change”
into an
if
statement because we don’t always want to output this phrase.
But we haven’t, so I can’t.]
-
[1 degree = π/180 radians.]
What is the size of your angle in degrees? 90
Then the size of your angle in radians is 1.5708
-
[The New York City taxi fare
(not counting the various surcharges)
is three dollars, plus
70¢ for each 1/5 mile.]
How many miles are you going? 3
The taxi ride will cost 13.5 dollars.