Question 8

OCR Computer Science June 2022 Algorithms and programming (H446-02)A level40 marks

On the specification

(a)(i)
2.3 Algorithms
(a)(ii)
1.4 Data types, data structures and algorithms
(b)(i)
2.2 Problem solving and programming2.3 Algorithms
(b)(ii)
2.2 Problem solving and programming2.3 Algorithms
(b)(iii)
2.2 Problem solving and programming
(b)(iv)
2.2 Problem solving and programming
(b)(v)
2.2 Problem solving and programming2.3 Algorithms
(c)(i)
1.4 Data types, data structures and algorithms
(c)(ii)
1.2 Software and software development2.2 Problem solving and programming
Question text

8 A computer uses a stack data structure, implemented using an array, to store numbers entered by the user. The array is zero based and has 100 locations. (a) Fig. 8 shows the current contents of the stack and the first 9 locations of the array. Index Data 8 7 pointerValue 5 6 5 41 3 23 26 15 0 10 Fig. 8 (i) The function pop() removes an item from the stack. The function push() adds an item to the stack that is passed in as a parameter. Show the contents of the stack and pointer from Fig. 8 after the following subroutines calls have run. pop() pop() push(3) push(6) push(7) Index Data 8 7 pointerValue 6 5 4 3 2 1 0 [2] (ii) State the purpose of pointerValue. [1] (b) The stack is programmed as an object using object-oriented programming. The design for the class, its attributes and methods are shown: class: stack attributes: private stackArray : Array of integer private pointerValue : integer methods: new() function pop() function push(value) (i) The method pop() returns the next value in the stack, or –1 if the stack is empty. Complete the pseudocode method pop(). public function pop() if pointerValue == then return else pointerValue = pointerValue returnValue = stackArray[ ] return endif endfunction [5] (ii) The method push() accepts an integer as a parameter and adds it to the top of the stack unless the stack is already full. If the push is successful the method returns true. If the push is unsuccessful due to the stack being full the method returns false. Write the method push() using either pseudocode or program code. [6] (iii) The main program initialises a new object of type stack with the identifier mathsStack. Write pseudocode or program code to declare the object. [2] (iv) The main program needs to: • take numbers as input from the user • push them onto the stack mathsStack until the stack is full • output an appropriate message if the stack is full. Complete the pseudocode algorithm to meet these requirements. returnValue = true while returnValue == returnValue = mathsStack. (input("Enter Number")) if returnValue == then ("Stack full") endif endwhile [4] (v) The main program also needs to: • remove one item from the stack at a time and add this to a total • output the total every time an item is removed • stop removing items when either the stack is empty, or 20 items have been removed. Write pseudocode or program code to meet these requirements. [8] (c) The program is amended to include the use of several queue data structures. (i) Describe how an array can be used to implement a queue data structure. [3] (ii)* Discuss the use of object-oriented programming and procedural programming to create and manipulate the queue data structures. You should include the following in your answer: • the features of object-oriented programming • the features of procedural programming • the benefits of using object-oriented instead of procedural programming when creating several queue structures. [9]