fd fd fd fd
1
Design and Stop Exercises Solutions fd fd fd fd
to accompany the third edition of
fd fd fd fd fd
C++ for Computer Science and Engineering
fd fd fd fd fd
by Vic Broquard
fd fd
© 2001, 2002, 2003 by Vic Broquard, All rights reserved
fd fd fd fd fd fd fd fd fd
Published by Broquard eBooks 10 fd fd fd fd
3 Timberlane
fd
East Peoria, IL 61611
fd fd fd
,Design and Stop Exercises Solutions
fd fd fd fd
2
Chapter 1 — Design Exercises fd fd fd fd
1. How would you solve this problem? What is the answer? A bug wishes to climb to the top of a 1
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
2-
foot tall telephone pole. During the day, it climbs 3 feet. However, while it sleeps at night, the bug s
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
lides back down 2 feet. How many days does it take the bug to reach its objective, the top of the pol
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
e?
Answer:
day Starts Day At fd fd Height Made that Day fd fd fd
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
fd fd fd fd fd fd
2. Sketch a solution in pseudocode or English to solve this problem. A math teacher wishes to h
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
ave a program that displays the multiplication tables for her fourth graders. She wants the progr
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
am to accept any whole number (integer) from 1 to 9. The program then displays the multiplicat
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
ion tables from 1 to that number. A sample run might be as follows. Note she enters the underlin
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
ed number 4.
fd fd
Enter a number from 1 to 9: 4 fd fd fd fd fd fd fd
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:
fd
display “Enter a number from 1 to 9: “ in fd fd fd fd fd fd fd fd fd
put endnumber fd
let startnumber = 1 fd fd fd
do the following while startnumber is less than or equal to endnumber let
fd fd fd fd fd fd fd fd fd fd fd fd
currentnumber = startnumber fd fd fd
,Design and Stop Exercises Solutions
fd fd fd fd
3
do the following while currentnumber is less than or equal to endnumber
fd fd fd fd fd fd fd fd fd fd fd fd
display startnumber, ‘x’, currentnumber, ‘=’, currentnumber, ‘x’, fd fd fd fd fd fd
startnumber, ‘=’ currentnumber times startnumber fd fd fd fd fd
add 1 to currentnumber fd fd fd
end do fd
add one to startnumber fd fd fd
end do fd
main storage: startnumber endnumber currentnumber
fd fd fd fd
3. Sketch a solution in pseudocode or English to solve this problem. A manager of some carpet sto
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
re wishes a program that calculates the square footage of carpet a customer requires and determine
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
s his cost for installation based on the square footage. The program first asks him to enter the lengt
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
h and width of the room. It then displays the square-
fd fd fd fd fd fd fd fd fd fd
footage. His installation cost is found by multiplying the square footage by 7.5%. A test run might
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
be:
Enter the length and width of the carpet: 10 20 fd fd fd fd fd fd fd fd fd
The square footage is 200 and the service charge is $15.00
fd fd fd fd fd fd fd fd fd fd
solution:
display “Enter the length and width of the carpet: “ i fd fd fd fd fd fd fd fd fd fd
nput length and width fd fd fd
sqFootage = length times width serviceChar fd fd fd fd fd
ge = sqFootage times .075 display “The squa
fd fd fd fd fd fd fd
re footage is “, sqFootage, fd fd fd fd
“ and the service charge is $”, serviceCharge fd fd fd fd fd fd fd fd
main storage: length width sqFootage serviceCharge
fd fd f d f d f d
, Design and Stop Exercises Solutions
fd fd fd fd
4
Chapter 1 — Stop! Do These Exercises Before Programming
fd fd fd fd fd fd fd fd
Correct the errors in the following programs. If you are having trouble determining what is wro
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
ng, you can always make a test program, enter this coding and see what the compiler indicates
fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
is wrong.
fd
1. Why does this program not compile? Show what must be done to fix it?
fd fd fd fd fd fd fd fd fd fd fd fd fd
int main () { fd fd fd
cout << "Hi there!\n"; return fd fd fd fd fd
0;
}
#include<iostream> # d
f fd
include <iomanip> fd
solution: header includes must come first and needs the using namespace statement
fd fd fd fd fd fd fd fd fd fd fd
#include <iostream> # fd fd
include <iomanip> usi fd fd
ng namespace std; int fd fd fd fd
main () { fd fd
cout << "Hi there!\n"; return fd fd fd fd fd
0;
}
2. Why does this program not compile? Show what must be done to fix it?
fd fd fd fd fd fd fd fd fd fd fd fd fd
#include<iostream> # d
f fd
include <iomanip> Int fd fd
Main () {
fd fd fd
Cout << "Great day outside!!\n"; retur fd fd fd fd fd
n 0; fd
}
solution: you must watch the case of items and using namespace
fd fd fd fd fd fd fd fd fd fd
#include <iostream> # fd fd
include <iomanip> usi fd fd
ng namespace std; int fd fd fd fd
main () { fd fd
cout << "Great day outside!!\n"; retur fd fd fd fd fd
n 0; fd
}
3. Why does this program not compile? Show what must be done to fix it?
fd fd fd fd fd fd fd fd fd fd fd fd fd
#include<iostream> # d
f fd
include <iomanip> fd