Discrete Mathematics and Its Applications, Seventh Edition

Published by McGraw-Hill Education
ISBN 10: 0073383090
ISBN 13: 978-0-07338-309-5

Chapter 3 - Section 3.1 - Algorithms - Exercises - Page 203: 19

Answer

procedure getvalues ($a_1, a_2, a_3:$ three integers) bubblesort($a_1, a_2, a_3$) {Bubblesort was defined in the chapter - Any sorting algorithm would do} min $:= a_1$ max $:= a_3$ median $:= a_2$ mean $:= \frac{a_1+a_2+a_3}{3}$ return {max, median, mean, min} {We return a set consisting of the four values that were asked for}

Work Step by Step

This algorithm attempts to calculate the minimum, maximum, median and mean for three integers. By first sorting the array, we cut the amount of work we have to do substantially. By sorting the array, we know that the minimum value is the first; the median is the second (middle value), and the maximum is the last. To calculate the mean, we just sum up all the values and divide by 3. We return the four values as a set.
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.