A program uses a stack to hold up to 60 numeric values. The stack is implemented using two integer variables and a 1D array. The array is declared in pseudocode as shown: DECLARE ThisStack : ARRAY [1:60] OF REAL The stack operates as follows: • Global variable SP acts as a stack pointer that points to the next available stack location. The value of SP represents an array index. • Global variable OnStack represents the number of values currently on the stack. • The stack grows upwards from array element index 1.
- (a)(i)Give the initial values that should be assigned to the two variables. SP OnStack[1]
- (a)(ii)Explain why it is not necessary to initialise the array elements before the stack is used.[2]
- (b)A function to add a value to ThisStack is expressed in pseudocode as shown. The function will return a value to indicate whether the operation was successful or not. Complete the pseudocode by filling in the gaps. FUNCTION Push(ThisValue : REAL) RETURNS BOOLEAN DECLARE ReturnValue : BOOLEAN IF RETURN ENDIF SP ← OnStack ← OnStack + 1 RETURN TRUE ENDFUNCTION THEN // stack is already full ThisValue[4]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.
- [1] SP: 1
- [1] OnStack: 0
- [1] Unused values cannot be popped / taken off the stack // initialised values would never be used / unused elements cannot be accessed
- [1] until a value has first been pushed / written // overwrites previous value
- [1] IF OnStack = 60 / >59 // SP = 61 / SP > 60 // SP outside the range 1 to 60 THEN
- [1] RETURN FALSE // Stack is already full
- [1] ThisStack[SP] ← ThisValue
- [1] SP ← SP + 1
- [1] OnStack ← OnStack + 1
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →