Programming Logic

Only available on StudyMode
  • Download(s) : 961
  • Published : May 10, 2013
Open Document
Text Preview
Programming Logic and Design, 6th Edition

Chapter 5

Exercises

1. What is output by each of the pseudocode segments in Figure 5-22?

Answer:
|a. |5, 22, 5 | |b. |4, 6, 7 | |c. |5, 6 | |d. |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | | |Goodbye | |e. |Hello | | |Hello | | |Hello | |f. |Adios | | |Adios | | |Adios | | |Adios | | |Adios | | |Adios | | |Adios | | |Adios |

2. Design the logic for a program that outputs every number from 1 through 10.

Answer:
A sample solution follows

Flowchart:

[pic]

Pseudocode:

start
Declarations
num number
housekeeping()
number = 1
while number 99999
output “Please enter a valid zip code”
input custZip
endwhile
return

validateCustAge()
while custAge < 10 OR custAge > 110
output “Please enter a valid customer age”
input custAge
endwhile
return

validateNumItems()
Declarations
num attempts = 0
num LIMIT = 2
validNumItems = 1
while (numItems < 1 OR numItems > 12) AND attempts < LIMIT
output “Please reenter the number of items”
input numItems
attempts = attempts + 1
endwhile
if attempts = 2 AND numItems < 0 then
output “An invalid number of items was entered”
output “This order will not be counted”
validNumItems = 0
endif
return

housekeeping()
output ZIP_PROMPT
input custZip
return

detailLoop()
output AGE_PROMPT
input custAge
output ITEM_PROMPT
input numItems

validateZipCode()
validateCustAge()
validateNumItems()

countOfOrders = countOfOrders + 1
totalCustAge = totalCustAge + custAge

if custZip = STORE_ZIP
numItemsInStoreZip = numItemsInStoreZip + numItems
else
numItemsInOtherZips = numItemsInOtherZips + numItems
endif

if custAge < 30 then
numItemsUnder30 = numItemsUnder30 + numItems
else
numItems30AndOlder = numItems30AndOlder + numItems
endif

output ZIP_PROMPT
input custZip
return

finishUp()
Declarations
avgCustAge
if countOfOrders > 0 then
output “Total items ordered in the ”,
STORE_ZIP, “ zip code = ”, numItemsInStoreZip
output “Total items ordered in other zip codes = ”, numItemsInOtherZips
avgCustAge = totalCustAge / countOfOrders
output “The average customer age is: ”, avgCustAge
output numItemsUnder30,
“ items were ordered by customers under 30”
output numItems30AndOlder,...
tracking img