Skip to main content

9.7) Exercise 16


Copy your solution to Exercise 11 to a new file. Add to this new program some code that will compute and display the median of the values in your vector. Use an algorithm to help perform the calculation.

The median is defined as the middle value from a sorted collection of values. If there is an odd number of values, this middle value will be one of the values from the sorted collection. If there is an even number of values in the collection, the median is defined as the average of the two values either side of the midpoint.

For example, if we have the values [4, 1, 3, 2], the sorted collection is [1, 2, 3, 4]. The midpoint lies between 2 and 3, so we take the median to be the average of these two values - i.e., 2.5.