Common Algorithms

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.

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 valueTotal <- Total + Value inside 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 ", Total

Output: Sum is 15. Each iteration adds the value of I (1, then 2, then 3, ...) to the accumulator.

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".

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.

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 = ", Total

Total 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 indexTotal <- 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.

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 55

Each 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 = ", Sum

Reads 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 = ", Avg

Totalling 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.

15.6 Key Points Summary

A quick recap of everything covered on totalling:

  • PurposeFind the sum of a set of values.
  • Initialise before loopTotal <- 0 BEFORE the loop, never inside.
  • Add the valueTotal <- Total + Value (not + 1 like counting).
  • Output after loopPrint the final total AFTER the loop ends.
  • Selective totallingWrap the add in an IF for criteria (e.g. positives).
  • Sentinel excludedThe stop value is NOT added to the total.
  • Arrays use FORFOR I <- 1 TO N to visit every element by index.
  • Average = Total / CountTotalling + 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.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

What does the totalling algorithm compute?

Question 2True / False

The accumulator variable must be initialised to 0 BEFORE the loop begins.

Question 3Multiple Choice

What is the final value of Sum?

DECLARE Sum : INTEGER
Sum <- 0
FOR I <- 1 TO 4
  Sum <- Sum + I * 2
NEXT I
OUTPUT Sum

Question 4True / False

In the basic totalling pattern, the addition happens unconditionally — no IF is required.

Question 5Multiple Choice

Which statement correctly adds the value of Mark to the accumulator Total?

Question 6True / False

In a sentinel totalling loop, the sentinel value itself is added to the total before the loop exits.

Question 7Multiple Choice

What is the final value of Sum?

DECLARE V, Sum : INTEGER
Sum <- 0
INPUT V
WHILE V <> -1 DO
  Sum <- Sum + V
  INPUT V
ENDWHILE
OUTPUT Sum
// Inputs: 10, 20, 30, -1

Question 8True / False

When totalling elements of an array, you should use a FOR loop from 1 to N (where N is the array size).

Question 9Multiple Choice

What is wrong with this totalling code?

FOR I <- 1 TO 5
  Sum <- 0
  Sum <- Sum + A[I]
NEXT I
OUTPUT Sum

Question 10True / False

To compute an average, you can use a single loop that updates both Total (Total + Value) and Count (Count + 1).

Question 11Multiple Choice

Which problem BEST fits the totalling algorithm?

Question 12True / False

Selective totalling uses an IF inside the loop to add only some items (e.g. only positive values) to the accumulator.

Answer all 12 questions to enable submission.