Worked solution from the System Software Paper 3 Advanced Theory booklet, Part 2 of 2.
Write: to improve the code so that it uses minimum resources, for example less memory, less storage or less CPU time. [1]
Rebuild the expression by reading the RPN left to right, using a stack.
a b - gives (a - b), then c + gives (a - b + c). [1]
c a - gives (c - a), and * multiplies the two results: (a - b + c) * (c - a). [1]
d / divides that by d. [1]
Write: (a - b + c) * (c - a) / d
Push each value. An operator takes the top two items, works out the answer and pushes it back. a = 3, b = 16, c = 9, d = 6.
| Step | Item read | Stack (bottom → top) |
|---|---|---|
| 1 | c | 9 |
| 2 | a | 9, 3 |
| 3 | / | 3 |
| 4 | b | 3, 16 |
| 5 | d | 3, 16, 6 |
| 6 | - | 3, 10 |
| 7 | * | 30 |
| 8 | b | 30, 16 |
| 9 | + | 46 |
c, a pushed, then / gives 3: stack 9 → 9, 3 → 3 [1]
b, d pushed, then - gives 10: stack 3, 16 → 3, 16, 6 → 3, 10 [1]
* gives 30: stack 30 [1]
b pushed, then + gives the final answer: stack 30, 16 → 46 [1]
▶ Starts at Q8 (1:06:00)
Starts at Q8 (1:06:00) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.