Learning Objectives
  • Implement standard accumulation patterns.
  • Distinguish a total from a count.
  • Find maximum and minimum values safely.
  • Calculate an average after processing data.
Key Terms
Accumulator
A variable that stores a running result, commonly a total.
Counter
A variable that records how many times an event occurs.
Running total
A total updated as each new value is processed.
Maximum
The greatest value processed.
Minimum
The least value processed.
Average
The total divided by the number of values.
Summary diagram
Summary Of The Main Ideas In This Lesson
Totalling

A total accumulates values. It is normally initialised to zero before a loop and updated using Total <- Total + Value. The previous total must be retained, so the update belongs inside the loop and the initialisation outside.

A total may add every value or only values meeting a condition. For example, TotalPassMarks increases only when Mark >= 50.

The total data type should accommodate the possible result. A total of REAL measurements should normally be REAL.

Counting

A counter records occurrences rather than adding the data values. It is initialised to zero and increased by one when the relevant event occurs.

For example, PassCount <- PassCount + 1 counts passing students. It does not add their marks. A loop variable also counts iterations, but a separate conditional counter is needed when only some values qualify.

Counters are INTEGER because they represent whole occurrences.

Maximum And Minimum

A maximum is updated when a new value is greater than the current maximum. A minimum is updated when a new value is less than the current minimum.

Initialisation is critical. The safest approach is often to use the first input value as both Maximum and Minimum, then process the remaining values. Initialising Maximum to zero fails when all valid data can be negative.

When processing an array, initialise from the first element and loop from the second element.

Average

Average is calculated after totalling: Average <- Total / Count. The count must be greater than zero, and Average should normally be REAL.

Do not repeatedly divide the running total inside the loop unless a specific running average is required. For the syllabus standard method, collect the total and count, then divide once.

If some values are excluded by a condition, divide by the number included, not by the total number entered.

Combined Pattern

This pattern assumes -1 is a sentinel outside the valid data. The sentinel is input after the current value has been processed and is not included in the total or count. A complete solution should also handle the possibility of immediate sentinel input if no data is allowed.

Total <- 0
Count <- 0
INPUT Value
Maximum <- Value
Minimum <- Value
REPEAT
  Total <- Total + Value
  Count <- Count + 1
  IF Value > Maximum THEN Maximum <- Value ENDIF
  IF Value < Minimum THEN Minimum <- Value ENDIF
  INPUT Value
UNTIL Value = -1
Average <- Total / Count
Pattern Comparison
Task Initial Value Update
Total 0 Total <- Total + Value
Count 0 Count <- Count + 1
Maximum First data value If Value > Maximum, replace Maximum
Minimum First data value If Value < Minimum, replace Minimum
Average Calculated after loop Total / Count
Worked Examples
Count Passing Marks

Question: For ten marks, find the total and number that are at least 50.

  1. Initialise Total and PassCount to zero.
  2. Use a FOR loop for ten inputs.
  3. Add every mark to Total.
  4. Increase PassCount only when Mark >= 50.

Answer: The total is accumulated for all marks; PassCount is conditionally incremented for passing marks.

All Negative Temperatures

Question: Why is Maximum <- 0 incorrect when all temperatures may be negative?

  1. No negative value is greater than zero.
  2. Maximum would remain zero even though zero was never input.
  3. Initialise Maximum from the first actual temperature.

Answer: Using the first data value prevents an invented value from becoming the result.

Examination Guidance
  • Initialise totals and counts outside loops.
  • Use the first real value for maximum and minimum when the range is not safely bounded.
  • Divide by the correct count.
  • Protect against division by zero.
  • Distinguish adding values from counting events.
Common Mistakes
  • Setting Total to zero inside the loop.
  • Adding one to a total instead of adding the data value.
  • Adding the mark to PassCount.
  • Initialising Maximum to zero without considering negative data.
  • Dividing by a fixed number when only some data is included.
Knowledge Check

1. How is a running total updated?

Answer: Total <- Total + Value.

2. How is a counter updated?

Answer: Count <- Count + 1.

3. What is a safe maximum initialisation?

Answer: The first actual data value.

4. When is average usually calculated?

Answer: After the loop, using Total / Count.

5. Why check Count > 0?

Answer: To avoid division by zero.