9618/31/O/N/23 question 9: worked solution (October/November 2023, paper 31)

Show as plain text

Cambridge A2 Level Computer Science 9618 · 16.2 Translation Software Worked solution from the System Software Paper 3 Advanced Theory booklet, Part 2 of 2. Q24 9618/31/O/N/23 original Q9 Structured · 9 marks Worked solution (a)(i) Work from the left. Each operator uses the two values before it. 5 2 – becomes (5 – 2); 5 4 + becomes (5 + 4). [1] Then * joins them, then / 9: write (5 – 2) * (5 + 4) / 9 [1] (a)(ii) Push each number. At an operator, pop two values, calculate, push the result. Item read Stack (bottom → top) 5 5 2 5, 2 – 3 5 3, 5 4 3, 5, 4 + 3, 9 * 27 9 27, 9 / 3 5 and 2 pushed, then 3 after the subtraction [1] 5 and 4 pushed, then 9 after the addition [1] 27 after the multiplication [1] 9 pushed, then final answer 3 after the division [1] Marks are given for correct stack stages like these. (b) Read the expression from left to right . [1] Push each value onto the stack until an operator is reached. [1] At an operator, pop the last two values, apply the operator, and push the result back. Repeat until the whole expression is read and one value is left. [1] KEY IDEA Evaluate RPN with a stack: push values, and at each operator pop two, calculate and push the result. TOTAL 9 marks Official mark scheme VIDEO ▶ Topic lesson A lesson on this topic · by Craig'n'Dave. Tap the picture to open it on YouTube.