Example 58: Mean, Median, and Mode
Learn how to calculate the mean, median, and mode of an array of numbers.
We'll cover the following
Introduction
Write a function that takes an array of 5 numbers and the result array as input and calculates the mean, median, and mode values.
Remember to store these values in the result
array passed as a parameter to the function. It should store the values such that the mean value should be stored at index 0, followed by the median value at index 1, and the mode value at index 2.
Examples
Input | Output (mean, median, mode) |
---|---|
1, 3, 5, 5, 4 | 3, 4, 5 |
10, 12, 22,32,10 | 17, 12, 10 |
-1, 4,3,3,-2 | 1, 3, 3 |
Try it yourself
Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
Get hands-on with 1400+ tech skills courses.