Paper 2 scenario practice
Validating ages from a survey
The scenario
A health researcher is collecting age data from people at a community event. They want a program that:
1. Repeatedly INPUTS an age.
2. If the age is -1, the program STOPS asking (this is the SENTINEL for 'no more entries').
3. Otherwise, the age must be valid: an INTEGER between 0 and 120 inclusive.
- If invalid (e.g. -5, 200), output a rejection message AND ask for another age.
- If valid, store it for the average calculation.
4. After the user enters -1, output:
- The COUNT of valid ages collected.
- The AVERAGE of the valid ages (only if count > 0; otherwise output a message saying no valid ages were entered).
Write a program in pseudocode (or a high-level language of your choice). Marks will be awarded for input handling, validation logic, sentinel detection, calculation, and clear program structure.
Marking scheme — total 15 marks
Cambridge IGCSE 0478 Paper 2
- 2Initialise running totals: DECLARE the variables (total, count, age) with sensible types. Initialise total ← 0 and count ← 0 BEFORE the loop.
- 3Loop with sentinel: REPEAT/UNTIL or WHILE that reads an age each iteration and stops when age = -1. The sentinel test must run BEFORE the validation test, so -1 is never rejected as invalid.
- 3Validation logic: IF age < 0 OR age > 120 THEN reject (output a rejection message); ELSE add to total and increment count. Boundary values 0 and 120 must be ACCEPTED (use < 0 and > 120, not ≤ 0 and ≥ 120).
- 4Output count and average: After the loop, output the count. If count > 0, output the average (total / count). If count = 0, output a message instead of dividing by zero.
- 3Program structure and naming: Code reads top-to-bottom; identifiers are clear (count, total, age — not c, t, a); REPEAT/UNTIL or WHILE/ENDWHILE properly paired; IF/ENDIF structure visible.
Hints — reveal one at a time, only if stuck
Your pseudocode workspace
1 line