The temperature logger that reports 13.3 °C on a 20 °C day
A Year 8 science class logs temperatures with a cheap sensor kit that writes a text file, one reading per line: 21.5, 19.0, ERR, -3.2, blank lines, and occasionally 21,5 when the kit is plugged into a laptop borrowed from the languages department. A pupil's program reads every line, adds the value to a running total and divides by the number of lines. On a day when the room was about 20 °C it reports a class average of 13.3 °C. The teacher wants a corrected average, a count of readings that could not be used, and a warning if the file looks wrong. The program must not stop when it meets a bad line.
1. Explain the mechanism that produces 13.3 °C, including which kinds of line contribute to it, and show the arithmetic that confirms the diagnosis.
Read the worked answer
Mechanism: bad lines add 0 to the total but still count in the divisor. The average is dragged toward zero by entries that are not temperatures at all.
ERRcannot convert to a number, so a typical pattern - try to convert, on failure use 0 - contributes 0 and increments the count.- Blank lines do the same.
21,5fails conversion on most parsers (a decimal comma is not accepted), so a genuine 21.5 °C reading also becomes a 0 - the worst case, because valid data is being thrown away silently.
Arithmetic check: 8 valid readings averaging 20 sum to 160. Count 4 rejected lines as 0 and divide by 12: 160 ÷ 12 = 13.3. That matches the reported figure exactly, which confirms the mechanism rather than merely suggesting it.
So the root cause is two faults, not one: invalid lines are being given a value of zero, and the denominator is 'number of lines' instead of 'number of usable readings'. Fixing only the parser would still leave a subtly wrong average.
2. Describe the control flow for processing each line so that bad data is skipped and counted, the decimal comma is handled, and the program never stops. Identity the case where the fixed program would still fail and how to guard against it.
Read the worked answer
Per line:
- Strip surrounding whitespace.
- If the line is now empty → skip it, increment
rejected. - Normalise the decimal comma to a point. A defensible strict rule: replace
,with.only when the line contains exactly one comma and no point, so a stray thousands separator is not silently misread. Document the rule - it is a decision, not a detail. - Attempt conversion to a number. On failure → skip, increment
rejected, and keep the line with its line number in a small list so the teacher can see what was dropped. - On success → append the value to a list of accepted readings.
After the loop: if the list is empty, report 'no usable readings' and exit; otherwise average = sum ÷ length of the list. The divisor can now never include a rejected line.
'Never stops' is achieved because every conversion sits inside a guarded branch - no conversion is performed where its failure would end the run.
Remaining failure case: a file with zero usable readings would cause a division by zero if the average were computed unconditionally. That is the one path the loop cannot protect, so it needs its own explicit check before the division - and it is exactly the case that occurs when the sensor is unplugged and every line reads ERR.
3. Recommend how to store the accepted values and the rejects, and justify the choice against the alternatives for this class and this task.
Read the worked answer
Store accepted readings as a list of numbers; keep rejected as an integer plus a small list of the offending lines with their line numbers.
Why not just a running total: the total alone cannot give the denominator - you would need a separate counter AND the total, and the two can drift apart under later edits. It also cannot produce a median, cannot show the teacher the spread of a lesson's readings, and cannot be re-checked once the run has finished. A few hundred readings from a 45-minute lesson cost nothing to hold in memory, so there is no efficiency argument for discarding them.
Why keep the rejected lines, not just the count: '4 readings were unusable' tells the teacher nothing actionable; 'lines 12, 13, 40 used ERR; line 41 used a decimal comma' tells her the sensor was bumped or the wrong laptop was used. Silently excluding data without being able to show which data is the same failure mode as the original bug.
Precision note: the sensor reports to 0.1 °C, so summing integer tenths would make the total exact. For a class exercise floats are fine, but the choice should be deliberate rather than accidental.