Vous êtes sur la page 1sur 16

Programming Logic and Design, 6e

Solutions 2-1

Programming Logic and Design, 6th Edition


Chapter 2
Exercises
1. Explain why each of the following names does or does not seem like a good variable
name to you.
Answer:
Answers will vary. A possible solution:
a. c
b. cost
c. costAmount
d. cost amount
e. cstofdngbsns
f. costOfDoingBusinessThisFiscalYear
g. costYear2012
h. 2012YearCost

Valid, but probably too


short to be descriptive
Good
Good, but redundant
Invalid, spaces arent
allowed
Valid, but difficult to read
Valid, but long and
awkward
Good
Invalid, cannot start with
a digit

17
2. If myAge and yourRate are numeric variables, and departmentName is a string
variable, which of the following statements are valid assignments? If a statement is
not valid, explain why not.
Answer:
a.
b.
c.
d.
e.

myAge = 23
myAge = yourRate
myAge = departmentName
myAge = departmentName
42 = myAge

f.
g.
h.
i.

yourRate = 3.5
yourRate = myAge
yourRate = departmentName
6.91 = yourRate

j.
k.
l.
m
.

departmentName
departmentName
departmentName
departmentName

=
=
=
=

Personnel
Personnel
413
413

Valid
Valid
Invalid, cannot assign string to numeric
Invalid, cannot assign string to numeric
Invalid, cannot assign a value to a literal
constant
Valid
Valid
Invalid, cannot assign string to numeric
Invalid, cannot assign a value to a literal
constant
Invalid, literal string must be in quotes
Valid
Invalid, literal string must be in quotes
Valid

Programming Logic and Design, 6e

Solutions 2-2

n. departmentName = myAge
o. departmentName = yourRate
p. 413 = departmentName

Invalid, cannot assign numeric to string


Invalid, cannot assign numeric to string
Invalid, cannot assign a value to a literal
constant
Invalid, cannot assign a value to a literal
constant

q. 413 = departmentName

3. Assume that cost = 10 and price = 12. What is the value of each of the
following expressions?
Answer:
a. price cost * 2
b. 15 + price 3 * 2
c. (price + cost) * 3
d. 4 3 * 2 + cost
e. cost * ((price 8) + 5) + 100

-8
21
66
8
19
0

4. Draw a typical hierarchy chart for a paycheck-producing program. Try to think of at


least 10 separate modules that might be included. For example, one module might
calculate an employees dental insurance premium.
Answer:
main()

getData()

computeGross()

computeFederalWithholding()

calculateCheck()

printCheck()

computeMandatoryDeductions()

computeStateWithholding()

computeVoluntaryDeductions()

computeMedicalIns()

computeDentalIns()

5. a. Draw the hierarchy chart and then plan the logic for a program for the sales
manager of The Couch Potato Furniture Company. The manager needs a program to
determine the profit on any item sold. Input includes the wholesale price and retail
price for an item. The output is the items profit, which is the retail price minus the
wholesale price. Use three modules. The main program declares global variables and

Programming Logic and Design, 6e

Solutions 2-3

calls housekeeping, detail, and end-of-job modules. The housekeeping module


prompts for and accepts a wholesale price. The detail module prompts for and accepts
the retail price, computes the profit, and displays the result. The end-of-job module
displays the message Thanks for using this program.
Answer: A sample solution is as follows:
a. Hierarchy chart:

main program

housekeeping()

Flowchart:

Pseudocode:
start
Declarations
num wholesalePrice
num retailPrice

detail()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-4

num profit
string WHOLE_PROMPT = Enter the wholesale price
string RETAIL_PROMPT = Enter the retail price
string END_LINE = Thanks for using this program
housekeeping()
detail()
endOfJob()
stop
housekeeping()
output WHOLE_PROMPT
input wholesalePrice
return
detail()
output RETAIL_PROMPT
input retailPrice
profit = retailPrice - wholesalePrice
output profit
return
endOfJob()
output END_LINE
return

b. Revise the profit-determining program so that it runs continuously for any number
of items. The detail loop executes continuously while the wholesale price is not 0; in
addition to calculating the profit, it prompts the user for and gets the next wholesale
price. The end-of-job module executes after 0 is entered for the wholesale price.
Answer: A sample solution is as follows:
b. Hierarchy chart:

main program

housekeeping()

Flowchart:

detailLoop()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-5

Pseudocode:
start
Declarations
num wholesalePrice
num retailPrice
num profit
string WHOLE_PROMPT = Enter the wholesale price
string RETAIL_PROMPT = Enter the retail price
string END_LINE = Thanks for using this program
housekeeping()
while not (wholesalePrice = 0)
detailLoop()
endwhile
endOfJob()
stop
housekeeping()
output WHOLE_PROMPT
input wholesalePrice
return
detailLoop()
output RETAIL_PROMPT

Programming Logic and Design, 6e

Solutions 2-6

input retailPrice
profit = retailPrice - wholesalePrice
output profit
output WHOLE_PROMPT
input wholesalePrice
return
endOfJob()
output END_LINE
return

6. a. Draw the hierarchy chart and then plan the logic for a program that calculates the
gown size a student needs for a graduation ceremony. The program uses three
modules. The first prompts a user for and accepts the students height in inches. The
second module accepts the students weight in pounds and converts the students
height to centimeters and weight to grams. Then, it calculates the graduation gown
size needed by adding 1/3 of the weight in grams to the value of the height in
centimeters. The programs output is the gown size the student should order. There
are 2.54 centimeters in an inch and 453.59 grams in a pound. Use named constants
wherever you think they are appropriate. The last module displays the message End
of job.
Answer: A sample solution is as follows:
Hierarchy chart:

main program

housekeeping()

Flowchart:

detail()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-7

Pseudocode:
start
Declarations
num studentInches
num studentPounds
num studentCm
num studentGrams
num studentSize
num CENT_IN_INCH = 2.54
num GRAM_IN_POUND = 453.59
string HEIGHT_PROMPT = Enter the students height in
inches
string WEIGHT_PROMPT = Enter the students weight in
pounds
string END_LINE = End of job
housekeeping()
detail()
endOfJob()
stop
housekeeping()
output HEIGHT_PROMPT
input studentInches

Programming Logic and Design, 6e

Solutions 2-8

return
detail()
output WEIGHT_PROMPT
input studentPounds
studentCm = studentInches * CENT_IN_INCH
studentGrams = studentPounds * GRAM_IN_POUND
studentSize = ((1 / 3) * studentGrams) + studentCm
output studentSize
return
endOfJob()
output END_LINE
return

b. Revise the size-determining program to execute continuously until the user enters 0
for the height in inches.
Answer: A sample solution is as follows:
b. Hierarchy chart:

main program

housekeeping()

Flowchart:

detailLoop()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-9

Pseudocode:
start
Declarations
num studentFeet
num studentInches
num studentPounds
num studentCm
num studentGrams
num studentSize
num CENT_IN_INCH = 2.54
num GRAM_IN_POUND = 453.59
string HEIGHT_PROMPT = Enter the students height in
inches
string WEIGHT_PROMPT = Enter the students weight in
pounds
string END_LINE = End of job
housekeeping()

Programming Logic and Design, 6e

Solutions 2-10

while not (studentInches = 0)


detailLoop()
endwhile
endOfJob()
stop
housekeeping()
output HEIGHT_PROMPT
input studentInches
return
detailLoop()
output WEIGHT_PROMPT
input studentPounds
studentCm = studentInches * CENT_IN_INCH
studentGrams = studentPounds * GRAM_IN_POUND
studentSize = ((1 / 3) * studentGrams) + studentCm
output studentSize
output HEIGHT_PROMPT
input studentInches
return
endOfJob()
output END_LINE
return

7. Draw the hierarchy chart and design the logic for a program that contains
housekeeping, detail loop, and end-of-job modules, and that calculates the service
charge customers owe for writing a bad check. The main program declares any
needed global variables and constants and calls the other modules. The housekeeping
module displays a prompt for and accepts a customers last name. While the user does
not enter ZZZZ for the name, the detail loop accepts the amount of the check in
dollars and cents. The service charge is computed as $20 plus 2 percent of the check
amount. The detail loop also displays the service charge and then prompts the user for
the next customers name. The end-of-job module, which executes after the user
enters the sentinel value for the name, displays a message that indicates the program
is complete.
Answer: A sample solution is as follows:
Hierarchy chart:

main program

housekeeping()

detailLoop()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-11

Flowchart:

Pseudocode:
start
Declarations
string customerLastName
num checkAmount
num serviceCharge
num SERVICE_CHARGE_BASE = 20
num SERVICE_CHARGE_PERCT = 0.02
string NAME_PROMPT = Enter the customers last name
string CHECK_PROMPT = Enter the amount of the check in
dollars and cents
string END_LINE = Thank you for using the program
housekeeping()
while not (customerLastName = ZZZZ)
detailLoop()
endwhile
endOfJob()
stop
housekeeping()
output NAME_PROMPT
input customerLastName
return

Programming Logic and Design, 6e

Solutions 2-12

detailLoop()
output CHECK_PROMPT
input checkAmount
serviceCharge = SERVICE_CHARGE_BASE +
SERVICE_CHARGE_PERCT * checkAmount
output serviceCharge
output NAME_PROMPT
input customerLastName
return
endOfJob()
output END_LINE
return

8. Draw the hierarchy chart and design the logic for a program for the owner of Bits and
Pieces Manufacturing Company, who needs to calculate an employees projected
salary following a raise. The input is the name of the employee, the employees
current weekly salary, and the percentage increase expressed as a decimal (for
example, 0.04 for a 4 percent raise). Design the program so that it runs continuously
for any number of employees using three modules. The housekeeping module
prompts the user for the percent raise that will be applied to every employee, and
prompts for the first employees name. The detail loop executes continuously until the
user enters XXX for the employees name. The detail loop gets the employees
weekly salary, applies the raise, produces the result, and prompts for the next
employee name. The end-of-job module, which executes after the user enters the
sentinel value for the name, displays a message that indicates the program is
complete.
Answer: A sample solution is as follows:
Hierarchy chart:

main program

housekeeping()

Flowchart:

detailLoop()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-13

Pseudocode:
start
Declarations
string employeeName
num weeklySalary
num percentIncrease
num newSalary
string NAME_PROMPT = Enter the employees name
string PERCT_PROMPT = Enter the percentage increase
(as a decimal)
string SALARY_PROMPT = Enter the weekly salary
string END_LINE = Thank you for using the program
housekeeping()
while not (employeeName = XXX)
detailLoop()
endwhile
endOfJob()
stop
housekeeping()
output PERCT_PROMPT
input percentIncrease
output NAME_PROMPT
input employeeName
return

Programming Logic and Design, 6e

Solutions 2-14

detailLoop()
output SALARY_PROMPT
input weeklySalary
newSalary = weeklySalary * percentIncrease + weeklySalary
output newSalary
output NAME_PROMPT
input employeeName
return
endOfJob()
output END_LINE
return

9. Draw the hierarchy chart and design the logic for a program for the manager of the
Jeter County softball team, who wants to compute batting averages for his players. A
batting average is computed as hits divided by at-bats, and is usually expressed to
three decimal positions (for example, .235). Design a program that prompts the user
for a player jersey number, the number of hits, and the number of at-bats, and then
displays all the data, including the calculated batting average. The program accepts
players continuously until 0 is entered for the jersey number. Use appropriate
modules, including one that displays End of job after the sentinel is entered for the
jersey number.
Answer: A sample solution is as follows:
Hierarchy chart:

main program

housekeeping()

Flowchart:

detailLoop()

endOfJob()

Programming Logic and Design, 6e

Solutions 2-15

Pseudocode:
start
Declarations
num jerseyNumber
num numOfHits
num numOfAtBats
num battingAvg
string NUM_PROMPT = Enter the players jersey number
string HITS_PROMPT = Enter the number of hits
string BATS_PROMPT = Enter the number of at bats
string END_LINE = End of job
housekeeping()
while not (jerseyNumber = 0)
detailLoop()
endwhile
endOfJob()
stop
housekeeping()
output NUM_PROMPT
input jerseyNumber

Programming Logic and Design, 6e


return
detailLoop()
output HITS_PROMPT
input numOfHits
output BATS_PROMPT
input numOfAtBats
battingAvg = numOfHits / numOfAtBats
output battingAvg
output NUM_PROMPT
input jerseyNumber
return
endOfJob()
output END_LINE
return

Solutions 2-16

Vous aimerez peut-être aussi