Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 1 - Exercises - Page 40: 13

Answer

The algorithm produced by the student would work, provided that all three of the numerical values are different. This could be solved by using the "more or equal" sign. It is still inefficient. We only need 2 comparisons. Input: $x, y, z$ Output: Largest Algorithm: Step 1 Check if $(x \geq y) .$ If yes, go to Step $2,$ otherwise to Step $3 .$ Step 2 Check if $(x \geq z) .$ If yes then Largest $=x .$ Otherwise Largest $=z$ . Step 3 Check if $(y \geq z)$ . If yes then Largest $=y .$ Otherwise Largest $=z$ . Step 4 Print out the Largest.

Work Step by Step

The algorithm produced by the student would work, provided that all three of the numerical values are different. This could be solved by using the "more or equal" sign. This way, when comparing 2 equal numbers, any of them could be used for the other comparison, since the value would be the same. The problem, however, is that it would still be too long. We should be able to find the largest number by performing no more than 2 comparisons. Here is what a better solution would be: Input: $x, y, z$ Output: Largest Algorithm: Step 1 Check if $(x \geq y) .$ If yes, go to Step $2,$ otherwise to Step $3 .$ Step 2 Check if $(x \geq z) .$ If yes then Largest $=x .$ Otherwise Largest $=z$ . Step 3 Check if $(y \geq z)$ . If yes then Largest $=y .$ Otherwise Largest $=z$ . Step 4 Print out the Largest.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.