An Introduction to Programming with C++: Eighth Edition
Chapter 7 Answers
Review Questions
1. a selection
2. b repetition
3. a selection
4. c both selection and repetition
Exercises Pencil and Paper
1. The answer to this TRY THIS Exercise is located at the end of Chapter 7 in the book.
2. The answer to this TRY THIS Exercise is located at the end of Chapter 7 in the book.
5. numEmployees += 1; (or numEmployees = numEmployees + 1;)
6. totalPay += grossPay; (or totalPay = totalPay + grossPay;)
7. while (age > 18)
8. The answer may vary. Possibilities include the following:
for (int numTimes = 1; numTimes < 11; numTimes += 1)
for (int numTimes = 1; numTimes <= 10; numTimes += 1)
9. Processing steps for Example 2 in Figure 7-32
1. The initialization argument (int x = 3) creates a variable named x and initializes it to 3.
2. The condition argument (x > 0) checks whether the x variable’s value is greater than 0. It is, so
the statement in the loop body displays the x variable’s value (3) on the screen.
10. while (inStock > reorder)
11. quantity = 5; (or quantity = quantity 5; or quantity += –5; or quantity =
quantity + –5;)
12. sales = salesReturns; (or sales = sales salesReturns;)
13. 1. initialize missed counter to 0
2. position both your right arm and your right hand toward the spider
3. shoot a laser beam at the spider
4. repeat while (the laser beam did not hit the spider)
add 1 to the missed counter
Exercises Computer
14. The answer to this TRY THIS Exercise is located at the end of Chapter 7 in the book. The code is entered
in the TryThis14.cpp file, which is contained in either the Cpp8\Chap07\TryThis14 Project-IM folder
(Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
15. The answer to this TRY THIS Exercise is located at the end of Chapter 7 in the book. The code is entered
18. See the Introductory18.cpp file, which is contained in either the Cpp8\Chap07\Introductory18 Project-IM
folder (Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
19. See the Introductory19.cpp file, which is contained in either the Cpp8\Chap07\Introductory19 Project-IM
folder (Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
24. See the Advanced24.cpp file, which is contained in either the Cpp8\Chap07\Advanced24 Project-IM folder
(Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
Input Processing Output
beginning balance Processing items: ending balance
Algorithm:
1. enter beginning balance
2. enter deposit amount
3. repeat while (the deposit amount is greater than or equal to 0)
add the deposit amount to the total deposits
enter another deposit amount
end repeat
4. enter a withdrawal amount
beginning deposit withdrawal total total ending
balance amount amount deposits withdrawals balance
2456.75 200 25 200 25 2617.53
56.50 100 256.50 125
IPO chart information
Input
beginning balance
deposit amount
withdrawal amount
Processing
C++ instructions
double beginningBal = 0.0;
double depositAmt = 0.0;
double withdrawalAmt = 0.0;
double totalDeposits = 0.0;
3. repeat while (the deposit amount is
greater than or equal to 0)
4. enter a withdrawal amount
5. repeat while (the withdrawal amount is
greater than or equal to 0)
add the withdrawal amount to the
while (depositAmt >= 0)
{
cout << endl << “Withdrawal amount
(negative number to stop): “;
cin >> withdrawalAmt;
while (withdrawalAmt >= 0)
{
totalWithdrawal += withdrawalAmt;
beginningBal depositAmt withdrawalAmt totalDeposits totalWithdrawals
0.0 0.0 0.0 0.0 0.0
2456.75 200.0 25.0 200.0 25.0
56.50 100.0 256.50 125.0
endingBal
0.0
25. See the Advanced25.cpp file, which is contained in either the Cpp8\Chap07\Advanced25 Project-IM folder
(Microsoft) or the Cpp8\Chap07 folder (non-Microsoft). The desk-check data will vary. The following
desk-checks use 4 and 2 for the first test, and 10, 1, and 5 for the second test.
Input Processing Output
rate 1 (1 to 3: 150) Processing items: total registered
number registered
Algorithm:
1. enter the number registered
2. repeat while (the number registered
is greater than 0)
add the number registered to the total registered
add the company charge to the total charge
enter the number registered
end repeat
3. calculate the average charge by dividing the
total charge by the total registered
4. display the total registered, total charge, and average charge
rate 1 rate 2 rate 3 number registered company charge total registered
150 100 90 4 400 4
total charge average charge
400 116.67 (rounded to two decimal places)
700
IPO chart information
Input
rate 1 (1 to 3: 150)
rate 2 (4 to 9: 100)
rate 3 (10 and over: 90)
number registered
Processing
Algorithm
1. enter the number registered
2. repeat while (the number registered
is greater than 0)
add the number registered to the
total registered
if (the number registered is less than 4)
add the company charge to the total charge
enter the number registered
end repeat
const int RATE_1TO3 = 150;
const int RATE_4TO9 = 100;
const int RATE_10_AND_OVER = 90;
int numRegistered = 0;
cout << “Number registered by the
first company (negative number or 0
to end): “;
cin >> numRegistered;
while (numRegistered > 0)
{
totalRegistered += numRegistered;
if (numRegistered < 4)
totalCharge += companyCharge;
cout << “Number registered by the
next company (negative number or
0 to end): “;
cin >> numRegistered;
}//end while
registrants: ” << totalRegistered <<
endl;
RATE_1TO3 RATE_4TO9 RATE_10_AND_OVER
150 100 90
150 100 90
numRegistered companyCharge totalRegistered
0 0 0
4 400 4
totalCharge averageCharge
0 0.0
400 116.67 (rounded to two decimal places)
26. See the Advanced26.cpp file, which is contained in either the Cpp8\Chap07\Advanced26 Project-IM folder
(Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
27. See the Advanced27.cpp file, which is contained in either the Cpp8\Chap07\Advanced27 Project-IM folder
(Microsoft) or the Cpp8\Chap07 folder (non-Microsoft).
28. See the SwatTheBugs28.cpp file, which is contained in either the Cpp8\Chap07\SwatTheBugs28 Project-
IM folder (Microsoft) or the Cpp8\Chap07 folder (non-Microsoft). To debug the program, change the
statements that update the counters in the while loop from positive =+ 1; and negative =+ 1;