The mean alone can mislead. If nine workers earn 20,000 rupees and the owner earns 5,00,000, the mean income of 68,000 describes nobody. Other measures, the median, the mode and the range, complete the picture of a set of data.
The Measures
Definition: Median. The middle value of a set of data after it has been arranged in order; for an even number of values, the mean of the two middle values.
Definition: Mode. The value that occurs most often in a set of data.
Definition: Range. The difference between the largest and the smallest value in a set of data.
The mean and the median describe the centre of the data. The mode describes the most typical value. The range describes how widely the data is spread.
Finding the Median
The data must first be sorted. If the n sorted values are in an array with indices 0 to n − 1:
- when n is odd, the median is the element at index n / 2;
- when n is even, the median is the mean of the elements at indices n / 2 − 1 and n / 2.
Take an array with n = 4 whose elements at indices 1 and 2 are both 4. The median is their mean, (4 + 4) / 2 = 4. For an array with n = 5 whose element at index 2 is 5, the median is that element, 5.
Finding the Mode
The mode is found by counting how many times each value occurs and keeping the value with the highest count. For each element a[i], an inner loop counts the elements equal to it. If that count exceeds the best count so far, the element becomes the mode.
A set of data may have one mode, several modes (when two or more values share the highest count) or no mode (when every value occurs once). A program must state how it treats these cases. A simple and common choice is to report the first value that reaches the highest count.
Finding the Range
The range is max − min. In a sorted array it is simply the last element minus the first: a[n - 1] - a[0].
Comparing the Measures
Table 4.5 applies all four measures to the same data.
Table 4.5: Measures for the data 2, 4, 4, 9
| Measure | Method | Value |
|---|---|---|
| Mean | (2 + 4 + 4 + 9) / 4 | 4.75 |
| Median | Mean of the two middle values, 4 and 4 | 4 |
| Mode | 4 occurs twice, more than any other value | 4 |
| Range | 9 − 2 | 7 |
The mean is pulled towards extreme values, while the median is not. For the incomes mentioned at the start, the median is 20,000, which describes the typical worker far better than the mean.
Example 4.5: Median, mode and range
Problem: For the sorted array 2, 4, 4, 9, find the median, the mode and the range.
Solution:
- The array is already sorted, so the median is taken from the middle position or positions.
- For the mode, count the occurrences of each value with nested loops.
- The range is the last element minus the first.
#include <stdio.h>
int main(void)
{
int a[4] = {2, 4, 4, 9};
int n = 4, i, j, count, best = 0, mode = a[0];
float median;
if (n % 2 == 1)
{
median = a[n / 2];
}
else
{
median = (a[n / 2 - 1] + a[n / 2]) / 2.0f;
}
for (i = 0; i < n; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
if (a[j] == a[i])
{
count++;
}
}
if (count > best)
{
best = count;
mode = a[i];
}
}
printf("Median = %.2f\n", median);
printf("Mode = %d (occurs %d times)\n", mode, best);
printf("Range = %d\n", a[n - 1] - a[0]);
return 0;
}
Output:
Median = 4.00
Mode = 4 (occurs 2 times)
Range = 7
Result: The results agree with Table 4.5. Because the test is count > best, the first value that reaches the highest count is reported when several values tie.
Common mistakes
- Taking the middle element of an unsorted array as the median.
- Forgetting to average the two middle values when n is even.
- Assuming that every set of data has exactly one mode.
Key takeaways
- The median is the middle value of sorted data, or the mean of the two middle values.
- The mode is the most frequent value; there may be one, several or none.
- The range is the maximum minus the minimum.
- The data must be sorted before the median is taken by position.
Practice questions
- Find the median of 7, 3, 9, 1, 5.
- Find the mode and the range of 6, 2, 6, 8, 3.
- Find the median of 10, 20, 30, 40.
Answers to the practice questions
- Sorted, the data is 1, 3, 5, 7, 9, and the median is 5.
- The mode is 6 and the range is 8 − 2 = 6.
- (20 + 30) / 2 = 25.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
