15.1 What is Totalling?
Totalling is the algorithm of finding the sum of a set of values. An accumulator variable starts at 0 and each item's value is added to it.
- Accumulator — INTEGER (or REAL), initialised to 0 before the loop.
- Add value —
Total <- Total + Valueinside the loop. - Output — the final total is printed AFTER the loop.
- Use REAL — if values may have decimals (e.g. prices).
Key idea: counting adds 1 (Count <- Count + 1); totalling adds the value (Total <- Total + Value). The patterns look similar but accumulate different things.
// Sum the integers from 1 to 5
DECLARE Total : INTEGER
Total <- 0
FOR I <- 1 TO 5
Total <- Total + I
NEXT I
OUTPUT "Sum is ", TotalOutput: Sum is 15. Each iteration adds the value of I (1, then 2, then 3, ...) to the accumulator.
Concepts — What is Totalling?
15.2 The Totalling Pattern
The totalling pattern has four steps: initialise the accumulator to 0, loop over the items, add each item's value to the accumulator, and output the final total after the loop.
// General totalling pattern
DECLARE Total : INTEGER
Total <- 0 // 1. INITIALISE before the loop
FOR I <- 1 TO N
Total <- Total + Value(I) // 2. ADD each value (no IF needed)
NEXT I
OUTPUT "Total: ", Total // 3. OUTPUT after the loop- Initialise ONCE — accumulator <- 0 before the loop, never inside.
- Add unconditionally — basic totalling adds every value.
- Selective totalling — wrap the add in an IF for criteria (e.g. positives only).
- OUTPUT after loop — print the final total, not the running total.
Common mistake: writing Total <- 0 inside the loop. This resets the accumulator each pass — the final total will be just the last value, not the full sum.
Exam tip: if a question says "find the sum of" or "calculate the total", that is the totalling pattern. Look for the keyword "sum" or "total".
The Totalling Pattern — Step by Step
15.3 Totalling with Input
When the values come from the user, a sentinel loop reads them until a stop signal is entered. The accumulator adds each value inside the loop body.
// Sum the prices the user enters (0 to stop)
DECLARE Price : REAL
DECLARE Total : REAL
Total <- 0
INPUT Price // prime read
WHILE Price <> 0 DO
Total <- Total + Price
INPUT Price // read next value
ENDWHILE
OUTPUT "Total due: ", Total- Prime read — first INPUT before the WHILE, so the condition has a value to test.
- Inner INPUT — read the next value inside the loop body.
- Sentinel excluded — the stop value (0) is NOT added to the total.
- WHILE preferred — because the body may need to be skipped entirely.
Watch out: forgetting the inner INPUT creates an infinite loop — the same value is added forever. Always read the next value inside the loop body.
Exam tip: "Read prices until 0 is entered, then print the total" is a textbook sentinel totalling problem. Use REAL for the accumulator if prices have decimals.
Totalling with Input — Sentinel Loops
15.4 Totalling in Arrays
Arrays pair naturally with FOR loops for totalling. Loop through each element by index, add its value to the accumulator. For selective totalling, wrap the add in an IF.
Total ALL elements
DECLARE A : ARRAY[1:5] OF INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 1 TO 5
Total <- Total + A[I]
NEXT I
OUTPUT "Sum = ", TotalTotal only POSITIVE elements
DECLARE A : ARRAY[1:5] OF INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR I <- 1 TO 5
IF A[I] > 0 THEN
Total <- Total + A[I]
ENDIF
NEXT I
OUTPUT "Positive sum = ", Total- 1-based indexing — CIE arrays declared
ARRAY[1:N]start at index 1. - FOR I <- 1 TO N — visits every element exactly once.
- Add element by index —
Total <- Total + A[I]. - Selective totalling — wrap the add in an IF for criteria.
Pattern: the totalling pattern is the same whether you iterate an array, read input, or use a fixed FOR range. Only the loop type and value source change — the four steps stay constant.
Totalling in Arrays — Indexed Sum
15.5 Practical Examples
Three classic totalling problems: sum of a known range, sentinel-controlled sum of user input, and selective totalling of array elements.
Example 1 — Sum 1 to 10 (FOR Loop)
DECLARE Sum : INTEGER
Sum <- 0
FOR I <- 1 TO 10
Sum <- Sum + I
NEXT I
OUTPUT "Sum = ", Sum // outputs 55Each iteration adds the value of I to the accumulator. Final sum = 1 + 2 + ... + 10 = 55.
Example 2 — Sentinel Sum of User Input
DECLARE V, Sum : INTEGER
Sum <- 0
INPUT V
WHILE V <> -1 DO
Sum <- Sum + V
INPUT V
ENDWHILE
OUTPUT "Sum = ", SumReads values until the user enters -1. Each value is added to Sum; -1 itself is NOT added (it is the sentinel).
Example 3 — Single-Pass Average (Total + Count)
DECLARE Mark : ARRAY[1:10] OF INTEGER
DECLARE Total, Count : INTEGER
DECLARE Avg : REAL
Total <- 0
Count <- 0
FOR I <- 1 TO 10
Total <- Total + Mark[I] // totalling
Count <- Count + 1 // counting
NEXT I
Avg <- Total / Count
OUTPUT "Average = ", AvgTotalling and counting run side-by-side in one loop. After the loop, divide to get the average. Use REAL for Avg so the division is not truncated.
Watch out: if both Total and Count are INTEGER, Total / Count in CIE pseudocode may be integer division. Store the result in a REAL variable to preserve any decimal part.
Practical Examples — Apply Totalling
15.6 Key Points Summary
A quick recap of everything covered on totalling:
- Purpose — Find the sum of a set of values.
- Initialise before loop — Total <- 0 BEFORE the loop, never inside.
- Add the value — Total <- Total + Value (not + 1 like counting).
- Output after loop — Print the final total AFTER the loop ends.
- Selective totalling — Wrap the add in an IF for criteria (e.g. positives).
- Sentinel excluded — The stop value is NOT added to the total.
- Arrays use FOR — FOR I <- 1 TO N to visit every element by index.
- Average = Total / Count — Totalling + counting in one loop gives the average.
Exam tip: look for the keywords "sum" or "total". That always means totalling. Don't confuse it with counting, which uses + 1 rather than + Value.