# Trace Tables & Algorithm Dry-Run Execution

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 7: Algorithm Design & Problem-Solving
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

A dry run follows an algorithm manually, tracking variables, condition results and output. Use the row convention stated in the task. Some tables record each change; others request snapshots at particular points. Do not invent a universal marking penalty for repeated values.

## Worked snapshots

This example records initial values, then a snapshot after each complete loop body, followed by output. Unchanged values repeat because these are snapshots.

```pseudocode
DECLARE Sum, Count : INTEGER
Sum <- 0
Count <- 1
WHILE Count <= 3 DO
    Sum <- Sum + Count
    Count <- Count + 1
ENDWHILE
OUTPUT Sum
```

  
| Event | Count | Sum | Output |
| --- | --- | --- | --- |
| Initialisation | 1 | 0 |  |
| After iteration 1 | 2 | 1 |  |
| After iteration 2 | 3 | 3 |  |
| After iteration 3 | 4 | 6 |  |
| Final output | 4 | 6 | 6 |

At Count = 4, the condition is false so the body does not run again. The update to Count happens after adding it to Sum, which explains why the first post-body row has Count = 2 but Sum = 1.

For a change-only table, record each assignment when it occurs and leave unaffected cells empty. Always preserve earlier entries, include initialisation where requested and record output exactly. Trace the exercises before using the interpreter to check your reasoning.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Concept Check: Trace order (Fill in blank, 30 XP)

**Instructions:**
Work out each answer before submitting. Starting with Sum=0 and Count=1, execute Sum <- Sum + Count then Count <- Count + 1. Sum is: Count is:


**Starter Code:**
```text
{"prompt":"Concept Check: Trace order","template":"1. Starting with Sum=0 and Count=1, execute Sum <- Sum + Count then Count <- Count + 1. Sum is: [blank_1]\n2. Count is: [blank_2]","blanks":[{"id":"blank_1"},{"id":"blank_2"}],"word_bank":["0","1","2","3","4"]}
```


### Exercise 2: Independent Trace: Decreasing N (Trace table, 30 XP)

**Instructions:**
Record five snapshots: initial values, after each of the three complete loop bodies, and after the final OUTPUT. Repeat unchanged values; leave OUTPUT empty until it executes.

```pseudocode
// no-run
N <- 3
Sum <- 0
WHILE N > 0 DO
    Sum <- Sum + N
    N <- N - 1
ENDWHILE
OUTPUT "Final Sum: ", Sum
```


**Starter Code:**
```text
{"columns": ["N", "Sum", "OUTPUT"], "rows": [["", "", ""], ["", "", ""], ["", "", ""], ["", "", ""], ["", "", ""]]}
```

**Autotest Expectations:**
- **All snapshots match**: `table_match` -> Expected: ``

### Exercise 3: Independent Trace: Powers (Trace table, 30 XP)

**Instructions:**
Record five snapshots: initial values, after each of the three complete loop bodies, and after the final OUTPUT. Repeat unchanged values; leave OUTPUT empty until it executes.

```pseudocode
// no-run
Power <- 1
Count <- 1
WHILE Count <= 3 DO
    Power <- Power * 2
    Count <- Count + 1
ENDWHILE
OUTPUT "Result: ", Power
```


**Starter Code:**
```text
{"columns": ["Count", "Power", "OUTPUT"], "rows": [["", "", ""], ["", "", ""], ["", "", ""], ["", "", ""], ["", "", ""]]}
```

**Autotest Expectations:**
- **All snapshots match**: `table_match` -> Expected: ``

