Top Banner
Programming Logic and Design, 6e Solutions 3-1 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. 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
39

Ch03 Logic6e Solutions

Nov 29, 2015

Download

Documents

Wei Xuen Lim

algorithms, sample questions, solution, pseudocode, flowchart, bahasa, IF, then, BM ver.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-1

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.

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 cleaningcall the dentist for an appointment tomorrowwhile the day and time you want isn’t available

if another time the same day is availablemake the appointment at the new time on the same day

elsepick a new day and time

endifendwhilewrite appointment on your calendar

endif

if you are taking a class this semester thenregister for a classwhile the class is full

if another section is availableenroll for the available section

elseselect a new class

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

Answer:

Page 2: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-2

a.

b.

Page 3: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-3

c.

Page 4: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-4

d.

Page 5: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-5

e.

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.

Answer:

a. do Awhile B is true

do C do A

endwhile

Page 6: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-6

b. do Dif E is true then

do H do I

else do F if G is true then do I

endifendif

c. do kif L is true then

do Pwhile Q is true

do Pendwhiledo R

elsedo Mdo Nif O is true then

do Rendif

endif

d.

do Sif 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 endifelse do U do V if W is true then do A else do X endifendif

Page 7: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-7

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 Felse do C do D while E is true do I do D endwhile do Fendif

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 squareb. a two-inch by one-inch rectanglec. a string of three beadsd. a short word (for example, “cat”).

Answer:

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.

Page 8: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-8

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. Draw a circle that is one inch in diameter.

a. a one-inch square

Pseudocode:startlower the pen to a piece of paper move one inch along a straight lineturn 90 degrees to the rightmove one inch along a straight line turn 90 degrees to the rightmove one inch along a straight lineturn 90 degrees to the rightmove one inch along a straight lineraise the pen from the paper

stop

or

startdo Ado Cdo Ddo Cdo Ddo Cdo Ddo Cdo B

stop

Page 9: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-9

Flowchart:

b. a two-inch by one-inch rectangle

Pseudocode:startlower the pen to a piece of papermove one inch along a straight linemove one inch along a straight lineturn 90 degrees to the rightmove one inch along a straight lineturn 90 degrees to the rightmove one inch along a straight linemove one inch along a straight lineturn 90 degrees to the rightmove one inch along a straight line raise the pen from the paper

do A

do C

do D

do C

do C

do C

do D

do D

do B

start

stop

Page 10: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-10

stop

or

startdo Ado Cdo Cdo Ddo Cdo Ddo Cdo Cdo Ddo Cdo B

stop

Page 11: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-11

Flowchart:

c. a string of three beads

Pseudocode:startlower the pen to a piece of paperdraw a circle that is one-inch in diameter raise the pen from the paper

do A

do C

do C

do D

do D

do C

do C

do C

do D

do C

do B

start

stop

Page 12: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-12

move one inch along a straight linelower the pen to a piece of paperdraw a circle that is one-inch in diameter raise the pen from the papermove one inch along a straight line lower the pen to a piece of paperdraw a circle that is one-inch in diameter raise the pen from the paper

stop

or

startdo Ado Edo Bdo Cdo Ado Edo Bdo Cdo Ado Edo B

stop

Page 13: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-13

Flowchart:

d. a short word (for example, “cat”)

startlower the pen to a piece of papermove one inch along a straight line

do A

do E

do B

do C

do E

do C

do B

do A

do E

do B

stop

do A

start

Page 14: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-14

raise the pen from the paperturn 90 degrees rightturn 90 degrees rightmove one inch along a straight lineturn 90 degrees rightlower the pen to a piece of papermove one inch along a straight lineturn 90 degrees rightmove one inch along a straight lineraise the pen from the papermove one inch along a straight linelower the pen to a piece of papermove one inch along a straight lineturn 90 degrees rightmove one inch along a straight lineturn 90 degrees rightmove one inch along a straight lineturn 90 degrees rightmove one inch along a straight linemove one inch along a straight lineraise the pen from the paperturn 90 degrees rightmove one inch along a straight lineturn 90 degrees rightlower the pen to a piece of papermove one inch along a straight lineturn 90 degrees rightturn 90 degrees rightturn 90 degrees rightraise the pen from the papermove one inch along a straight linelower the pen to a piece of papermove one inch along a straight linemove one inch along a straight lineturn 90 degrees rightturn 90 degrees rightraise the pen from the papermove one inch along a straight linelower the pen to a piece of paperturn 90 degrees rightmove one inch along a straight lineraise the pen from the paper

stop

or

startdo Ado Cdo Bdo Ddo Ddo Cdo Ddo Ado C

Page 15: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-15

do Ddo Cdo Bdo Cdo Ado Cdo Ddo Cdo Ddo Cdo Ddo Cdo Cdo Bdo Ddo Cdo Ddo Ado Cdo Ddo Ddo Ddo Bdo Cdo Ado Cdo Cdo Ddo Ddo Bdo Cdo Ado Ddo Cdo B

stop

The flowchart will be very similar to parts a-c.

5. 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?

Page 16: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-16

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:A. Stand up.B. Sit down.C. Turn left 90 degrees.D. Turn right 90 degrees.E. Take a step.F. Am I touching something?

Pseudocode:start

stand uptake a stepwhile Am I touching something? is not true take a stependwhileturn left 90 degreesturn left 90 degreessit down

stop

or

startdo Ado Ewhile F is not true do Eendwhiledo Cdo Cdo B

stop

Page 17: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-17

Flowchart:

6. 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:

startopen the dictionarywhile word not on page

if word > last word on pageturn the page forward

else

Page 18: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-18

turn the page backwardendif

endwhilestop

Flowchart:

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

Page 19: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-19

Flowchart:

Page 20: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-20

Pseudocode:start

get out of bedwhile feeling awake is not true

drink coffeeendwhileif the outside temperature < 65 is true then

wear sweaterelse

wear t-shirtendif

if you’re feeling hungry is true theneat breakfast

endifwhile you have keys is not true

search for keysendwhiledrive to work

stop

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

Page 21: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-21

Flowchart:

Page 22: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-22

Pseudocode:start while teeth need brusing

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

endifwhile thirsty

drink water endwhile get in bedstop

9. 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:

Page 23: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-23

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

Page 24: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-24

10. 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:

Page 25: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-25

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 endifstop

Find the Bugs

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

12. 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:

Page 26: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-26

Pseudocode:gameOfWar()

ask friend to play the card game Warwhile 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

Page 27: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-27

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 Warendwhile

return

13. 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:

Page 28: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-28

Page 29: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-29

Pseudocode:jeopardy()

while questions are availablecontestant picks category and dollar amountquestion is readwhile the timer hasn’t run out

contestant buzzes in and answersif answer is correct

contestant earns dollar amounttime runs out

elsecontestant loses dollar amounttime decreases

endifendwhile

endwhilereturn

14. 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:

Page 30: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-30

Pseudocode:tennis() player attempts servewhile ball goes over net and is in bounds

opposite player returns ballendwhile

return

Up for Discussion

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

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

Page 31: Ch03 Logic6e Solutions

Programming Logic and Design, 6e Solutions 3-31

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.

17. 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 statements.