Programming Logic and Design, 6e
Solutions 5-1
Programming Logic and Design, 6th Edition Chapter 5 Exercises 1.
What What is outp output ut by by eac each h of the the pseu pseudo doco code de segm segmen ents ts in igu igure re 5-!! 5-!!""
Answer:
!.
a. b. c. d.
5, 22, 5 4, 6, 7 5, 6 Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye
e.
Hello Hello Hello
f.
Adios Adios Adios Adios Adios Adios Adios Adios
Desi Design gn the the log logic ic for for a pro progr gram am tha thatt out outpu puts ts e#er e#ery y num number ber from from 1 thr throu ough gh 1$. 1$.
Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num number housekeeping( number ! " #hile number $! "% detail&oop( end#hile 'inishp( stop housekeeping( number ! " return detail&oop( output number number ! number ) " return 'inishp( output *+nd o' program return
Solutions 5-!
Programming Logic and Design, 6e
Pseudocode: start Declarations num number housekeeping( number ! " #hile number $! "% detail&oop( end#hile 'inishp( stop housekeeping( number ! " return detail&oop( output number number ! number ) " return 'inishp( output *+nd o' program return
Solutions 5-!
Programming Logic and Design, 6e
'.
Solutions 5-'
Desi Design gn the the log logic ic for for a pro progr gram am tha thatt out outpu puts ts e#er e#ery y num number ber from from 1 thr throu ough gh 1$ 1$ along &ith its s(uare and cube.
Answer:
% sample solution follo&s Flowchart:
Pseudocode: start Declarations num number num s-uare num cube housekeeping( #hile number $! "% detail&oop( end#hile 'inishp( stop
Programming Logic and Design, 6e
Solutions 5-)
housekeeping( number ! " return detail&oop( s-uare ! number . number cube ! s-uare . number output number, s-uare, cube number ! number ) " return 'inishp( output *+nd o' program return
).Design the logic for a program that outputs e#ery e#en number from ! through '$. Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num number housekeeping( #hile number $! /% detail&oop( end#hile 'inishp( stop housekeeping( number ! 2 return detail&oop( output number number ! number ) 2 return 'inishp( output *+nd o' program
Solutions 5-5
Programming Logic and Design, 6e
Solutions 5-6
return
5.
Design the logic for a program that outputs numbers in re#erse order from 1$ do&n to 1.
Answer:
% sample solution follo&s Flowchart:
Pseudocode: start Declarations num number housekeeping( #hile number 0! " detail&oop( end#hile
Programming Logic and Design, 6e
Solutions 5-*
'inishp( stop housekeeping( number ! "% return detail&oop( output number number ! number 1 " return 'inishp( output *+nd o' program return
6. a. +he o nterest redit ompany pro#ides /ero-interest loans to customers. Design an application that gets customer account data, including an account number, customer name, and balance due. 0utput the account number and name then output the customer2s pro3ected balance each month for the ne4t 1$ months. %ssume that there is no finance charge on this account, that the customer maes no ne& purchases, and that the customer pays off the balance &ith e(ual monthly payments, &hich are 1$ percent of the original bill. Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Solutions 5-
Pseudocode: start Declarations num accountum num balanceDue num month num paymentAmt string custame num 3AA+ ! %8"% string 39:3" ! *+nter an account number string 39:32 ! *+nter the customer name and balance due housekeeping( detail( 'inishp( stop housekeeping( output 39:3"
Programming Logic and Design, 6e
Solutions 5-7
input accountum return detail( output 39:32 input custame, balanceDue output accountum, custame month ! " paymentAmt ! balanceDue . 3AA+ #hile month $! "% balanceDue ! balanceDue ; paymentAmt output month, balanceDue month ! month ) " end#hile output 39:3" input accountum return 'inishp( output *+nd o' program return
b. 8odify the o nterest redit ompany application so it e4ecutes continuously for any number of customers until a sentinel #alue is supplied for the account number. Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Solutions 5-1$
Pseudocode: start Declarations num accountum num balanceDue num month num paymentAmt string custame num 3AA+ ! %8"% string 39:3" ! *+nter an account number or <<< to e=it string 39:32 ! *+nter the customer name and balance due housekeeping( #hile accountum not e-ual to <<< detail&oop( end#hile 'inishp( stop
Programming Logic and Design, 6e
Solutions 5-11
housekeeping( output 39:3" input accountum return detail&oop( output 39:32 input custame, balanceDue output accountum, custame month ! " paymentAmt ! balanceDue . 3AA+ #hile month $! "% balanceDue ! balanceDue ; paymentAmt output month, balanceDue month ! month ) " end#hile output 39:3" input accountum return 'inishp( output *+nd o' program return
*. a. +he Some nterest redit ompany pro#ides loans to customers at 1.5 percent interest per month. Design an application that gets customer account data, including an account number, customer name, and balance due. 0utput the account number and name then output the customer2s pro3ected balance each month for the ne4t 1$ months. %ssume that &hen the balance reaches 91$ or less, the customer can pay off the account. %t the beginning of e#ery month, 1.5 percent interest is added to the balance, and then the customer maes a payment e(ual to 5 percent of the current balance. %ssume the customer maes no ne& purchases. Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num accountum num balanceDue num month num paymentAmt string custame
Solutions 5-1!
Programming Logic and Design, 6e
Solutions 5-1'
num 3AA+ ! %8%5 num >++?A+ ! %8%"5 string 39:3" ! *+nter an account number string 39:32 ! *+nter the customer name and balance due housekeeping( detail( 'inishp( stop housekeeping( output 39:3" input accountum return detail( output 39:32 input custame, balanceDue output accountum, custame month ! " #hile month $! "% balanceDue ! balanceDue . (" ) >++?A+ i' balanceDue 0 "% then paymentAmt ! balanceDue . 3AA+ balanceDue ! balanceDue ; paymentAmt output month, balanceDue month ! month ) " else balanceDue ! % output month, balanceDue month ! "" endi' end#hile output 39:3" input accountum return 'inishp( output *+nd o' program return
b. 8odify the Some nterest redit ompany application so it e4ecutes continuously for any number of customers until a sentinel #alue is supplied for the account number. Answer:
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations
Solutions 5-1)
Programming Logic and Design, 6e
Solutions 5-15
num accountum num balanceDue num month num paymentAmt string custame num 3AA+ ! %8%5 num >++?A+ ! %8%"5 string 39:3" ! *+nter an account number or <<< to e=it string 39:32 ! *+nter the customer name and balance due housekeeping( #hile accountum not e-ual to <<< detail&oop( end#hile 'inishp( stop housekeeping( output 39:3" input accountum return detail&oop( output 39:32 input custame, balanceDue output accountum, custame month ! " #hile month $! "% balanceDue ! balanceDue . (" ) >++?A+ i' balanceDue 0 "% then paymentAmt ! balanceDue . 3AA+ balanceDue ! balanceDue ; paymentAmt output month, balanceDue month ! month ) " else balanceDue ! % output month, balanceDue month ! "" endi' end#hile output 39:3" input accountum return 'inishp( output *+nd o' program return
. Secondhand :ose :esale Shop is ha#ing a se#en-day sale during &hich the price of any unsold item drops 1$ percent each day. or e4ample, an item that costs 91$.$$ on the first day costs 1$ percent less, or 97.$$, on the second day. 0n the third day, the same item is 1$ percent less than 97.$$, or 9.1$. Design an application that allo&s a user to input a price until an appropriate sentinel #alue is entered. 0utput is the price of e#ery item on each day, one through se#en.
Programming Logic and Design, 6e
Solutions 5-16
Answer:
% sample solution follo&s Flowchart:
Pseudocode: start Declarations num itemumber num price num day string description num +: ! 7 num +D@>9 ! %8"% string 39:3" ! *+nter an item number or <<< to e=it string 39:32 ! *+nter the description and price housekeeping( #hile itemumber not e-ual to <<< detail&oop(
Programming Logic and Design, 6e
Solutions 5-1*
end#hile 'inishp( stop housekeeping( output 39:3" input itemumber return detail&oop( output 39:32 input description, price day ! % #hile day $ +: day ! day ) " output day, price price ! price . (" ; +D@>9 end#hile output 39:3" input itemumber return 'inishp( output *+nd o' program return
7. +he ;o&ell
% sample solution follo&s Flowchart:
Programming Logic and Design, 6e
Solutions 5-1
Pseudocode: start Declarations num accountum num balance num year string 'irstame string lastame num >++?A+ ! %8%4 num +: ! 2% string 39:3" ! *+nter an account number or <<< to e=it string 39:32 ! *+nter the 'irst name, last name, and balance housekeeping #hile accountum does not e-ual <<< detail&oop( end#hile 'inishp( stop housekeeping( output 39:3"
Programming Logic and Design, 6e
Solutions 5-17
input accountum return detail&oop( output 39:32 input 'irstame, lastame, balance output accountum, 'irstame, lastame year ! % #hile year $ +: balance ! balance . (" ) >++?A+ year ! year ) " output year, balance end#hile output 39:3" input accountum return 'inishp( output *+nd o' program return
1$. 8r. :oper o&ns !$ apartment buildings. =ach building contains 15 units that he rents for 9$$ per month each. Design the application that &ould print 1! payment coupons for each of the 15 apartments in each of the !$ buildings. =ach coupon should contain the building number >1 through !$?, the apartment number >1 through 15?, the month >1 through 1!?, and the amount of rent due. Answer:
% sample solution follo&s
Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num buildingum num aptum num month num 9A&>&D? ! 2% num 9A&>? ! "5 num 9A&:9H? ! "2 num + ! B%% string @939H+AD>G ! *3ayment @oupon housekeeping( #hile buildingum $! 9A&>&D?
Solutions 5-!$
Programming Logic and Design, 6e
Solutions 5-!1
detail&oop( end#hile 'inishp( stop housekeeping( buildingum ! " return detail&oop( aptum ! " #hile aptum $! 9A&>? month ! " #hile month $! 9A&:9H? output @939H+AD>G output *uilding umberC *, buildingum output *Apartment umberC *, aptum output *:onthC *, month output *ent DueC *, + month ! month ) " end#hile aptum ! aptum ) " end#hile buildingum ! buildingum ) " return 'inishp( output *+nd o' program return
11. a. Design a program for the ;olly&ood 8o#ie :ating @uide, in &hich users continuously enter a #alue from $ to ) that indicates the number of stars they are a&arding to the @uide2s featured mo#ie of the &ee. +he program e4ecutes continuously until a user enters a negati#e number to (uit. f a user enters a star #alue that does not fall in the correct range, reprompt the user continuously until a correct #alue is entered. %t the end of the program, display the a#erage star rating for the mo#ie. Answer:
% sample solution follo&s
Flowchart:
Programming Logic and Design, 6e
Solutions 5-!!
Pseudocode: start Declarations num num9'?tars num count ! % num total ! % num ag string 39:3 ! *+nter the star rating or a negatie number to -uit housekeeping( #hile num9'?tars 0! % detail&oop( end#hile 'inishp( stop housekeeping( output 39:3 input num9'?tars return
Programming Logic and Design, 6e
Solutions 5-!'
detail&oop( #hile num9'?tars 0 4 9 num9'?tars $ % output *3lease enter a alue 'rom % to 4 input num9'?tars end#hile count ! count ) " total ! total ) num9'?tars output 39:3 input num9'?tars return 'inishp( ag ! total E count output *he aerage star rating isC , ag output *+nd o' program return
b. 8odify the mo#ie-rating program so that a user gets three tries to enter a #alid rating. %fter three incorrect entries, the program issues an appropriate message and continues &ith a ne& user. Answer:
% sample solution follo&s
Flowchart:
Programming Logic and Design, 6e
Solutions 5-!)
Pseudocode: start Declarations num num9'?tars num count ! % num total ! % num attempts num ag num &>:> ! / string 39:3 ! *+nter the star rating or a negatie number to -uit
Programming Logic and Design, 6e
Solutions 5-!5
housekeeping( #hile num9'?tars 0! % AD attempts $ &>:>? detail&oop( end#hile 'inishp( stop housekeeping( output 39:3 input num9'?tars attempts ! % return detail&oop( #hile (num9'?tars 0 4 9 num9'?tars $ % AD attempts $ &>:> output *3lease enter a alue 'rom % to 4 input num9'?tars attempts ! attempts ) " end#hile i' attempts $ &>:> then count ! count ) " total ! total ) num9'?tars output 39:3 input num9'?tars else output *ou hae 'ailed to enter a alid rating8 output *he program #ill no# end8 endi' return 'inishp( ag ! total E count output *he aerage star rating isC , ag output *+nd o' program return
c. 8odify the mo#ie-rating program so that the user is prompted continuously for a mo#ie title until ABBBBBC is entered. +hen, for each mo#ie, continuously accept starrating #alues until a negati#e number is entered. Display the a#erage rating for each mo#ie. Answer:
% sample solution follo&s
Flowchart:
Programming Logic and Design, 6e
Solutions 5-!6
Programming Logic and Design, 6e
Solutions 5-!*
Pseudocode: start Declarations string moieitle num num9'?tars num count ! % num total ! % num attempts num ag num &>:> ! / string ?A39:3 ! *+nter a star rating or a negatie number 'or a ne# moie string :9F>+39:3 ! *+nter a moie title or to -uit housekeeping( #hile moieitle not e-ual to * AD attempts $ &>:>? detail&oop( end#hile 'inishp( stop housekeeping( output :9F>+39:3 input moieitle attempts ! % return detail&oop( output ?A39:3 input num9'?tars #hile num9'?tars 0! % AD attempts $ &>:> get?tarating( end#hile calcAerage( i' attempts $ &>:> then output :9F>+39:3 input moieitle attempts ! % endi' return get?tarating( #hile (num9'?tars 0 4 9 num9'?tars $ % AD attempts $ &>:> output *3lease enter a alue 'rom % to 4 input num9'?tars attempts ! attempts ) " end#hile i' attempts $ &>:> then count ! count ) " total ! total ) num9'?tars output 39:3 input num9'?tars else output *ou hae 'ailed to enter a alid rating8 output *he program #ill no# end8
Programming Logic and Design, 6e
Solutions 5-!
endi' return calcAerage( ag ! total E count output *he aerage star rating 'or , moieitle, * isC , ag return 'inishp( output *+nd o' program return
1!. +he af oir offee Shop &ants some maret research on its customers. When a customer places an order, a cler ass for the customer2s /ip code and age. +he cler enters that data as &ell as the number of items the customer orders. +he program operates continuously until the cler enters a $ for /ip code at the end of the day. When the cler enters an in#alid /ip code >more than 5 digits? or an in#alid age >defined as less than 1$ or more than 11$?, the program reprompts the cler continuously. When the cler enters fe&er than 1 or more than 1! items, the program reprompts the cler t&o more times. f the cler enters a high #alue on the third attempt, the program accepts the high #alue, but if the cler enters a negati#e #alue on the third attempt, an error message is displayed and the order is not counted. %t the end of the program, display a count of the number of items ordered by customers from the same /ip code as the coffee shop >5)7)?, and a count from other /ip codes. %lso display the a#erage customer age, as &ell as counts of the number of items ordered by customers under '$ and by customers '$ and older. Answer:
% sample solution follo&s
Pseudocode: start Declarations num custip num custAge num num>tems ! % num alidum>tems num count9'9rders ! % num total@ustAge ! % num num>temsnder/% ! % num num>tems/%And9lder ! % num num>tems>n?toreip ! % num num>tems>n9therips ! % num ?9+>3 ! 54339:3 ! *+nter the ip code string AG+39:3 ! *+nter the customerIs age string >+:39:3 ! *+nter the number o' items housekeeping( #hile custip not e-ual to % detail&oop( end#hile
Programming Logic and Design, 6e
Solutions 5-!7
'inishp( stop alidateip@ode( #hile custip 0 <<<<< output *3lease enter a alid ip code input custip end#hile return alidate@ustAge( #hile custAge $ "% 9 custAge 0 ""% output *3lease enter a alid customer age input custAge end#hile return alidateum>tems( Declarations num attempts ! % num &>:> ! 2 alidum>tems ! " #hile (num>tems $ " 9 num>tems 0 "2 AD attempts $ &>:> output *3lease reenter the number o' items input num>tems attempts ! attempts ) " end#hile i' attempts ! 2 AD num>tems $ % then output *An inalid number o' items #as entered output *his order #ill not be counted alidum>tems ! % endi' return housekeeping( output >339:3 input custip return detail&oop( output AG+39:3 input custAge output >+:39:3 input num>tems alidateip@ode( alidate@ustAge( alidateum>tems( count9'9rders ! count9'9rders ) " total@ustAge ! total@ustAge ) custAge i' custip ! ?9+>3 num>tems>n?toreip ! num>tems>n?toreip ) num>tems else num>tems>n9therips ! num>tems>n9therips ) num>tems endi'
Programming Logic and Design, 6e
Solutions 5-'$
i' custAge $ /% then num>temsnder/% ! num>temsnder/% ) num>tems else num>tems/%And9lder ! num>tems/%And9lder ) num>tems endi' output >339:3 input custip return 'inishp( Declarations ag@ustAge i' count9'9rders 0 % then output *otal items ordered in the , ?9+>3, * ip code ! , num>tems>n?toreip output *otal items ordered in other ip codes ! , num>tems>n9therips ag@ustAge ! total@ustAge E count9'9rders output *he aerage customer age isC , ag@ustAge output num>temsnder/%, * items #ere ordered by customers under /% output num>tems/%And9lder, * items #ere ordered by customers /% and older endi' output *+nd o' program return
Flowchart: +he flo&chart2s structure &ill be #ery similar to that gi#en for pre#ious e4ercises.
Find the Bugs 1'. Eour student dis contains files named D=GG?. ollo&ing the comments, each file contains pseudocode that has one or more bugs you must find and correct. Answer H
Please see the D=
Game one 1). n hapter !, you learned that in many programming languages you can generate a random number bet&een 1 and a limiting #alue named &>:> by using a statement similar to randomumber ! random(&>:>. n hapter ), you created the logic for a guessing game in &hich the application generates a random number and the
Programming Logic and Design, 6e
Solutions 5-'1
player tries to guess it. o&, create the guessing game itself. %fter each guess, display a message indicating &hether the player2s guess &as correct, too high, or too lo&. When the player e#entually guesses the correct number, display a count of the number of guesses that &ere re(uired. Answer H
% sample solution is as follo&sH Flowchart:
Pseudocode: start Declarations num myandomumber num guess num count num &>:> string 39:3 ! *+nter a number bet#een " and * string @9+@ ! *ou guessed correctlyJ string H>GH ! *ou guessed too highJ
Programming Logic and Design, 6e
Solutions 5-'!
string &9K ! *ou guessed too lo#J housekeeping( #hile guess not e-ual to myandomumber detail&oop( end#hile 'inishp( stop housekeeping( myandomumber ! random(&>:> output 39:3, &>:> input guess count ! " return detail&oop( i' guess 0 myandomumber then output H>GH else output &9K endi' input guess count ! count ) " return 'inishp( output @9+@ output *>t took , count, * guessesJ output *+nd o' program return
15. reate the logic for a game that simulates rolling t&o dice by generating t&o numbers bet&een 1 and 6 inclusi#e. +he player chooses a number bet&een ! and 1! >the lo&est and highest totals possible for t&o dice?. +he player then ArollsC t&o dice up to three times. f the number chosen by the user comes up, the user &ins and the game ends. f the number does not come up &ithin three rolls, the computer &ins. Answer H
% sample solution is as follo&sH Flowchart:
Programming Logic and Design, 6e
Pseudocode: start Declarations num dice" num dice2
Solutions 5-''
Programming Logic and Design, 6e
Solutions 5-')
num guess num count num #on num &>:> ! 6 string 39:3 ! *@hoose a number bet#een " and "2* string K> ! *ou #inJ string &9?+ ! *ou loseJ housekeeping( #hile count $ / AD #on ! % detail&oop( end#hile 'inishp( stop housekeeping( count ! % #on ! % dice" ! random(&>:> dice2 ! random(&>:> output 39:3 input guess return detail&oop( count ! count ) " i' guess ! dice" ) dice2 then output K> #on ! " endi' dice" ! random(&>:> dice2 ! random(&>:> return 'inishp( i' count ! / AD #on ! % then output &9?+ endi' output *+nd o' program return
16. reate the logic for the dice game Pig, in &hich a player can compete &ith the computer. +he ob3ect of the game is to be the first to score 1$$ points. +he user and computer tae turns rolling a pair of dice follo&ing these rulesH •
•
•
0n a turn, each player rolls t&o dice. f no 1 appears, the dice #alues are added to a running total for the turn, and the player can choose &hether to roll again or pass the turn to the other player. f a 1 appears on one of the dice, the player2s turn total becomes $ in other &ords, nothing more is added to the player2s game total for that turn, and it becomes the other player2s turn. f a 1 appears on both of the dice, not only is the player2s turn o#er, but the player2s entire accumulated total is reset to $.
Programming Logic and Design, 6e
•
Solutions 5-'5
When the computer does not roll a 1 and can choose &hether to roll again, generate a random #alue of 1 or !. +he computer &ill then decide to continue &hen the #alue is 1 and decide to (uit and pass the turn to the player &hen the #alue is not 1.
Answer H
% sample solution is as follo&sH Pseudocode: start Declarations num dice" num dice2 num user@ount num computer@ount num userurn num &>:> ! 2 num K>: !"%% housekeeping( #hile computer@ount $ K>: AD user@ount $ K>: detail&oop( end#hile stop housekeeping( output he 'irst to "%% #ins the game o' 3ig user@ount ! % computer@ount ! % userurn ! " return detail&oop( i' userurn ! " then output *serIs turn to roll the dice else output *@omputerIs turn to roll the dice endi' dice" ! random(&>:> dice2 ! random(&>:> i' userurn ! " then i' dice" ! " 9 dice2 ! " then output *@omputerLs turn userurn ! % i' dice" ! " AD dice2 ! " then user@ount ! % endi' else user@ount ! user@ount ) dice" ) dice2 i' user@ount 0! K>: output *ser #insJ else output *+nter a " to play again or % to pass
Programming Logic and Design, 6e
Solutions 5-'6
input userurn endi' endi' else i' dice" ! " 9 dice2 ! " output *serLs turn userurn ! " i' dice" ! " AD dice2 ! " H+ computer@ount ! % endi' else computer@ount ! computer@ount ) dice" ) dice2 i' computer@ount 0! K>: output *@omputer #insJ else i' random(2 ! " then userurn ! % else userurn ! " endi' endi' endi' endi' return 'inishp( output *+nd o' program return
Flowchart: +he flo&chart2s structure &ill be #ery similar to that gi#en for pre#ious e4ercises.
!p "or Discussion 1*. Suppose you &rote a program that you suspect is in an infinite loop because it 3ust eeps running for se#eral minutes &ith no output and &ithout ending. What &ould you add to your program to help you disco#er the origin of the problem" Answer H
Adding output statements at key locations would show you which variables were changing and which were not, helping you to locate the source of the error. You could also add decisions that display a message or variable values at key points in the process. 1. Suppose you no& that e#ery employee in your organi/ation has a se#en-digit D number used for logging on to the computer system. % loop &ould be useful to guess e#ery combination of se#en digits in an D. %re there any circumstances in &hich you should try to guess another employee2s D number"