Sunteți pe pagina 1din 14

CHAPTER 1: SOLUTIONS TO REVIEW EXERCISES

Solution R1.1: Using a computer does not require any advanced know-how or skill.
Most people are capable of turning a computer on and executing a program (such as
double clicking on the icon for Microsoft Word to bring up the word processor to type a
letter. !rogramming a computer" however" requires very specific knowledge. #ou must
know the rules of the programming language" and you must know how to construct a
program.
Solution R1.2: $oth program code and data are typically stored long term in a
computer%s secondary storage" such as a hard disk. &econdary storage is relatively
inexpensive and retains information even if the computer%s power is turned off. 'n
addition to a computer%s secondary storage" program code and data can also be stored
in a computer%s primary storage. !rimary storage consists of read-only memory (()M"
which holds programs that must always be present (such as the computer%s operating
system" and random access memory ((*M" which can hold changing data and
programs that are currently executing. (*M is relatively expensive when compared to
secondary storage" and is erased whenever the computer is turned off.
Solution R1.3: +he user of a computer receives information from the computer%s
display screen" speakers" and printers (the computer%s output devices. +he user can
input data using the computer%s keyboard" a pointing device such as a mouse" a
microphone" or a webcam (the computer%s input devices.

Solution R1.4: * simple cell phone that can only be used to make calls would be a
single-function device. 'f you can download programs (such as media players" web
browsers" and games to the cell phone and execute them" then it would be considered
a programmable computer" as it can serve multiple purposes.
Solution R1.5: )ne advantage of ,-- over machine code is that ,-- statements are
independent of the machine (computer they are being executed on. machine code
statements differ from one type of machine to the next. *nother advantage of ,-- is
that it is much more readable and understandable (by humans than machine code.
Solution R1.6: +he hello.cpp file that ' created was found in the following folder on
my computer. +he location will vary from machine to machine.
C:\Documents and Settings\Jonathan\My Documents\Visual Studio
2005\Proects\!ello\!ello
+he iostream file was found in the following folder on my computer. +he location will
vary from machine to machine.
C:\Program "iles\Microso#t Visual Studio $\VC\include
*s you can see" the location of the files on my computer shows that the computer ' use
has the Microsoft /isual &tudio development environment installed on it.
Solution R1.: +he program prints the following0
% & ' ( )2
Solution R1.!: +he program prints the following0
!ello*orld
Solution R1.": +he program prints the following0
!ello
*orld

Solution R1.1#: !rograms 1 to 2 below each have a different compile-time error (an
error that violates the rules of ,--. !rogram 3 has a run-time error (an error that will
allow the program to execute" but which fails to provide the correct results.
P$o%$&' 1 ()o'*il+,ti'+ +$$o$ - no +n. /uot+ on 0t$in%1:
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2!ello3 *orld4 ,, endl.
return 0.
5
P$o%$&' 2 ()o'*il+,ti'+ +$$o$ - no 0+'i)olon on t2+ )out 0t&t+'+nt1:
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2!ello3 *orld42 ,, endl
return 0.
5
P$o%$&' 3 ()o'*il+,ti'+ +$$o$ - no *&$+nt2+0+0 3o$ '&in 3un)tion1:
+include ,iostream-
using namespace std.
int main
1
cout ,, 2!ello3 *orld42 ,, endl.
return 0.
5
P$o%$&' 4 ($un,ti'+ +$$o$ - 4Wo$l.5 i0 'i00*+ll+.1:
+include ,iostream-
using namespace std.
int main
1
cout ,, 2!ello3 *olrd42 ,, endl.
return 0.
5
Solution R1.11: * compile-time error is typically found by the compiler during the
compilation process. * compile-time error is caused when the source code violates the
rules of the programming language being used.
* run-time error cannot be found by the compiler. 't is found by testing the program and
carefully examining the output or results for errors.
Solution R1.12: 4xample algorithm to determine number of years until account
depletion0
1. Repeat the following while account is greater than $0:
A. Set account_value equal to account_value times 1.005.
B. e!uct 500 from account_value.
". #ncrement num$er of months $% 1.
&. 'rint the total num$er of months !ivi!e! $% 1& to !etermine how
man% %ears till account was !eplete!.
+he account is depleted after 55 months" or 1.62222 years.
Solution R1.13: 'f the user was allowed to enter the inputs into the algorithm written in
&olution (.1.15 above" there are a few number combinations that may cause problems.
7or example" if the amount of interest added each month is greater than the amount
withdrawn each month" then the algorithm will never terminate. We can fix this by
adding some simple changes to the algorithm0
1. #f the account_value times the interest rate is greater than or equal to
the with!raw_amount( terminate) otherwise( repeat the following while
account is greater than $0:
A. Set account_value equal to account_value times *1 + interest_rate,.
B. e!uct with!raw_amount from account value.
". #ncrement num$er of months $% 1.
&. 'rint the total num$er of months !ivi!e! $% 1& to !etermine how man%
%ears till account was !eplete!.
We would have a similar problem if the user entered a negative value for the
withdraw8amount" which would have the effect of increasing the account instead of
decreasing the account. 7or this solution" we will assume they will not enter a negative
amount.
Solution R1.14: 4xample algorithm to determine the exterior surface area of a house0
1. "alculate the wall_area of house $% using the formula:
wall_area - *& . height . length, + *& . height . wi!th,
&. "alculate the !oor_area $% using the formula:
!oor_area - num_!oors . !oor_height . !oor_wi!th
/. "alculate the win!ow_area $% using the formula:
win!ow_area - num_win . win_height . win_wi!th
0. "alculate the total_area $% using the formula:
total_area - wall_area 1 !oor_area 1 win!ow_area
Solution R1.15: 4xample algorithm to determine cheapest method of getting to work
(9mpg: stands for miles per gallon0
1. "alculate the num$er of gallons of gas use! $% using the formula:
gal_use! - !istance 2 mpg
&. "alculate the cost of the gas use!:
cost_of_gas - gal_use! . $0
/. "alculate total cost of !riving inclu!ing maintenance:
total_cost - cost_of_gas + *!istance . 0.05,
0. #f total_cost is greater than cost of tic3et( then print 45a3ing the train
is cheaper(6 otherwise print 4riving is as cheap or cheaper than the
train.6
Solution R1.16: 4xample algorithm to determine what percent of car use is for personal
reasons versus for commuting to work. 7or this algorithm" we will assume that you drive
to and from work once each work day.
1. "alculate the total miles !riven !uring perio!:
total_miles - en!_o!om 1 $egin_o!om
&. "alculate the average num$er of wor3_miles for the same perio!:
wor3_miles - num_wor3_!a%s . *& . !ist_to_wor3,
/. "alculate the percent of wor3 !riving versus personal !riving:
percent_wor3 - wor3_miles 2 total_miles . 100
percent_personal - 100 1 percent_wor3
Solution R1.1: Unlike a human" a computer cannot make a decision based on instinct
or emotion. 't must use a formula with valid input data to calculate a value to be used as
a decision point. * computer must be told exactly which values to use in a formula.
Without making assumptions about the price of gas and the annual usage" the computer
program cannot calculate the overall cost of each car" and would not have anything on
which to base a decision.
Solution R1.1!: 4xample algorithm to calculate the approximate value of pi0
1. Set num - 1. Set start - /.
&. Repeat the following until num is compute! to si7 significant !igits
A. Set num - num 1 *12start, + *12*start+&,,
B. Set start - start + 0
/. 'rint the result of num times 0 *to get the appro7imate value of pi,

Solution R1.1": 4xample instructions (algorithm for little brother backing up files0
1. 8nce each !a%( at mi!night( perform the following steps:
A. 5urn on computer
B. #nsert a $lan3 " into the !is3 !rive
". "op% all files from c:9stu!ent fol!er to the "
. Burn the "
:. ;iew the contents of the ".
<. :=ect the " an! la$el it
>. 5urn off the computer
?. Rewar! %ourself with a coo3ie
Solution R1.2#: 4xample algorithm that yields the fare for traveling a given distance in
a given time" assuming that the taxi moves at a constant speed0
We have two inputs0
travel distance (in miles
travel time (in minutes
travel spee! - travel !istance 2 travel time
if travel spee! @- 0.&
total cost - /.10 + 0.05 . *5 . travel !istance A 1,
else
total cost - /.10 + 0.05 . *travel time A 1,
Solution R1.21: 4xample algorithm for computing the time required to travel a given
distance0
We have three inputs0
time to accelerate to ;< mph
travel speed
distance
'n phase 1" the car accelerates until it reaches the travel speed.
'n phase 5" it travels at that speed. =ere is the algorithm for the total time0
acceleration - B0 2 time to accelerate to B0 mph
phase1 time - travel spee! 2 acceleration
phase1 !istance - 0.5 . acceleration . *phase1 time,
&
phase& time - *!istance A phase1 !istance, 2 travel spee!
total time - phase1 time + phase& time
Su**l+'+nt&l En%in++$in% E6+$)i0+ Solution0
Solution ER1.22: *lgorithm to compute the taxi tire radius0
*t the proper radius (" one revolution travels a distance of , > 5 ? (" and the @ mile taxi
ride takes @ A , > @ A (5 ? ( revolutions (assuming for the moment that ( is measured
in miles.
&imilarly" at the inflated radius r" a # mile taxi ride takes # A (5 ? r revolutions
+he meter counts revolutions" and we want the two counts to be equal0
@ A (5 ? ( > # A (5 ? r.
&olving for r yields
r > ( # A @
7ortunately" the units for @ and # cancel each other out" so we donBt have to worry
whether @ and ( are entered in the same unit. +he output r will simply be in the same
unit as (.
'n the U.&." trip distances are usually measured in miles" and tire diameters in inches" so
weBll use those units in the prompts.
=ere is the pseudocode0
'rompt the user to enter the actual !istance *in miles,
C - user input
'rompt the user to enter the !esire! !istance *in miles,
D - user input
'rompt the user to enter the actual ra!ius *in inches,
R - user input
r - R D 2 C
!eflation - R A r
'rint Eeflate the tires $% E
'rint !eflation
'rint E inchesE
CHAPTER 1: SOLUTIONS TO PRO7RA88IN7 EXERCISES
Solution P1.1: 4xample program to print 9Connichiwa:" which is 9Dood afternoon: in
Eapanese.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 26onnichi7a.2 ,, endl.
return 0.
5
Solution P1.2: 4xample program using user input.
+include ,iostream-
+include ,string-
using namespace std.
int main/0
1
cout ,, 2!ello3 my name is !al42 ,, endl.
cout ,, 2*hat 7ould you li8e me to do92 ,, endl.
string user:input.
getline/cin3 user:input0.
cout ,, 2;<m sorry3 ; cannot do that.2 ,, endl.
return 0.
5
Solution P1.3: 4xample program using user input.
+include ,iostream-
+include ,string-
using namespace std.
int main/0
1
cout ,, 2!ello3 my name is !al42 ,, endl.
cout ,, 2*hat is your name92 ,, endl.
string user:input.
getline/cin3 user:input0.
cout ,, 2!ello3 2 ,, user:input
,, 2. ; am glad to meet you42 ,, endl.
return 0.
5
Solution P1.4: 4xample program to print sum of the first 1< positive integers.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2=he sum o# the #irst ten positi>e integers is 2
,, ? @ 2 @ A @ ) @ 5 @ % @ ' @ $ @ B @ ?0 ,, endl.
return 0.
5
Solution P1.5: 4xample program to print the product of the first 1< positive integers.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2=he product o# the #irst ten positi>e integers is 2
,, ? & 2 & A & ) & 5 & % & ' & $ & B & ?0 ,, endl.
return 0.
5
Solution P1.6: 4xample program to calculate the interest on an amount for the first
three years. (+he account starting balance may vary.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2=he starting Calance is D?03000.2 ,, endl.
cout ,, 2=he interest rate is 5E.2 ,, endl.
cout ,, 2=he Calance a#ter one year is: 2
,, ?0000 & ?.05 ,, endl.
cout ,, 2=he Calance a#ter t7o years is: 2
,, /?0000 & ?.050 & ?.05 ,, endl.
cout ,, 2=he Calance a#ter three years is: 2
,, ///?0000 & ?.050 & ?.050 & ?.050 ,, endl.
return 0.
5
Solution P1.: 4xample program to print a name in a box.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2 @FFFFFFFFFF@2 ,, endl.
cout ,, 2 G Jonathan G2 ,, endl.
cout ,, 2 @FFFFFFFFFF@2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.!: 4xample program to print a name in large letters.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2 && && &&& & & 2 ,, endl.
cout ,, 2 & & & & & & & & 2 ,, endl.
cout ,, 2 & && & &&&&&&& & & 2 ,, endl.
cout ,, 2 & & & & & & 2 ,, endl.
cout ,, 2 & & & & & & 2 ,, endl.
cout ,, 2 & & & & & & 2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.": 4xample program to print a face. +he program may look a little different
than the actual face that prints out. (emember" in order to print a \ character (which
normally signals the start of an escape sequence" you have to use two \\ in a row to
print one to the screen.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2 \\\\\\HHH 2 ,, endl.
cout ,, 2 H \\ 2 ,, endl.
cout ,, 2 H II II \\ 2 ,, endl.
cout ,, 2 1 & & 5 2 ,, endl.
cout ,, 2 \\ - H 2 ,, endl.
cout ,, 2 \\ J@@K H 2 ,, endl.
cout ,, 2 \\::::H 2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.1#: 4xample program to print a picture of a house. Use the escape
sequence \\ to print a single \ character.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2 H\\ 2 ,, endl.
cout ,, 2 H \\ 2 ,, endl.
cout ,, 2@FFFF@ 2 ,, endl.
cout ,, 2G .F.G 2 ,, endl.
cout ,, 2G G GG 2 ,, endl.
cout ,, 2@F@F@@ 2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.11: 4xample program to print a picture of an animal (it%s supposed to be a
turtle. Use the escape sequence \\ to print a single \ character.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2 @FFFFF@ @FF@ FFFFFFFF 2 ,, endl.
cout ,, 2 H \\ H & \\ H !ey \\2 ,, endl.
cout ,, 2 \\ H \\ HFFH , there3 G2 ,, endl.
cout ,, 2 @FFFFFFFFFF@ H \\ !uman4 H 2 ,, endl.
cout ,, 2 G G G G FFFFFFFF 2 ,, endl.
cout ,, 2 @F@ @F@ 2 ,, endl.
cout ,, 2 2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.12: 4xample program to print the names of my three best friends. #ou can
print the names in one long cout statement" such as this one" or print each one using a
separate cout statement.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2Margy2 ,, endl
,, 2MaL7ell2 ,, endl
,, 2MCi the Dog2 ,, endl.
return 0.
5
Solution P1.13: 4xample program to print out my favorite poem.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2Noses are red32 ,, endl
,, 2Violets are Clue.2 ,, endl
,, 2; lo>e C@@32 ,, endl
,, 2and ; 8no7 you do3 too42 ,, endl.
return 0.
5
Solution P1.14: 4xample program to print an imitation !iet Mondrian painting.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2@FFF@FFFFFF@FFFFFFFFFFF@FFF@2 ,, endl.
cout ,, 2G G G G G2 ,, endl.
cout ,, 2G G G @FFF@2 ,, endl.
cout ,, 2@FFF@FFFFFF@ G:::G2 ,, endl.
cout ,, 2G G&&&&&&@FFF@FFFFFFF@:::G2 ,, endl.
cout ,, 2G G&&&&&&GOOOG G:::G2 ,, endl.
cout ,, 2@FFF@&&&&&&@FFF@ @FFF@2 ,, endl.
cout ,, 2G G&&&&&&G G G G2 ,, endl.
cout ,, 2@FFF@FFFFFF@FFF@FFFFFFF@FFF@2 ,, endl.
cout ,, endl.
return 0.
5
Solution P1.15: 4xample program to print the United &tates flag (perhaps a bit out of
proportion" but with an accurate number of stars and stripes.
+include ,iostream-
using namespace std.
int main/0
1
cout ,, endl.
cout ,, 2@FFFFFFFFFFFF@FFFFFFFFFFFFFFFFFFFFFFFF@2 ,, endl.
cout ,, 2G& & & & & & G((((((((((((((((((((((((G2 ,, endl.
cout ,, 2G & & & & & G G2 ,, endl.
cout ,, 2G& & & & & & G((((((((((((((((((((((((G2 ,, endl.
cout ,, 2G & & & & & G G2 ,, endl.
cout ,, 2G& & & & & & G((((((((((((((((((((((((G2 ,, endl.
cout ,, 2G & & & & & G G2 ,, endl.
cout ,, 2G& & & & & & G((((((((((((((((((((((((G2 ,, endl.
cout ,, 2G & & & & & G G2 ,, endl.
cout ,, 2G& & & & & & G((((((((((((((((((((((((G2 ,, endl.
cout ,, 2@FFFFFFFFFFFF@ G2 ,, endl.
cout ,, 2G(((((((((((((((((((((((((((((((((((((G2 ,, endl.
cout ,, 2G G2 ,, endl.
cout ,, 2G(((((((((((((((((((((((((((((((((((((G2 ,, endl.
cout ,, 2@FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF@2 ,, endl.
cout ,, endl.
return 0.
5
Solution EP1.16: !rogram to print chart of atmospheric gases0
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2=ype o# planet Pame o# planet Primary gas Secondary gas2
,, endl.
cout ,, 2=errestrial Venus CM2 P22 ,, endl.
cout ,, 2=errestrial Qarth P2 M22 ,, endl.
cout ,, 2=errestrial Mars CM2 P22 ,, endl.
cout ,, 2Ras giant Jupiter !2 !e2 ,, endl.
cout ,, 2Ras giant Saturn !2 !e2 ,, endl.
cout ,, 2Ras giant Sranus !2 !e2 ,, endl.
cout ,, 2Ras giant Peptune !2 !e2 ,, endl.
return 0.
5
Solution EP1.1: !rogram to print circuit image0
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2 :::H\\ H\\ H\\ :::::::H\\ H\\ H\\ ::2 ,, endl.
cout ,, 2 G \\H \\H \\H G \\H \\H \\H G2 ,, endl.
cout ,, 2 G 58Mhm G %8Mhm G2 ,, endl.
cout ,, 2 G \\ \\2 ,, endl.
cout ,, 2 FFF H H2 ,, endl.
cout ,, 2 ?2V G @ G ?08Mhm \\ )8Mhm \\2 ,, endl.
cout ,, 2 G F G H H2 ,, endl.
cout ,, 2 FFF \\ \\2 ,, endl.
cout ,, 2 G H H2 ,, endl.
cout ,, 2 G G G2 ,, endl.
cout ,, 2 G G G2 ,, endl.
cout ,, 2 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2 ,, endl.
return 0.
5
Su**l+'+nt&l En%in++$in% E6+$)i0+ Solution0
Solution EP1.1!: !rogram to print metric conversion0
+include ,iostream-
using namespace std.
int main/0
1
cout ,, 2? 8ilogram ( 2.2? pounds2 ,, endl.
cout ,, 2? pound ( 0.)5) 8ilograms2 ,, endl.
cout ,, 2? #oot ( 0.A05 meters2 ,, endl.
cout ,, 2? meter ( A.2$ #eet2 ,, endl.
cout ,, 2? mile ( ?.%? 8ilometers2 ,, endl.
cout ,, 2? 8ilometer ( 0.%2? miles2 ,, endl.
return 0.
5

S-ar putea să vă placă și