OCR A-level Computer Science H446
Trace the state, then write the code
Component 02: Algorithms and programming
- Specification
- 2.2.1(a)
- Marks
- 10
- Suggested time
- 15 minutes
- Calculator
- Not permitted
A program totals readings greater than 4. Array indexing starts at 0. The for loop includes both its first and last index. Record the trace at the end of each loop pass, after any addition.
01 array readings[5] = [3, 8, 2, 7, 4] 02 total = 0 03 for i = 0 to 4 04 if readings[i] > 4 then 05 total = total + readings[i] 06 endif 07 next i 08 print(total)
| i | readings[i] | Condition true? | total |
|---|---|---|---|
| 0 | |||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
a Complete the trace table.
4 marks
b State the value printed on line 08.
1 mark
c Explain why total does not change on every pass of the loop.
2 marks
d Write a replacement for lines 03–07 using a condition-controlled loop. Keep the same output.
3 marks