An engineer is writing an algorithm to process and sum the daily temperature readings for five monitoring sensors, each recording four readings over a 24-hour cycle. There are 20 historical readings recorded in total.
The algorithm below contains a logic error and fails to process all 20 temperature readings.
1 sensors ← ['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon']
2 readings ← [14.2, 15.1, 14.8, 13.9, 18.0, 18.5, 17.9, 19.1, 12.1, 11.8, 12.5, 13.0, 15.5, 16.0, 15.8, 15.2, 20.1, 21.3, 20.8, 19.9]
3 totalTemp ← 0.0
4 FOR i ← 0 TO 4
5 sensorName ← sensors[i]
6 OUTPUT 'Sensor: ', sensorName
7 FOR j ← 0 TO 2
8 OUTPUT 'Reading: ', j + 1
9 temp ← readings[i * 4 + j]
10 OUTPUT temp
11 totalTemp ← totalTemp + temp
12 ENDFOR
13 ENDFOR
How could the error in the algorithm be corrected so that all 20 temperature readings are processed?
Change line number 4 to: FOR i ← 0 TO 5
Change line number 7 to: FOR j ← 0 TO 3
Change line number 9 to: temp ← readings[i * 5 + j]
Change line number 11 to: totalTemp ← totalTemp + readings[i]
Practise AQA GCSE Computer Science Programming concepts with exam-style questions for GCSE Computer Science. 100 questions, matched to the AQA GCSE Computer Science (8525) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.