A weather station records a temperature every hour. At the end of the day it must report the highest, the lowest and the average temperature. It does not need to remember every reading to do so. A handful of variables, updated as each value arrives, is enough.
Processing in One Pass
Definition: Single-pass processing. Computing the required results by examining each value of a sequence exactly once, as it arrives, without storing the whole sequence.
Four running quantities are maintained: the sum, the count, the maximum so far and the minimum so far. Table 3.7 shows how each is started and updated.
Table 3.7: Running variables for statistics over a sequence
| Variable | Starting value | Update for each value x |
|---|---|---|
| count | 0 | count = count + 1 |
| sum | 0 | sum = sum + x |
| max | The first value | if x > max then max = x |
| min | The first value | if x < min then min = x |
The average is not updated on each pass. It is computed once, at the end, as sum divided by count.
Starting Values for Maximum and Minimum
It is tempting to start max at 0. That fails when every value is negative: for the temperatures −4, −9 and −2, the program would report a maximum of 0, a value that was never entered. The safe rule is to start both max and min with the first actual value of the sequence. Every later value is then compared with real data.
Sentinel-Controlled Input
The number of values is usually not known in advance, so the input is ended by a sentinel, as studied in Unit II. The sentinel is a signal only. It must not be counted, added or compared.
The Empty Sequence
If the very first value entered is the sentinel, there is no data. The count is 0, and dividing by it would be an error. The algorithm must detect this case and report that no data was entered.
Integer and Real Division
When sum and count are both integers, sum / count discards the fractional part: 14 / 4 gives 3, not 3.5. To obtain the true average, one operand is converted to a real type, for example (float)sum / count.
Why Few Variables Matter
This method uses the same small number of variables whether ten values are entered or ten million. A method that stored every value would need memory in proportion to the amount of data. For the four statistics here, storing the data is unnecessary.
Example 3.7: Statistics of marks
Problem: Read marks until −1 is entered. Display the maximum, minimum, sum and average.
Solution:
- Read the first mark. If it is −1, report that there is no data.
- Otherwise start max and min with it, and process marks in a loop until −1 appears.
- Compute the average with real division after the loop.
#include <stdio.h>
int main(void)
{
int x, max, min, sum = 0, count = 0;
printf("Enter marks (-1 to stop):\n");
scanf("%d", &x);
if (x == -1)
{
printf("No data entered\n");
return 0;
}
max = x;
min = x;
while (x != -1)
{
sum = sum + x;
count++;
if (x > max)
{
max = x;
}
if (x < min)
{
min = x;
}
scanf("%d", &x);
}
printf("Maximum = %d\n", max);
printf("Minimum = %d\n", min);
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", (float)sum / count);
return 0;
}
Input:
45 72 38 91 -1
Output:
Enter marks (-1 to stop):
Maximum = 91
Minimum = 38
Sum = 246
Average = 61.50
Result: Four marks were processed. The sum is 246 and the average is 246 / 4 = 61.50. The sentinel −1 ended the loop without being included.
Common mistakes
- Starting max or min at 0 instead of the first data value.
- Including the sentinel in the sum, the count or the comparisons.
- Dividing two integers for the average, or dividing when the count is 0.
Key takeaways
- Sum, count, maximum and minimum can all be maintained in one pass.
- max and min start from the first actual value.
- The sentinel ends the input and is never treated as data.
- The average is sum divided by count, using real division, and only when count is not 0.
Practice questions
- What are the maximum, minimum and average of 4, 7 and 1?
- Why should max not be started at 0?
- What must a program do if the first value entered is the sentinel?
Answers to the practice questions
- Maximum 7, minimum 1, average 12 / 3 = 4.
- If all the values are negative, 0 would wrongly be reported as the maximum although it was never entered.
- Report that no data was entered and avoid dividing by a count of 0.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
