Question 1 Report
Study the following pseudocode.
DECLARE Row : INTEGER
DECLARE Col : INTEGER
DECLARE Value : INTEGER
Value ← 0
FOR Row ← 1 TO 3
FOR Col ← 1 TO Row
Value ← Value + Col
OUTPUT Value, " "
NEXT Col
OUTPUT "" 'new line
NEXT Row(a) Complete the trace table showing all iterations. [4]
| Row | Col | Value (before) | Value + Col | Value (after) | OUTPUT |
|---|---|---|---|---|---|
| 1 | 1 | ||||
| 2 | 1 | ||||
| 2 | 2 | ||||
| 3 | 1 | ||||
| 3 | 2 | ||||
| 3 | 3 |
(a) The nested loops iterate with Col running from 1 up to the current Row value. Value accumulates by adding Col each time. The complete trace:
| Row | Col | Value (before) | Value + Col | Value (after) | OUTPUT |
|---|---|---|---|---|---|
| 1 | 1 | 0 | 0 + 1 = 1 | 1 | 1 |
| 2 | 1 | 1 | 1 + 1 = 2 | 2 | 2 |
| 2 | 2 | 2 | 2 + 2 = 4 | 4 | 4 |
| 3 | 1 | 4 | 4 + 1 = 5 | 5 | 5 |
| 3 | 2 | 5 | 5 + 2 = 7 | 7 | 7 |
| 3 | 3 | 7 | 7 + 3 = 10 | 10 | 10 |
The output appears on three lines: "1", then "2 4", then "5 7 10" (a newline is printed after each Row completes). Note that Value is cumulative across all rows, not reset per row. [4]
Everything you need to excel in your exams