Unable to open this exercise
Common exam pitfall
Students often initialize accumulators inside the loop, resetting the total back to zero on every single iteration.
Two of the most frequently examined standard algorithmic operations in Cambridge IGCSE Computer Science are Totalling (accumulating sums) and Counting (tracking occurrences).
1. Totalling vs Counting
Characteristic | Totalling (Accumulator) | Counting (Counter) |
|---|---|---|
Purpose | Computes the running sum of a series of numerical inputs. | Tracks how many times an event occurred or items processed. |
Typical Initialization |
|
|
Update Assignment |
|
|
Value Added | Variable: Changes with each data item entered. | Constant: Always increments by a fixed step (usually 1). |
2. Sentinel Values and Rogue Values
When the exact number of items to be entered is unknown in advance, a Sentinel Value (also called a rogue value) is used to terminate input:
- Common examples:
-1(for positive numbers),999,"XXX", or"STOP". - Exam Rule: The sentinel value must never be included in the calculation or added to the counter!
The Priming Read Pattern:
To prevent the sentinel value from polluting the accumulator, developers use the "priming read" pattern:
Total <- 0
Count <- 0
INPUT Value // 1. Priming read before loop starts
WHILE Value <> -1 DO // 2. Test condition BEFORE processing
Total <- Total + Value
Count <- Count + 1
INPUT Value // 3. Read subsequent value at end of loop body
ENDWHILE
If the user enters -1 immediately on the very first prompt, the WHILE condition evaluates to FALSE, the loop never executes, and Total correctly remains 0.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Exam-Style Question: Trace Initialisation and Accumulator Logic
Fill in the missing values and variable names to complete the algorithm that totals 20 student exam scores:
ExamTotal <- [Space 1]
FOR i <- 1 TO [Space 2]
INPUT Score
ExamTotal <- ExamTotal + [Space 3]
NEXT i
Interactive exercises for Module 2 and beyond require an active plan.