CIE IGCSE | 7.4.4 Finding Maximum, Minimum and Average Values

Lesson objective

Find maximum and minimum values using comparisons, calculate an average using a total and count, and trace a complete algorithm.

Learn

7.4.4 | FINDING MAXIMUM, MINIMUM AND AVERAGE VALUES

01 | WHAT DO THESE THREE VALUES TELL US?

A weather station records five temperatures during a day: 4, 7, 2, 9 and 3 degrees. You could display every reading, but a short summary helps someone understand the range of conditions.

The maximum is the greatest value: 9. The minimum is the smallest value: 2. The average in this lesson means the arithmetic mean: add the values and divide by how many there are.

The total is 4 + 7 + 2 + 9 + 3 = 25. There are five readings, so the average is 25 ÷ 5 = 5.

These summaries answer different questions. The average is not necessarily one of the original values: no reading here was 5. It is also not necessarily halfway between the maximum and minimum.

We will first understand each calculation, then combine them in one precise algorithm.

02 | FIND A MAXIMUM ONE VALUE AT A TIME

Imagine looking through the readings while keeping a card labelled “Highest so far”. Start by writing the first reading, 4, on the card.

Compare the next reading, 7, with the current highest. Seven is greater than four, so replace the candidate with 7. Compare 2 with 7: it is smaller, so keep 7. Nine replaces 7, and the final 3 leaves 9 unchanged.

The card always represents the greatest value seen so far. A program can use a variable called Maximum for the same purpose.

Keep the greatest value seen so far
ReadingActionMaximum afterwards
4Initial candidate4
77 > 4: replace7
22 > 7 is false: retain7
99 > 7: replace9
33 > 9 is false: retain9

This method does not sort the readings. It only retains the candidate needed for the greatest-value result. When every reading has been considered, the candidate is the overall maximum.

03 | FIND A MINIMUM USING THE OPPOSITE COMPARISON

For a minimum, begin with the first reading again, but keep the smallest value seen so far. Replace the candidate only when a new reading is smaller.

Start at 4. Seven does not replace it. Two is smaller and becomes the new minimum. Nine and three do not replace two.

Keep the smallest value seen so far
ReadingActionMinimum afterwards
4Initial candidate4
77 < 4 is false: retain4
22 < 4: replace2
99 < 2 is false: retain2
33 < 2 is false: retain2

The update condition is Value < Minimum, whereas the maximum condition is Value > Maximum.

Equal values do not need to replace the candidate when you only want the numeric result. If the task asks which occurrence has the minimum or maximum, you also need to track an index or other identifying information and define how ties are handled.

04 | WHY START WITH A REAL INPUT?

Starting Maximum at zero may appear convenient, but it is wrong for some valid data. Suppose the readings are −6, −2 and −8. None is greater than zero, so a zero-initialised maximum would remain zero even though zero was never entered.

The correct maximum is −2. Starting with the first reading, −6, allows the next greater reading, −2, to replace it.

Starting Minimum at zero also fails for an all-positive set such as 4, 7 and 2. No reading is smaller than zero, so it would report a value that is not in the set.

A safe general method for a non-empty input sequence is to set both candidates to the first actual value. That value is already the maximum and minimum of the one-item set seen so far.

An extreme starting constant can work only if it is chosen using guaranteed data limits. Do not guess a “very large” or “very small” number without a justified range.

05 | AVERAGE NEEDS THE WHOLE TOTAL AND THE RIGHT COUNT

The mean is total of the included values ÷ number of included values. The total and count must refer to the same set.

For 4, 7, 2, 9 and 3, the running total becomes 4, 11, 13, 22 and 25. The final count is five, giving an average of 5.

Do not divide only the last reading by five. Do not divide the total by four because four further inputs followed the first: there are five inputs altogether.

An average can have a fractional part even when every input is a whole number. For readings 2, 3 and 5, the mean is 10 ÷ 3. Use a suitable REAL value and ordinary division rather than integer division that discards the fractional part.

Rounding is a separate requirement. If the task does not ask for a particular number of decimal places, do not silently round every input or intermediate total.

06 | GIVE EACH VARIABLE A CLEAR ROLE

We will process five valid numeric readings. The first is input before the loop; the remaining four are processed inside it.

Values may be fractional; occurrence counts are whole numbers
VariableTypeRole
ValueREALCurrent reading
Maximum / MinimumREALExtreme candidates
Total / AverageREALAccumulated sum / final mean
ReadingCountINTEGERNumber of included readings
IndexINTEGERControls remaining input repetitions

Value holds the current reading. Maximum and Minimum retain the greatest and smallest values seen so far. Total accumulates all readings. ReadingCount tells us how many were included.

Average is calculated only after the collection is complete. Index controls the four remaining input repetitions.

The worked example assumes exactly five valid numeric values. It does not implement input validation or an empty collection. Stating this scope keeps the algorithm understandable.

07 | BUILD THE STARTING STATE CAREFULLY

Before receiving readings, set Total to 0.0 and ReadingCount to 0. Then receive the first Value.

Set Maximum and Minimum to that first reading. Add it to Total and increase ReadingCount to one.

Total ← 0.0
ReadingCount ← 0
INPUT Value
Maximum ← Value
Minimum ← Value
Total ← Total + Value
ReadingCount ← ReadingCount + 1

The first input must not be forgotten in the total or count just because it was used to initialise the candidates. Each reading contributes once.

After this starting section, all four summaries correctly describe the one reading processed so far: it is both the largest and smallest; Total equals it; and the count is one.

08 | PROCESS EACH REMAINING READING

Receive the next Value. Compare it with Maximum and update Maximum if it is greater. Separately compare it with Minimum and update Minimum if it is smaller.

INPUT Value
IF Value > Maximum
  THEN
    Maximum ← Value
ENDIF
IF Value < Minimum
  THEN
    Minimum ← Value
ENDIF
Total ← Total + Value
ReadingCount ← ReadingCount + 1

The two IF statements make the two responsibilities explicit. The total and count updates happen for every reading, not only for a new extreme.

For example, the final reading 3 is neither a new maximum nor a new minimum, but it must still contribute to the total and count. Putting those updates inside only the maximum branch would exclude ordinary readings and produce an incorrect average.

This fragment belongs inside the loop for the remaining four readings. Its variables have already been declared and initialised by the surrounding design.

09 | READ THE COMPLETE ALGORITHM

Combine the initial input with four further repetitions and calculate the mean after all five readings have been included.

DECLARE Value : REAL
DECLARE Maximum : REAL
DECLARE Minimum : REAL
DECLARE Total : REAL
DECLARE Average : REAL
DECLARE ReadingCount : INTEGER
DECLARE Index : INTEGER

// Assume exactly five valid numeric readings
Total ← 0.0
ReadingCount ← 0
INPUT Value
Maximum ← Value
Minimum ← Value
Total ← Total + Value
ReadingCount ← ReadingCount + 1
FOR Index ← 2 TO 5
    INPUT Value
    IF Value > Maximum
      THEN
        Maximum ← Value
    ENDIF
    IF Value < Minimum
      THEN
        Minimum ← Value
    ENDIF
    Total ← Total + Value
    ReadingCount ← ReadingCount + 1
NEXT Index
Average ← Total / ReadingCount
OUTPUT "Maximum: ", Maximum
OUTPUT "Minimum: ", Minimum
OUTPUT "Average: ", Average

The loop runs from Index 2 to 5 inclusive: four repetitions. Together with the initial INPUT, that gives five readings.

The algorithm can find all three summaries in one traversal of the input sequence. It does not need to sort the data or retain every reading in an array for these specific results.

Its final output communicates the summaries. If a separate requirement asks to keep the original readings for later analysis, add appropriate storage; the summary variables alone do not preserve the full sequence.

10 | TRACE ALL THE VALUES TOGETHER

Follow the worked data 4, 7, 2, 9 and 3. Record the summary state after each reading.

State after each reading; average calculated afterwards
ReadingMaximumMinimumTotalReadingCount
44441
774112
272133
992224
392255

After reading three, Maximum is still 7, Minimum is 2, Total is 13 and ReadingCount is 3. The current summaries describe the first three readings, not the entire set yet.

After the last reading, the values are Maximum 9, Minimum 2, Total 25 and ReadingCount 5. Only now does the algorithm calculate Average as 25 ÷ 5 = 5.

Use a trace to locate a fault rather than guessing from the final output. If the count ends at four, check whether the first input was included. If Total ends at the last value, look for an unwanted reset inside the loop.

11 | NEGATIVE VALUES, EQUAL VALUES AND ONE VALUE

For −6, −2 and −8, the correct maximum is −2, minimum is −8 and average is −16 ÷ 3, approximately −5.33. Numeric order matters: −2 is greater than −6 even though its digit is smaller.

For 7, 7 and 7, both maximum and minimum are 7, and the mean is 7. Equal comparisons cause no candidate update, but every occurrence still contributes to the total and count.

A general algorithm receiving one value would have that value as its maximum, minimum and mean. There would be no remaining readings to compare.

These examples illustrate the method on different collection sizes. The complete worked pseudocode on this page is fixed at five inputs; adapting its loop bounds is necessary when the required number changes.

12 | WHAT IF THERE ARE NO VALUES?

A maximum and minimum need at least one value. An empty collection does not have an ordinary greatest or smallest member to report.

An average also cannot be calculated using a count of zero because division by zero is undefined.

Our fixed-five-input algorithm avoids this situation by requiring five readings. A variable-length or sentinel-controlled design must handle it explicitly.

For example, after input finishes, check ReadingCount. If it is zero, output “No readings supplied”. Otherwise output the extremes and calculate Total / ReadingCount. Initialise the extreme candidates when the first valid reading arrives, not from an arbitrary zero.

The absence of data should be communicated as a distinct result, not disguised as a genuine maximum, minimum or average of zero.

13 | USE THE RIGHT SET FOR CONDITIONAL AVERAGES

Suppose a teacher wants the average of scores at least five. For scores 4, 5, 8, 2 and 5, only 5, 8 and 5 belong in that calculation.

The qualifying total is 18 and the qualifying count is 3, so their mean is 6. Dividing by all five inputs would give 3.6, which is not the mean of the qualifying set.

If no scores qualify, do not divide by zero. Report that there are no qualifying scores. If you also want extremes for the qualifying set, initialise them from the first qualifying value.

This extends the previous lesson: the updates and the final divisor must refer to the same group. Read the requirements carefully before deciding which inputs contribute.

14 | TEST THE ALGORITHM AND EXPLAIN WHY IT WORKS

Useful tests include a mixed set, all-negative values, all-equal values and a set where the largest or smallest arrives last. These reveal different mistakes in initialisation, comparison and inclusion.

Tests for the fixed-five-input algorithm
Five readingsMaximumMinimumAverage
4, 7, 2, 9, 3925
-6, -2, -8, -4, -5-2-8-5
7, 7, 7, 7, 7777
2, 3, 4, 5, 111125

For a describe question, explain the initial candidate, comparisons, updates and final output. For an explain question, connect these to their purposes.

A useful response is: “Maximum is set to the first input because that gives a candidate from the actual data. Each later value replaces it only if greater, therefore it retains the greatest value seen so far.”

For the mean, say why the calculation is after the loop: the total and count must include every required reading before division. You should now be able to build and trace the three summaries without confusing their roles.

MATCH THE SUMMARY TO ITS ROLE

Terminology

Terminology

Maximum

The greatest value in the set being considered.

Minimum

The smallest value in the set being considered.

Arithmetic mean

The sum of included values divided by how many values are included.

Candidate

The current value retained as the possible extreme.

Initialisation

Assigning a suitable starting value before processing.

First-value initialisation

Using the first actual input as the initial maximum and minimum.

Running total

The sum of values processed so far.

Reading count

The number of readings included in the total.

Comparison

Checking the relationship between a new value and the current candidate.

Conditional replacement

Updating a candidate only when the comparison warrants it.

REAL

A numeric type capable of holding a fractional part.

INTEGER

A whole-number type suitable for the reading count.

Division by zero

An undefined calculation prevented by checking that the divisor is non-zero.

Empty collection

A set with no values; ordinary extrema and mean are not available.

Qualifying set

The values meeting a stated inclusion condition.

Trace

A step-by-step record of changing values during execution.

Tie

Equal values for an extreme; tracking positions requires a defined tie rule.

Rounding

Adjusting a numeric result to a specified precision when required.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. For 4, 7, 2, 9, 3, which are correct?
2. Why initialise extreme candidates from the first input?
3. Which updates are correct?
4. Which statements about averages are correct?
5. The first input occurs before FOR Index ← 2 TO 5. How many inputs altogether?
6. Why update Total and ReadingCount for ordinary non-extreme readings too?
7. Which are correct for an empty collection?
8. Scores at least five from 4, 5, 8, 2, 5 have which mean?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Define maximum, minimum and arithmetic mean.

2. Explain why setting Maximum to zero fails for -6, -2 and -8.

3. Describe how a minimum is found without sorting.

4. Explain why the first input must be included in Total and ReadingCount.

5. Explain why average calculation occurs after the input loop.

6. Explain why counts normally use INTEGER while Average uses REAL.

7. Suggest how to handle an unknown-length sequence containing no readings.

8. Explain why a qualifying average must use a qualifying count.

TRACE IT | ALL THREE SUMMARIES

Trace the complete five-reading algorithm with inputs −3, 6, −7, 2 and 2. Record Maximum, Minimum, Total and ReadingCount after each input, then calculate Average. Explain why the repeated two is still included.

Reveal the worked trace
Final maximum 6, minimum -7 and average 0
ValueMaximumMinimumTotalCount
-3-3-3-31
66-332
-76-7-43
26-7-24
26-705

The average is 0 / 5 = 0. Both occurrences of two are readings, so both contribute to total and count even though neither changes the extremes.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 7.4.4 Finding Maximum, Minimum and Average Values is coming soon. Complete the matching task and written questions in your exercise book.