Question 2

OCR Computer Science June 2024 Algorithms and problem solving (H046-02)AS17 marks

On the specification

(a)
1.4 Data types, data structures and algorithms
(b)(i)
2.2 Problem solving and programming
(b)(ii)
2.2 Problem solving and programming
(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
(b)(vi)
2.2 Problem solving and programming2.3 Algorithms
Question text

2 A programmer is designing a program that will store data. The programmer is deciding whether to store the data in a stack or a queue. (a) Identify one similarity and one difference between a stack and a queue. Similarity Difference [2] (b) The pseudocode function, enqueue, inserts an item into a queue. 1 function enqueue(item) 2 if tailPointer >= queue.length then 3 return false 4 else 5 queue[tailPointer] = item 6 tailPointer = tailPointer + 1 7 return true 8 endif 9 endfunction (i) Give the name of the parameter in the function enqueue. [1] (ii) Give the name of one global variable that is used in the function enqueue. [1] (iii) Describe one benefit and one drawback of using global variables instead of parameter passing in a subroutine. Benefit Drawback [4] (iv) The function enqueue can be called by the main program. Explain why the function enqueue returns true or false values, and how this can be used by the main program that calls the function. [3] (v) The pseudocode function, dequeue, removes and returns the first item in the queue. If the queue is empty, the function returns the string "EMPTY". 1 function dequeue(data) 2 if headPointer != tailPointer then 3 return "EMPTY" 4 elseif 5 value = queue[headPointer] 6 return value 7 headPointer = headPointer + 1 8 endif 9 endfunction The function dequeue has several errors. Identify the line number of any three errors and state the correction required. Error 1 Line Number Error 1 Correction Error 2 Line Number Error 2 Correction Error 3 Line Number Error 3 Correction [3] (vi) The programmer has corrected all of the errors in the function dequeue. The main program repeatedly calls the function dequeue until all of the elements in the queue have been output. Write the main program using pseudocode or program code. [3]