Programming Logic and Design
Programming Logic and Design, 6th Edition
Chapter 3
Exercises
- In Figure 3-10 the process of buying and planting flowers in the spring was shown using the same structures as the generic example in Figure 3-9. Describe some other process with which you are familiar using exactly the same logic.
Answer:
Student answers will vary widely. They should come up with processes that fit the generic logic shown in Figure 3-9. Some examples could include: making a dentist appointment or registering for a class. Pseudocode for each follows.
if it’s time for your annual teeth cleaning
call the dentist for an appointment tomorrow
while the day and time you want isn’t available
if another time the same day is available
make the appointment at the new time on the same day
else
pick a new day and time
endif
endwhile
write appointment on your calendar
endif
if you are taking a class this semester then
register for a class
while the class is full
if another section is available
enroll for the available section
else
select a new class
endif
endwhile
print schedule
endif
- Each of the flowchart segments in Figure 3-35 is unstructured. Redraw each flowchart segment so that it does the same thing but is structured.
Answer:
a.
c.
d.
e.
- Write pseudocode for each example (a through e) in Exercise 2 making sure your pseudocode is structured but accomplishes the same tasks as the flowchart segment.
Answer:
- do A
while B is true
do C
do A
endwhile
- do D
if E is true then
do H
do I
else
do F
if G is true then
do I
endif
endif
- do k
if L is true then
do P
while Q is true
do P
endwhile
do R
else
do M
do N
if O is true then
do R
endif
endif
- do S
if T is true then
do Y
if Z is true then
do V
if W is true then
do A
else
do X
endif
else
do A
endif
else
do U
do V
if W is true then
do A
else
do X
endif
endif
- if B is true then
do G
while H is not true
do I
do G
endwhile
do D
while E is true
do I
do D
endwhile
do F
else
do C
do D
while E is true
do I
do D
endwhile
do F
endif
- Assume you have created a mechanical arm that can hold a pen. The arm can perform the following tasks:
- Lower the pen to a piece of paper.
- Raise the pen from the paper.
- Move the pen one inch along a straight line. (If the pen is lowered, this action draws a one-inch line from left to right; if the pen is raised, this action just repositions the pen one inch to the right.)
- Turn 90 degrees to the right.
- Draw a circle that is one inch in diameter.
Draw a structured flowchart or write structured pseudocode describing the logic that would cause the arm to draw the following:
- a one-inch square
- a two-inch by one-inch rectangle
- a string of three beads
- a short word (for example, “cat”).
Answer:
This solution assumes the above tasks are labeled as follows:
- Lower the pen to a piece of paper.
- Raise the pen from the paper.
- Move the pen one inch along a straight line. (If the pen is lowered, this action draws a one-inch line from the left to right; if the pen is raised, this action just repositions the pen one inch to the right.)
- Turn 90 degrees to the right.
- Draw a circle that is one inch in diameter.
- a one-inch square
Pseudocode:
start
lower the pen to a piece of paper
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
raise the pen from the paper
stop
or
start
do A
do C
do D
do C
do D
do C
do D
do C
do B
stop
Flowchart:
|
|
- a two-inch by one-inch rectangle
Pseudocode:
start
lower the pen to a piece of paper
move one inch along a straight line
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
move one inch along a straight line
turn 90 degrees to the right
move one inch along a straight line
raise the pen from the paper
stop
or
start
do A
do C
do C
do D
do C
do D
do C
do C
do D
do C
do B
stop
Flowchart:
|
|
- a string of three beads
Pseudocode:
start
lower the pen to a piece of paper
draw a circle that is one-inch in diameter
raise the pen from the paper
move one inch along a straight line
lower the pen to a piece of paper
draw a circle that is one-inch in diameter
raise the pen from the paper
move one inch along a straight line
lower the pen to a piece of paper
draw a circle that is one-inch in diameter
raise the pen from the paper
stop
or
start
do A
do E
do B
do C
do A
do E
do B
do C
do A
do E
do B
stop
Flowchart:
|
- a short word (for example, “cat”)
start
lower the pen to a piece of paper
move one inch along a straight line
raise the pen from the paper
turn 90 degrees right
turn 90 degrees right
move one inch along a straight line
turn 90 degrees right
lower the pen to a piece of paper
move one inch along a straight line
turn 90 degrees right
move one inch along a straight line
raise the pen from the paper
move one inch along a straight line
lower the pen to a piece of paper
move one inch along a straight line
turn 90 degrees right
move one inch along a straight line
turn 90 degrees right
move one inch along a straight line
turn 90 degrees right
move one inch along a straight line
move one inch along a straight line
raise the pen from the paper
turn 90 degrees right
move one inch along a straight line
turn 90 degrees right
lower the pen to a piece of paper
move one inch along a straight line
turn 90 degrees right
turn 90 degrees right
turn 90 degrees right
raise the pen from the paper
move one inch along a straight line
lower the pen to a piece of paper
move one inch along a straight line
move one inch along a straight line
turn 90 degrees right
turn 90 degrees right
raise the pen from the paper
move one inch along a straight line
lower the pen to a piece of paper
turn 90 degrees right
move one inch along a straight line
raise the pen from the paper
stop
or
start
do A
do C
do B
do D
do D
do C
do D
do A
do C
do D
do C
do B
do C
do A
do C
do D
do C
do D
do C
do D
do C
do C
do B
do D
do C
do D
do A
do C
do D
do D
do D
do B
do C
do A
do C
do C
do D
do D
do B
do C
do A
do D
do C
do B
stop
The flowchart will be very similar to parts a-c.
- Assume you have created a mechanical robot that can perform the following tasks:
- Stand up.
- Sit down.
- Turn left 90 degrees.
- Turn right 90 degrees.
- Take a step.
Additionally, the robot can determine the answer to one test condition:
- Am I touching something?
Place two chairs 20 feet apart, directly facing each other. Draw a structured flowchart or write pseudocode describing the logic that would allow the robot to start from a sitting position in one chair, cross the room, and end up sitting in the other chair.
Answer:
This solution assumes the above tasks are labeled as follows:
- Stand up.
- Sit down.
- Turn left 90 degrees.
- Turn right 90 degrees.
- Take a step.
- Am I touching something?
Pseudocode:
start
stand up
take a step
while Am I touching something? is not true
take a step
endwhile
turn left 90 degrees
turn left 90 degrees
sit down
stop
or
start
do A
do E
while F is not true
do E
endwhile
do C
do C
do B
stop
Flowchart:
start
do A
do E
while F is not true
do E
endwhile
do D
do C
do C
do D
while F is true
do B
stop
start
(A)stand up
(E)take a step
while (F)Am I touching something? is not true
take a (E)step
end while
turn right 90 degrees (D)
turn left 90 degrees (C)
turn left 90 degrees (C)
turn right 90 degrees (D)
while (F)Am I touching something is true
(B)sit down
stop
3.3^^ (Pseudocode below)
- Looking up a word in a dictionary can be a complicated process. For example, assume you want to look up “logic.” You might open the dictionary to a random page and see “juice.” You know that word comes alphabetically before “logic,” so you flip forward and see “lamb.” That is still not far enough, so you flip forward and see “monkey.” That means you have gone too far, so now you flip back, and so on. Draw a structured flowchart or write pseudocode that describes the process of looking up a word in a dictionary. Pick a word at random and have a fellow student attempt to carry out your instructions.
Answer:
Answers will vary.
Pseudocode:
start
open the dictionary
while word not on page
if word > last word on page
turn the page forward
else
turn the page backward
endif
endwhile
stop
Flowchart:
- Draw a structured flowchart or write structured pseudocode describing your preparation to go to work or school in the morning. Include at least two decisions and two loops.
Answer:
Answers will vary. An example solution is shown below.
Flowchart:
Pseudocode:
start
get out of bed
while feeling awake is not true
drink coffee
endwhile
if the outside temperature < 65 is true then
wear sweater
else
wear t-shirt
endif
if you’re feeling hungry is true then
eat breakfast
endif
while you have keys is not true
search for keys
endwhile
drive to work
stop
- Draw a structured flowchart or write structured pseudocode describing your preparation to go to bed at night. Include at least two decisions and two loops.
Answer:
Answers will vary. An example solution is shown below.
Flowchart:
Pseudocode:
start
while teeth need brusing
brush teeth
endwhile
if temperature is less than 65 degrees then
wear flannel pajamas
else
wear cotton pajamas
endif
if tomorrow is a school day then
set alarm clock
endif
while thirsty
drink water
endwhile
get in bed
stop
- Draw a structured flowchart or write structured pseudocode describing how your paycheck is calculated. Include at least two decisions.
Answer:
Answers will vary. An example solution is shown below.
Flowchart:
Pseudocode:
start
if the employee is full-time is true then
weekly pay = 40 * pay rate
if employee worked overtime is true then
overtime pay = (hours worked – 40) * 1.5 * pay rate
weekly pay = weekly pay + overtime pay
endif
else
weekly pay = hours worked * pay rate
endif
net pay = weekly pay - taxes
stop
- Draw a structured flowchart or write structured pseudocode describing the steps a retail store employee should follow to process a customer purchase. Include at least two decisions.
Answer:
Answers will vary. An example solution is shown below.
Flowchart:
Pseudocode:
start
add item price to total
while customer has more items is true
add item price to total
endwhile
if customer has coupon is true
subtract discount from total
endif
display customer total
if customer is paying w/cash is true
accept cash
while customer needs change is true
give change
endwhile
else
swipe credit card
endif
stop
Find the Bugs
- Your student disk contains files named DEBUG03-01.txt, DEBUG03-02.txt, and DEBUG03-03.txt. Each file starts with some comments that describe the problem. Comments are lines that begin with two slashes (//). Following the comments, each file contains pseudocode that has one or more bugs you must find and correct.
Answer:
Please see the DEBUG03-01.txt, DEBUG03-02.txt, and DEBUG03-03.txt solution files.
Game Zone
- Choose a very simple children’s game and describe its logic, using a structured flowchart or pseudocode. For example, you might try to explain Rock, Paper, Scissors; Musical Chairs; Duck, Duck, Goose; the card game War; or the elimination game Eenie, Meenie, Minie, Moe.
Answer:
Answers will vary. The following is a possible solution for the card game War.
Flowchart:
Pseudocode:
gameOfWar()
ask friend to play the card game War
while answer is yes
deal out my hand and your hand
while both players have cards
turn over my card and your card
if my card is equal to your card then
cards stay on table
else
if my card is higher than your card
I collect cards on table
else
you collect all cards on table
endif
endif
endwhile
if my hand is empty is true
you are the winner
else
I am the winner
endif
ask friend to play the card game War
endwhile
return
- Choose a television game show such as Deal or No Deal or Jeopardy! and describe its rules using a structured flowchart or pseudocode.
Answer:
Answers will vary. The following is a possible solution for Jeopardy!.
Flowchart:
Pseudocode:
jeopardy()
while questions are available
contestant picks category and dollar amount
question is read
while the timer hasn’t run out
contestant buzzes in and answers
if answer is correct
contestant earns dollar amount
time runs out
else
contestant loses dollar amount
time decreases
endif
endwhile
endwhile
return
- Choose a professional sport such as baseball or football and describe the actions in one play period using a structured flowchart or pseudocode.
Answer:
Answers will vary. The following is a very simple example solution for tennis.
Flowchart:
Pseudocode:
tennis()
player attempts serve
while ball goes over net and is in bounds
opposite player returns ball
endwhile
return
Up for Discussion
- Find more information about one of the following people and explain why he or she is important to structured programming: Edsger Dijkstra, Corrado Bohm, Giuseppe Jacopini, and Grace Hopper.
Answer:
Edsger Wybe Dijkstra was a Dutch computer scientist who lived from 1930 to 2002. Among his contributions to computer science are the shortest path-algorithm, also known as Dijkstra's algorithm. He received the Turing Award in 1972. He was also known for his low opinion of the GOTO statement in computer programming, culminating in the 1968 article Go To Statement Considered Harmful . This article was regarded as a major step towards the widespread deprecation of the GOTO statement and its effective replacement by control structures such as the while loop.
Bohm and Jacopini were theorists who showed in 1966 that all logical problems could be handled using only the three control structures sequence, selection, and loop.
Rear Admiral Grace Hopper lived from 1906 to 1992. She was a U.S. Navy officer and computer scientist. She was one of the first programmers of the Harvard Mark I calculator and she developed the first compiler. She often has been quoted as claiming to develop the term "bug" when a moth was found in the Mark I circuitry.
- Computer programs can contain structures within structures and stacked structures, creating very large programs. Computers also can perform millions of arithmetic calculations in an hour. How can we possibly know the results are correct?
Answer:
Only by repeatedly testing portions of programs can you have confidence that their results are correct. According to author Harlan Mills, "There is no foolproof way to ever know that you have found the last error in a program. So the best way to acquire confidence that a program has no errors is never to find the first one, no matter how much it is tested and used".
Some people believe that software never breaks, unlike mechanical parts such as bolts, or electronic parts such as transistors. However, there have been many incidents that prove this theory incorrect:
- Therac-25 was a radiation therapy machine. Between 1985 and 1987 at least six patients were given massive overdoses of radiation. These accidents highlighted the dangers of software control of safety-critical systems.
- The British destroyer Sheffield was sunk because the radar system identified an incoming missile as "friendly".
- On February 25, 1991, during the Gulf War, 28 lives were lost when an error that missed 0.000000095 second in precision in every 10th of a second made the Patriot missile fail to intercept a scud missile.
- In 1991, after changing three lines of code in a signaling program which contains millions of lines of code, the local telephone systems in California and along the Eastern seaboard came to a stop.
- Develop a checklist of rules you can use to help you determine whether a flowchart or pseudocode segment is structured.
Answer:
- Every structure must have only one entry point and one exit point.
- In a flowchart you should be able to remove structured pieces, replacing them with a place holder, and see that the resulting shell is structured. In a flowchart, no lines should ever cross.
- In pseudocode, every if statement should pair with an endif and every while statement should pair with an endwhile with no intervening endif or endwhile
hihi
Want to order fresh copy of the Sample Template Answers? online or do you need the old solutions for Sample Template, contact our customer support or talk to us to get the answers of it.