Question 5
OCR Computer Science June 2018 Paper 2A level29 marks
On the specification
- (a)
- 1.4 Data types, data structures and algorithms2.3 Algorithms
- (b)(i)
- 2.2 Problem solving and programming1.4 Data types, data structures and algorithms
- (b)(ii)
- 2.2 Problem solving and programming
- (c)
- 1.4 Data types, data structures and algorithms2.3 Algorithms
- (d)(i)
- 2.3 Algorithms
- (d)(ii)
- 2.3 Algorithms
- (d)(iii)
- 2.2 Problem solving and programming2.3 Algorithms
Question text
5 A computer program stores data input on a stack named dataItems. The stack has two sub- programs to add and remove data items from the stack. The stack is implemented as a 1D array, dataArray. Sub-program Description push() The parameter is added to the top of the stack pop() The element at the top of the stack is removed The current contents of dataItems are shown: 6 15 100 23 (a) Show the contents of the stack dataItems after each line of the following lines of code are run 1 push(13) 2 pop() 3 push(10) 4 push(20) Line 01Line 01 LineLine 0202 LineLine 0303 LineLine 0404 6 15 100 23 [4] (b) The main program asks a user to push or pop an item from the stack. If the user chooses ‘push’, the data item is added to the stack. If the user chooses “pop”, the next item is removed from the stack, multiplied by 3 and output. The main program is shown: 1 userAnswer = input("Would you like to push or pop an item?") 2 if userAnswer == "push" then 3 push(input("Enter data item")) 4 else 5 print(pop() * 3) 6 endif (i) Before the sub-programs, push() and pop(), can add or remove items from the stack, a selection statement is used to decide if each action is possible. Describe the decision that needs to be made in each sub-program and how this impacts the next process. push() pop() [4] (ii) The algorithm does not work when the user enters "PUSH" or "Push". The algorithm needs to be changed in order to accept these inputs. Identify the line number to be changed and state the change that should be made. Line number Change [2] (c) The stack is implemented as a 1D array, dataArray. Describe how a 1D array can be set up and used to push and pop items as a stack. [3] (d) As an array, the data in dataArray is sorted and then searched for a specific value. (i) The data in dataArray is sorted into ascending order using an insertion sort. The current contents of dataArray are shown: 100 22 5 36 999 12 Show the steps of an insertion sort on the current contents of the array dataArray. [5] (ii) The array dataArray can now be searched using a binary search. Describe the stages of a binary search on an array of size n. [7] (iii) The array has 50 items. The function, searchItem(), performs a linear search for a data item. function searchItem(dataItem) for count = 0 to 49 if dataArray[count] == dataItem then return(count) endif next count return(-1) endfunction Rewrite the function using a while loop. [4]