9618/23/M/J/23 question 3: worked solution (May/June 2023, paper 23)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.4 Introduction to Abstract Data Types (ADT) Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4. Q75 9618/23/M/J/23 original Q3 Structured · 11 marks Worked solution (a)(i) All 6 items can be popped before the stack is empty. Write: 6 [1] (a)(ii) Follow each operation. Pop WWW into Data1, pop YYY into Data2, push AAA (to 504), push BBB (to 505), pop and discard BBB, pop AAA into Data2. TopOfStack ends at 503. Memory location Value Pointer 506 505 BBB 504 AAA 503 XXX TopOfStack 502 ZZZ 501 NNN 500 PPP BottomOfStack Variable Value Data1 WWW Data2 AAA Values BBB and AAA shown in 505 and 504 (they stay in memory but are no longer in the stack) [1] Values XXX, ZZZ, NNN and PPP unchanged [1] Both pointers shown and labelled (TopOfStack at 503, BottomOfStack at 500) [1] Both variables correct: Data1 = WWW and Data2 = AAA [1] (b)(i) The data can be recovered (restored) the next time the program is run, because it is saved permanently and is not lost when the program ends [1] (b)(ii) Write the five steps: Step 1: open the text file in WRITE mode [1] Step 2: check that there is a value on the stack [1] Step 3: POP a value from the stack [1] Step 4: write the value to the text file [1] Step 5: repeat from step 2 (loop while the stack has items) [1] Another way: read the value at TopOfStack, write it, then decrement TopOfStack, and repeat. KEY IDEA A stack is last in, first out: POP and PUSH move the top pointer, and popped values are not erased from memory. TOTAL 11 marks Official mark scheme VIDEO ▶ Starts at Q3 (37:30) Starts at Q3 (37:30) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.