Unable to open this exercise
Common exam pitfall
Students often confuse totalling (adding variable amounts) with counting (incrementing by 1 each time an event occurs).
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 Check: Distinguish Totalling from Counting
• Multiple ChoiceA cashier system records transactions. An algorithm uses two variables: Count and RunningTotal. Which pair of statements correctly updates these two variables each time an item with price ItemPrice is scanned?
Interactive exercises for Module 2 and beyond require an active plan.