Question 2

OCR Computer Science June 2023 Computer systems (H446-01)A level22 marks

On the specification

(a)
2.2 Problem solving and programming2.3 Algorithms
(b)(i)
2.3 Algorithms
(b)(ii)
2.3 Algorithms
(b)(iii)
2.3 Algorithms
(c)(i)
1.4 Data types, data structures and algorithms
(c)(ii)
1.4 Data types, data structures and algorithms
(c)(iii)
1.4 Data types, data structures and algorithms
(c)(iv)
1.4 Data types, data structures and algorithms2.3 Algorithms
Question text

2 Sundip writes an algorithm to carry out addition and subtraction. The algorithm will use an initially empty stack with the identifier numbers and will take input from the user. The action the algorithm takes depends on the value input by the user. These actions are listed in Fig. 2. Value input Action to take • Pop two values from the numbers stack • Add the two values A • Push the result back onto the numbers stack • Pop two values from the numbers stack • Subtract the first popped value from the second S • Push the result back onto the numbers stack • Pop one value from the numbers stack • Output this value E • End program Any other • Push the input value to the numbers stack value Fig. 2 (a) Complete the pseudocode here to implement Sundip’s algorithm. do value = input("Enter a value") if then num = numbers.pop() print(num) elseif value == "A" or then numone = numbers.pop() numtwo = numbers.pop() if value == "A" then numbers.push elseif value == "S" then numbers.push(numtwo – numone) endif else numbers.push( ) endif until value == [5] (b) (i) Complete the diagram to show the state of the stack after each value is entered into the algorithm. The letters will complete an action stated in Fig. 2. The state of the stack after the first value, 8, has been completed for you. Input 8 7 A 6 8 [3] (ii) Complete the following table to give the output from this algorithm when the following set of inputs are entered by the user. The letters will complete an action stated in Fig. 2. Input data (from left to right) Output 93 A E 10 5 A 8 S E 25 5 S 2 3 A S E [3] If the user enters 4 2 S A E , the algorithm will not work correctly. (iii) Explain what problem this input data will cause and why the problem occurs. [3] (c) A stack is one data structure that is available for Sundip to use. She could also use a queue, list, linked list, array or tuple. (i) Describe one difference between a stack and a queue. [2] (ii) Describe one difference between an array and a list. [2] (iii) State how a tuple is different to a list. [1] (iv) Describe how the second item in a linked list would be accessed using pointer values. [3]