An Introduction to Programming with C++: Eighth Edition
Chapter 5 Answers
Review Questions
1. c in any instruction after the declaration statement in the if statement’s true path
2. a if (average > 70.5 && average < 80.5)
3. b if (letter == ‘Z’ || letter == ‘z’)
4. a true
Exercises Pencil and Paper
1. The answer to this TRY THIS Exercise is located at the end of Chapter 5 in the book.
2. The answer to this TRY THIS Exercise is located at the end of Chapter 5 in the book.
4.
if (units <= 0)
cout << “Entry error” << endl;
else
5.
if (marySales < jimSales)
{
lowSales = marySales;
highSales = jimSales;
6.
if (toupper(department) == ‘A’ || toupper(department) == ‘B’)
raise = salary * 0.02;
7. The corrections are shaded.
if (toupper(code) == ‘X’)
Exercises Computer
8. The answer to this TRY THIS Exercise is located at the end of Chapter 5 in the book. The code is entered
in the TryThis8.cpp file, which is contained in either the Cpp8\Chap05\TryThis8 Project-IM folder
(Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
9. The answer to this TRY THIS Exercise is located at the end of Chapter 5 in the book. The code is entered
11. See the Introductory11.cpp file, which is contained in either the Cpp8\Chap05\Introductory11 Project-IM
folder (Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
Input Processing Output
pounds Processing items: total owed
price per pound sales tax
charged for sales tax (Y or N)
sales tax rate (3.5%)
Algorithm:
1. enter the pounds, price per pound, and charged for sales tax
2. calculate the total owed by multiplying the pounds
by the price per pound
pounds price per pound charged for sales tax sales tax rate sales tax total owed
5 13.69 Y .035 2.40 68.45
IPO chart information
Input
pounds
Processing
sales tax
Output
total owed
Algorithm
1. enter the pounds, price per pound, and
2. calculate the total owed by multiplying
the pounds by the price per pound
3. if (charged for sales tax is Y)
C++ instructions
int pounds = 0;
double tax = 0.0;
double total = 0.0;
cout << “Pounds ordered: “;
cin >> pounds;
total = pounds * poundPrice;
if (toupper(chargedTax) == ‘Y’)
pounds poundPrice chargedTax TAX_RATE tax total
0 0.0 0.035 0.0 0.0
5 13.69 Y 2.40 68.45
12. See the Introductory12.cpp file, which is contained in either the Cpp8\Chap05\Introductory12 Project-IM
folder (Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
Input Processing Output
item 1 price Processing items: none total owed
item 2 price
Algorithm:
1. enter the item 1 price and item 2 price
2. if (the item 1 price is lower than the item 2 price)
calculate the total owed by dividing
3. display the total owed
item 1 price item 2 price total owed
24.99 10 29.99
IPO chart information
Input
item 1 price
item 2 price
Processing
none
2. if (the item 1 price is lower than the
item 2 price)
calculate the total owed by dividing
the item 1 price by 2 and then adding
the item 2 price to the result
C++ instructions
double item1Price = 0.0;
double item2Price = 0.0;
if (item1Price < item2Price)
total = item1Price / 2 + item2Price;
item1Price item2Price total
0.0 0.0 0.0
13. See the Intermediate13.cpp file, which is contained in either the Cpp8\Chap05\Intermediate13 Project-IM
folder (Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
Input Processing Output
current reading Processing items: none gallons of water
previous reading total charge
Algorithm:
1. enter the current reading and previous reading
2. calculate the gallons of water by subtracting
current reading previous reading gallon charge minimum charge
16000 13000 .007 16.67
3675 1650 .007 16.67
gallons of water total charge
3000 21
IPO chart information
Input
current reading
Processing
none
Output
gallons of water
total charge
Algorithm
1. enter the current reading and
previous reading
3. calculate the total charge by multiplying
the gallons of water by the gallon charge
4. if (the total charge is less than the
minimum charge)
C++ instructions
int current = 0;
int gallons = 0;
double total = 0.0;
cout << “Current reading: “;
cin >> current;
total = gallons * CHG_PER_GAL;
if (total < MINIMUM_CHG)
current previous CHG_PER_GAL MINIMUM_CHG
0 0 .007 16.67
16000 13000
0 0 .007 16.67
3675 1650
gallons total
0 0.0
14. See the Intermediate14.cpp file, which is contained in either the Cpp8\Chap05\Intermediate14 Project-IM
folder (Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
Input Processing Output
gender (M or F) Processing items: BMR
Algorithm:
1. enter the gender, age in years, weight in pounds,
and height in feet
2. calculate the weight in kgs by dividing the
weight in pounds by 2.2
gender age in years weight in pounds height in feet
M 25 175 6
F 31 130 5.5
weight in kgs height in cms BMR
IPO chart information
Input
gender (M or F)
Processing
weight in kgs
height in cms
Output
BMR
Algorithm
1. enter the gender, age in years, weight in
2. calculate the weight in kgs by dividing
the weight in pounds by 2.2
3. calculate the height in cms by
calculate BMR as follows: (10 *
weight in kgs) + (6.25 * height in
cms) (5 * age in years) – 161
end if
5. display the BMR
C++ instructions
char gender = ‘ ‘;
double weightKgs = 0.0;
double heightCms = 0.0;
double bmr = 0.0;
cout << “Gender (M or F): “;
weightKgs = weightPounds / 2.2;
heightCms = heightFeet * 12 * 2.54;
heightCms) – (5 * age) – 161;
//end if
cout << “BMR: ” << bmr << endl;
gender age in years weight in pounds height in feet
0 0.0 0.0
weight in kgs height in cms BMR
0.0 0.0 0.0
15. See the Advanced15.cpp file, which is contained in either the Cpp8\Chap05\Advanced15 Project-IM folder
(Microsoft) or the Cpp8\Chap05 folder (non-Microsoft).
Input Processing Output
Algorithm:
1. enter the amount owed and amount paid
2. if (the amount paid is less than the amount owed)
display a message
else
calculate the change by subtracting the
amount owed from the amount paid and
multiplying the difference by 100
calculate the dimes by dividing the remainder by 10
calculate the remainder by multiplying the dimes
by 10 and then subtracting the difference from
the remainder
amount owed amount paid remainder change dollars
75.34 80 66 466 4
16
6
quarters dimes nickels pennies
2 1 1 1
1 0 1 3
IPO chart information
Input
amount owed
amount paid
Processing
remainder
Output
change
Algorithm
1. enter the amount owed and amount paid
2. if (the amount owed is greater than
the amount paid)
display a message
else
calculate the change by subtracting
the amount owed from the amount
paid and multiplying the difference
by 100
C++ instructions
double owed = 0.0;
double paid = 0.0;
double remain = 0.0;
double change = 0.0;
cout << “Enter the amount owed: “;
if (owed > paid)
cout << “The amount owed is more
than the amount paid.” << endl;
else
{
change = (paid – owed) * 100;
calculate the quarters by dividing
the remainder by 25
calculate the remainder by
multiplying the dimes by 10 and
then subtracting the difference
from the remainder
display the change, dollars,
quarters, dimes, nickels, and pennies
end if
quarters =
static_cast<int>(remain / 25);
remain = remain – dimes * 10;
cout << “Change: ” << change / 100
<< endl;
cout << “Dollars: ” << dollars
<< endl;
owed paid remain change dollars
0.0 0.0 0.0 0.0 0
75.34 80.0 66.0 466.0 4
16.0
6.0
quarters dimes nickels pennies
0 0 0 0.0
16. See the Advanced16.cpp file, which is contained in either the Cpp8\Chap05\Advanced16 Project-IM folder
(Microsoft) or the Cpp8\Chap05 folder (non-Microsoft). Change the if clause to if (fabs(quotient
3.33333) <= 0.00001)
17. See the SwatTheBugs17.cpp file, which is contained in either the Cpp8\Chap05\SwatTheBugs17 Project-