Unable to open this exercise
The mean is total divided by count. The total and count must describe the same accepted values. Use real division because an average can have a fractional part.
Worked trace
For readings 4,7,9: after 4, total=4 and count=1; after 7, total=11 and count=2; after 9, total=20 and count=3. The mean is 20/3, about 6.67. Dividing by the last value or by count-1 is incorrect.
If a sentinel arrives before any reading, count remains zero. Check count before dividing and provide an explicit no-data output. A sentinel terminates input; it is not another reading.
Independent explanation
Explain why displaying zero as the average for no readings can be misleading: a genuine set of zero-valued readings has mean zero, whereas an empty set has no calculated mean.
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.
Independent Practice: Mean of readings
Read non-negative integer readings until -1. Output their mean rounded to two decimal places, or NO DATA if there are none.
Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.
Interactive exercises for Module 2 and beyond require an active plan.
Run code to inspect step-by-step variable trace states.