# Programming

Topics: Edsger W. Dijkstra, Programming language, Structured programming Pages: 24 (3038 words) Published: December 5, 2012
Programming

Programming Logic and Design, 6th Edition

Chapter 3

Exercises

1. 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.

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
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

2. 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.

a.
[pic]

b.
[pic]

c.
[pic]

d.
[pic]

e.
[pic]

3. 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.

a. do A
while B is true
do C
do A
endwhile

b. do D
if E is true then
do H
do I
else
do F
if G is true then
do I
endif
endif

c.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

d.
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

e.
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

4. 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.a one-inch square
b.a two-inch by one-inch rectangle
d. a short word (for example, “cat”).

This solution assumes the above tasks are labeled as follows: A. Lower the pen to a piece of paper.
B. Raise the pen from the paper.
C. 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.) D. Turn 90 degrees to the right.

E....