Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.
The array is filled from element 1 upwards, so the bottom item D2 goes in element 1.
| Array element | Data |
|---|---|
| 8 | |
| 7 | |
| 6 | |
| 5 | D1 |
| 4 | D3 |
| 3 | D4 |
| 2 | D5 |
| 1 | D2 |
| Variable | Value |
|---|---|
| TopOfStack | 5 |
| BottomOfStack | 1 |
All values in the order and places shown [1]
TopOfStack = 5 (the element holding D1) [1]
BottomOfStack = 1 (the element holding D2) [1]
Complete the five steps:
1 If TopOfStack = 8 (the stack is full) then return FALSE [1]
2 Otherwise increment TopOfStack [1]
3 Use TopOfStack as an index to the array [1]
4 Set the element at this index (location or position) to the value (data or item) being added [1]
5 Return TRUE [1]
▶ Starts at Q3 (15:42)
Starts at Q3 (15:42) · by Pseudocode Pro. 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.