Docsity

Programming and Problem Solving: Exercises and Algorithms, Assignments of Engineering

Toggle sidebar
bg1
Chapter 1 Overview of Programming and Problem Solving
Chapter 1
EXERCISE ANSWERS
Exam Preparation Exercises
1. The steps keep repeating forever because there is no way for the algorithm to stop. (Also, it is a poor
algorithm because the last action is lathering—you wouldn’t want to leave the lather in your hair.)
Corrected algorithm:
(1) Wet hair.
(2) Lather.
(3) Rinse.
(4) Repeat steps 2 and 3 once.
3. Recipe for pound cake:
Preheat the oven to 350 degrees
Line the bottom of a 9−inch tube pan with wax paper
Sift 2¾ c flour, ¾ t cream of tartar, ½ t baking soda, 1½ t salt, and 1¾ c sugar into a large
bowlAdd 1 c shortening to the bowl
If [BRANCH] using butter, margarine, or lard, then
add 2/3 c milk to the bowl,
else
(for other shortenings) add 1 c minus 2 T of milk to the bowl
Add 1 t vanilla to the mixture in the bowl
If [BRANCH] mixing with a spoon, then
[SUBALGORITHM REFERENCE] see the instructions in the introduction to the chapter
on cakes,
else
(for electric mixers) beat the contents of the bowl for 2 minutes at medium speed,
scraping the bowl and beaters as needed
Add 3 eggs plus 1 extra egg yolk to the bowl
Melt 3 squares of unsweetened chocolate and add to the mixture in the bowl
Beat the mixture for 1 minute at medium speed
Pour the batter into the tube pan
Put the pan into the oven and bake for 1 hour and 10 minutes
[SUBALGORITHM REFERENCE] Perform the test for doneness described in the
introduction to the chapter on cakes
[LOOP] Repeat the test once each minute until the cake is done
Remove the pan from the oven and allow the cake to cool for 2 hours
[SUBALGORITHM REFERENCE] Follow the instructions for removing the cake from the
pan, given in the introduction to the chapter on cakes
Sprinkle powdered sugar over the cracks on top of the cake just before serving
5. Hardware: disk drive, memory, arithmetic/logic unit, mouse, and CPU
Software: compiler, editor, operating system, and object program
6. a. For means−ends analysis, you must know
1) the starting state.
2) the ending state.
3) the actions available to get from start to end.
b. The techniques can be combined by using divide−and−conquer to break up the actions into
1
pf3

Partial preview of the text

Download Programming and Problem Solving: Exercises and Algorithms and more Assignments Engineering in PDF only on Docsity!

Chapter 1 Overview of Programming and Problem Solving

Chapter 1

EXERCISE ANSWERS

Exam Preparation Exercises

  1. The steps keep repeating forever because there is no way for the algorithm to stop. (Also, it is a poor algorithm because the last action is lathering—you wouldn’t want to leave the lather in your hair.) Corrected algorithm:

(1) Wet hair. (2) Lather. (3) Rinse. (4) Repeat steps 2 and 3 once.

  1. Recipe for pound cake:

Preheat the oven to 350 degrees Line the bottom of a 9−inch tube pan with wax paper Sift 2¾ c flour, ¾ t cream of tartar, ½ t baking soda, 1½ t salt, and 1¾ c sugar into a large bowl Add 1 c shortening to the bowl If [BRANCH] using butter, margarine, or lard, then add 2/3 c milk to the bowl, else (for other shortenings) add 1 c minus 2 T of milk to the bowl Add 1 t vanilla to the mixture in the bowl If [BRANCH] mixing with a spoon, then [SUBALGORITHM REFERENCE] see the instructions in the introduction to the chapter on cakes, else (for electric mixers) beat the contents of the bowl for 2 minutes at medium speed, scraping the bowl and beaters as needed Add 3 eggs plus 1 extra egg yolk to the bowl Melt 3 squares of unsweetened chocolate and add to the mixture in the bowl Beat the mixture for 1 minute at medium speed Pour the batter into the tube pan Put the pan into the oven and bake for 1 hour and 10 minutes [SUBALGORITHM REFERENCE] Perform the test for doneness described in the introduction to the chapter on cakes [LOOP] Repeat the test once each minute until the cake is done Remove the pan from the oven and allow the cake to cool for 2 hours [SUBALGORITHM REFERENCE] Follow the instructions for removing the cake from the pan, given in the introduction to the chapter on cakes Sprinkle powdered sugar over the cracks on top of the cake just before serving

  1. Hardware: disk drive, memory, arithmetic/logic unit, mouse, and CPU Software: compiler, editor, operating system, and object program
  2. a. For means−ends analysis, you must know
    1. the starting state.
    2. the ending state.
    3. the actions available to get from start to end. b. The techniques can be combined by using divide−and−conquer to break up the actions into

smaller, simpler steps.

  1. One approach to finding a job, using the divide−and−conquer strategy, would be as follows:

Find a job Find information on available jobs Check newspaper. Call employment service. Apply for jobs Write résumé. Submit résumé to potential employers. Arrange interviews. Attend interviews. Make follow−up calls after promising interviews.

Each of these steps could be further subdivided.

Programming Warm−Up Exercises

  1. Algorithm to drive to airport:

From home, travel to I−10 interchange Get on I−10 eastbound If traffic heavy, Get on north loop Drive to Exit 23 Turn right on River Road Drive to airport entrance Turn left at airport entrance else Drive to Exit 18 Turn left on River Road Drive to airport entrance Turn right at airport entrance Search lot for parking space

  1. Algorithm to make a peanut butter and jelly sandwich:

Open refrigerator door by pulling on door handle Remove bread bag, jelly jar and peanut butter jar from refrigerator Place bread bag, jelly jar and peanut butter jar on counter Close refrigerator door Open bread bag by removing twist−tie Remove 2 bread slices from bread bag Place 2 bread slices on counter Close bread bag using twist−tie Open jelly jar by unscrewing lid Place jelly jar lid on counter Open drawer under counter Remove knife from drawer Close drawer Use knife blade to scoop desired amount of jelly out of jar Spread jelly on face−up side of one slice of bread Close jelly jar by screwing lid on Open peanut butter jar by unscrewing lid Place peanut butter jar lid on counter Use knife blade to scoop out desired amount of peanut butter Spread peanut butter on face−up side of other slice of bread Close peanut butter jar by screwing lid on