Try Magic Notes and save time.Try it free
Try Magic Notes and save timeCrush your year with the magic of personalized studying.Try it free
Question

Compare the two solutions to the shampooing algorithm shown. Which do you think is a better general-purpose solution? Why? (Hint: What if you wanted to wash your hair 1,000 times?)

StepOperation1Wet your hair2Set the value of WashCount to 03Repeat Steps 4 through 6 until the value of WashCount equals 24Lather your hair5Rinse your hair 6Add 1 to the value of WashCount7Stop, you have finished shampooing your hair\begin{array} {ll} \text{Step} & \text{Operation}\\ \text{1} & \text{Wet your hair}\\ \text{2} & \text{Set the value of WashCount to 0}\\ \text{3} & \text{Repeat Steps 4 through 6 until the value of WashCount equals 2}\\ \text{4} & \text{Lather your hair}\\ \text{5} & \text{Rinse your hair} & \text{ }\\ \text{6} & \text{Add 1 to the value of WashCount}\\ \text{7} & \text{Stop, you have finished shampooing your hair}\\ \end{array}

 Step  Operation 1 Wet your hair 2 Lather your hair 3 Rinse your hair 4 Lather your hair 5 Rinse your hair 6 Stop, your have finished shampooing your hair \begin{array}{ll}{\text { Step }} & {\text { Operation }} \\ {1} & {\text { Wet your hair }} \\ {2} & {\text { Lather your hair }} \\ {3} & {\text { Rinse your hair }} \\ {4} & {\text { Lather your hair }} \\ {5} & {\text { Rinse your hair }} \\ {6} & {\text { Stop, your have finished shampooing your hair }}\end{array}

Solution

Verified
Step 1
1 of 2

The algorithm from Fig 1.3 contains an iterative operation (repeating a number of steps) and a value to keep track of how many times the loop should be performed.

The Fig 1.4 algorithm simply has steps which look exactly the same and take extra space while not providing enough new information. One would only have to read the 3 steps and count how many times the same steps are written which is what WashCountWashCount is for in the second algorithm.

The first one is more efficient and elegant since it would look the same regardless of how many times the loop has to be performed. Only the value of WashCountWashCount would change.

Create an account to view solutions

Create an account to view solutions

Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface 5th Edition by David A. Patterson, John L. Hennessy

Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263David A. Patterson, John L. Hennessy
226 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
895 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848 (5 more)Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
849 solutions
Invitation to Computer Science 7th Edition by G Michael Schneider, Judith L. Gersting

Invitation to Computer Science

7th EditionISBN: 9781305075771G Michael Schneider, Judith L. Gersting
345 solutions

More related questions

1/4

1/7