Programming Logic and Design, Third Edition
Chapter 7
Answers to Review Questions
- b
- c
- a
- c
- d
- c
- d
- a
- a
- d
- a
- c
- c
- c
- b
- b
- b
- d
- d
- b
Answers to Exercises
1.What fields might you want to use as the control break fields to produce a report that lists all inventory items in a grocery store? (For example, you might choose to group items by grocery store department.) Design the print chart for the report.
Answer: Appropriate control break fields for grocery store inventory include department, manufacturer, distributor, price category, expiration date, volume sold, and so on. Print chart designs will vary.
- What fields might you want to use as the control break fields to produce a report that lists all the people you know? (For example, you might choose to group friends by city of residence.) Design the print chart for the report.
Answer: Appropriate control break fields for friends include length of relationship, first letter of last name, age, favorite hobby, source of relationship (from high school, college, work, camp, etc.), and so on. Print chart designs will vary.
- Cool’s Department Store keeps a record of every sale in the following format:
DEPARTMENT STORE SALES FILE DESCRIPTION
File name: DEPTSALES
Sorted by: Department
Field DescriptionPositionsData TypeDecimalsExample
Transaction Number1–7Numeric02615441
Amount8-13Numeric2012599
Department14-16Numeric0501
Create the logic for a program that would print each transaction’s details, with a total at the end of each department.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
- Print chart:
b. Hierarchy chart:
c. Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finish()
stop
housekeeping()
declare variables
open files
print head1
print head2
read storeRec
oldDept = storeDept
return
mainLoop()
if storeDept not equal oldDept then
perform deptBreak()
endif
print storeTransNum, storeAmount
deptTotal = deptTotal + storeAmount
read storeRec
return
deptBreak()
print "Dept", oldDept, "Total", deptTotal
deptTotal = 0
oldDept = storeDept
return
finish()
perform deptBreak()
close files
return
4.A used-car dealer keeps track of sales in the following format:
AUTO SALES FILE DESCRIPTION
File name: AUTO
Sorted by: Salesperson
Field Description PositionsData TypeDecimalsExample
Salesperson 1–20CharacterMiller
Make of car21–30CharacterFord
Vehicle type31–40CharacterSedan
Sale price41–45Numeric012500
By the end of the week, a salesperson may have sold no cars, one car, or many cars. Create the logic of a program that would print one line for each salesperson, with that salesperson’s total sales for the week and commission earned, which is 4% of the total sales.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
a.Print chart:
b.Hierarchy chart:
c. Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
perform headings()
read salesRec
prevPerson = salesPerson
return
headings()
print heading1
print heading2
return
mainLoop()
if salesPerson not equal to prevPerson then
perform totalBreak()
endif
total = total + salesPrice
read salesRec
return
totalBreak()
comm = total * commRate
print prevPerson, total, comm
prevPerson = salesPerson
total = 0
return
finishUp()
perform totalBreak()
close files
return
5.A community college maintains student records in the following format:
STUDENT FILE DESCRIPTION
File name: STUDENTS
Sorted by: Hour of first class
Field DescriptionPositionsData Type DecimalsExample
Student Name1–20Character Amy Lee
City21–30CharacterWoodstock
Hour of First Class31–32Numeric008
Phone Number33–42Numeric08154379823
The records have been sorted by hour of the day. The hour of first class is a two-digit number based on a 24-hour clock (that is, a 1 p.m. first class is recorded as 13).
Create a report that students can use to organize carpools. The report lists the names and phone numbers of students from the city of Huntley. Note that some students come from cities other than Huntley; these students should not be listed on the report.
Start a new page for each hour of the day, so that all students starting classes at the same hour are listed on the same page. Include the hour that each page represents in the heading for that page.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
- Print chart:
b.Hierarchy chart:
c. Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
read stuRec
saveHour = stuHour
return
headings()
print heading1a, searchCity, heading1b, stuHour, heading1c
print heading2
return
mainLoop()
if stuCity = searchCity then
if stuHour not equal to saveHour then
perform newHour()
endif
print stuName, stuPhone
endif
read stuRec
return
newHour()
perform headings()
saveHour = stuHour
return
finishUp()
close files
return
6.The Stanton Insurance Agency needs a report summarizing the counts of life, health, and other types of insurance policies it sells. Input records contain policy number, name of insured, policy value, and type of policy, and have been sorted in alphabetical order by type of policy. At the end of the report, display a count of all the policies.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
a.Print chart:
b.Hierarchy chart:
c. Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
perform headings()
read insRec
prevType = insType
return
headings()
print heading1
print heading2
return
mainLoop()
if insType not equal to prevType then
perform typeTotal()
endif
count = count + 1
read insRec
return
typeTotal()
print prevType, count
grandTotal = grandTotal + count
count = 0
prevType = insType
return
finishUp()
perform typeTotal()
print "Total policies", grandTotal
close files
return
- If a university is organized into colleges (such as Liberal Arts), divisions (such as Languages), and departments (such as French), what would constitute the major, intermediate, and minor control breaks in a report that prints all classes offered by the university?
Answer: The major control break would be college, the intermediate control break would be division, and the minor control break would be department.
8.A zoo keeps track of the expense of feeding the animals it houses. Each record holds one animal’s ID number, name, species (elephant, rhinoceros, tiger, lion, and so on), zoo residence (pachyderm house, large cat house, and so on), and weekly food budget. The records take the following form:
ANIMAL FEED RECORDS
File name: ANIMFOOD
Sorted by: Species within house
Field DescriptionPositionsData TypeDecimalsExample
Animal ID1–4Numeric04116
Animal name5–29CharacterElmo
Species30–45CharacterElephant
House46–55CharacterPachyderm
Weekly food56–59Numeric075
budget in dollars
Design a report that lists each animal’s ID, name, and budgeted food amount. At the end of each species group, print a total budget for the species. At the end of each house (for example, the species lion, tiger, and leopard are all in the large cat house), print the house total. At the end of the report, print the grand total.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
- Print chart:
b.Hierarchy chart:
c. Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
perform headings()
read anRec
saveSpecies = anSpecies
saveHouse = anHouse
return
headings()
print heading1
print heading2
return
mainLoop()
if anHouse = saveHouse then
if anSpecies not equal to saveSpecies then
perform speciesBreak()
endif
else
perform houseBreak()
endif
print anID, anName, anBudget
speciesTotal = speciesTotal + anBudget
readanRec
return
speciesBreak()
print "Species Total", speciesTotal
houseTotal = houseTotal + speciesTotal
saveSpecies = anSpecies
speciesTotal = 0
return
houseBreak()
perform speciesBreak()
print "House Total", houseTotal
grandTotal = grandTotal + houseTotal
saveHouse = anHouse
houseTotal = 0
return
finishUp()
perform houseBreak()
print "Grand Total", grandTotal
close files
return
9.A soft-drink manufacturer produces several flavors of drink—for example, cola, orange, and lemon. Additionally, each flavor has several versions such as regular, diet, and caffeine-free. The manufacturer operates factories in several states. Assume you have input records that list version, flavor, yearly production in gallons, and state. (For example: Regular Cola 5000 Kansas.) The records have been sorted in alphabetical order by version within flavor within state. Design the report that lists each version and flavor, with minor total production figures for each flavor and major total production figures for each state.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
- Print chart:
b.Hierarchy chart:
c.Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
perform headings()
read drinkRec
saveFlavor = drinkFlavor
saveState = drinkState
return
headings()
print heading1
print heading2
return
mainLoop()
if drinkState = saveState then
if drinkFlavor not equal to saveFlavor then
perform newFlavor()
endif
else
perform newState()
endif
print drinkVersion, drinkFlavor
totalFlavor = totalFlavor + drinkGal
read drinkRec
return
newFlavor()
print "Flavor", saveFlavor, totalFlavor
totalState = totalState + totalFlavor
totalFlavor = 0
saveFlavor = drinkFlavor
return
newState()
perform newFlavor()
print "State", saveState, totalState
totalState = 0
saveState = drinkState
return
finishUp()
perform newState()
close files
return
10.An art shop owner maintains records for each item in the shop, including the title of the work, the artist who made the item, the medium (for example, watercolor, oil, or clay), and the monetary value. The records are sorted by artist within medium. Design a report that lists all the items in the store, with a minor total value following each artist’s work, and a major total value following each medium. Allow only 40 detail lines per page.
- Create the print chart.
- Create the hierarchy chart.
- Create the flowchart.
- Create the pseudocode.
a.Print chart:
b.Hierarchy chart:
c.Flowchart:
d.Pseudocode:
start
perform housekeeping()
while not eof
perform mainLoop()
endwhile
perform finishUp()
stop
housekeeping()
declare variables
open files
perform headings()
read artRec
saveArtist = artArtist
saveMedium = artMedium
return
headings()
print heading1
print heading2
lineCount=2
return
mainLoop()
if artMedium = saveMedium then
if artArtist not equal to saveArtist then
perform newArtist()
endif
else
perform newMedium()
endif
print artTitle
totalArtist = totalArtist + artValue
lineCount = lineCount + 1
if lineCount = 42 then
eject page
perform headings
endif
read artRec
return
newArtist()
print "Artist", saveArtist, totalArtist
totalMedium = totalMedium + totalArtist
totalArtist = 0
saveArtist = artArtist
return
newMedium()
perform newArtist()
print "Medium", saveMedium, totalMedium
totalMedium = 0
saveMedium = artMedium
return
finishUp()
perform newMedium()
close files
return