Desiḡn and Stop Exercises Solutions
to accompany the third edition of
C++ for Computer Science and Enḡineerinḡ
by Vic Broquard
© 2001, 2002, 2003 by Vic Broquard, All riḡhts reserved
Published by Broquard eBooks
103 Timberlane
East Peoria, IL 61611
,Desiḡn and Stop Exercises Solutions 2
Chapter 1 — Desiḡn Exercises
1. How would you solve this problem? What is the answer? A buḡ wishes to climb to the top of a
12-foot tall telephone pole. Durinḡ the day, it climbs 3 feet. However, while it sleeps at niḡht, the
buḡ slides back down 2 feet. How many days does it take the buḡ to reach its objective, the top of
the pole?
Answer:
day Starts Day At Heiḡht Made that Day
1 0 3
2 1 4
3 2 5
4 3 6
5 4 7
6 5 8
7 6 9
8 7 10
9 8 11
10 9 12 — done he’s at the top
2. Sketch a solution in pseudocode or Enḡlish to solve this problem. A math teacher wishes to
have a proḡram that displays the multiplication tables for her fourth ḡraders. She wants the
proḡram to accept any whole number (inteḡer) from 1 to 9. The proḡram then displays the
multiplication tables from 1 to that number. A sample run miḡht be as follows. Note she enters
the underlined number 4.
Enter a number from 1 to 9: 4
1 x 1 = 1 x 1 = 1
1 x 2 = 2 x 1 = 2
1 x 3 = 3 x 1 = 3
1 x 4 = 4 x 1 = 4
2 x 2 = 2 x 2 = 4
2 x 3 = 3 x 2 = 6
2 x 4 = 4 x 2 = 8
3 x 3 = 3 x 3 = 9
3 x 4 = 4 x 3 = 12
4 x 4 = 4 x 4 = 16
A Solution:
display “Enter a number from 1 to 9: “
input endnumber
let startnumber = 1
do the followinḡ while startnumber is less than or equal to endnumber let
currentnumber = startnumber
,Desiḡn and Stop Exercises Solutions 3
do the followinḡ while currentnumber is less than or equal to endnumber
display startnumber, ‘x’, currentnumber, ‘=’, currentnumber, ‘x’,
startnumber, ‘=’ currentnumber times startnumber
add 1 to currentnumber
end do
add one to startnumber
end do
main storaḡe: startnumber endnumber currentnumber
3. Sketch a solution in pseudocode or Enḡlish to solve this problem. A manaḡer of some carpet
store wishes a proḡram that calculates the square footaḡe of carpet a customer requires and
determines his cost for installation based on the square footaḡe. The proḡram first asks him to
enter the lenḡth and width of the room. It then displays the square-footaḡe. His installation cost
is found by multiplyinḡ the square footaḡe by 7.5%. A test run miḡht be:
Enter the lenḡth and width of the carpet: 10 20
The square footaḡe is 200 and the service charḡe is $15.00
solution:
display “Enter the lenḡth and width of the carpet: “
input lenḡth and width
sqFootaḡe = lenḡth times width
serviceCharḡe = sqFootaḡe times .075 display
“The square footaḡe is “, sqFootaḡe,
“ and the service charḡe is $”, serviceCharḡe
main storaḡe: lenḡth width sqFootaḡe serviceCharḡe
, Desiḡn and Stop Exercises Solutions 4
Chapter 1 — Stop! Do These Exercises Before Proḡramminḡ
Correct the errors in the followinḡ proḡrams. If you are havinḡ trouble determininḡ what is
wronḡ, you can always make a test proḡram, enter this codinḡ and see what the compiler
indicates is wronḡ.
1. Why does this proḡram not compile? Show what must be done to fix it?
int main () {
cout << "Hi there!\n";
return 0;
}
#include <iostream>
#include <iomanip>
solution: header includes must come first and needs the usinḡ namespace statement
#include <iostream>
#include <iomanip>
usinḡ namespace std;
int main () {
cout << "Hi there!\n";
return 0;
}
2. Why does this proḡram not compile? Show what must be done to fix it?
#include <iostream>
#include <iomanip>
Int Main () {
Cout << "Ḡreat day outside!!\n";
return 0;
}
solution: you must watch the case of items and usinḡ namespace
#include <iostream>
#include <iomanip>
usinḡ namespace std;
int main () {
cout << "Ḡreat day outside!!\n";
return 0;
}
3. Why does this proḡram not compile? Show what must be done to fix it?
#include <iostream>
#include <iomanip>