ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
15 marks · Insertion sort
The first prototype uses four 1-D arrays called towelID, type, price and quantity. An insertion sort is used to arrange the towel data in ascending order of towelID. The values currently stored in the towelID array are shown below. index 0 1 2 3 4 value B224 F127 F411 B432 H121 Explain how the insertion sort algorithm would be used to place the towel with towelID B432 in the correct position in the towelID array. You should assume that the corresponding values in the type, price and quantity arrays will be sorted accordingly. The binary search algorithm is used to determine whether a particular towelID is in stock. 1. set startPointer to 0 2. set endPointer to index of (last element in towelID array) − 1 3. set found to false 4. enter towelIDtoFind 5. repeat 6. set middle to (startPointer + endPointer)/2 7. make comparisons and update pointers 8. until startPointer > endPointer or found = true 9. if found = true then 10. check stock level 11. else 12. display not found 13. end if Use pseudocode to refine step 7 of the algorithm. A function called calc()is to be applied to the quantity array. The values stored in the quantity array are shown below. index 0 1 2 3 4 value 134 14 13 111 34 The code for function calc()is shown below. FUNCTION calc(ARRAY OF INTEGER q, INTEGER n) RETURNS INTEGER IF n <= 0 THEN RETURN 0 ELSE RETURN calc(q, n-1) + q[n-1] END IF END FUNCTION Name the computational construct used in function calc(). The function is called using the following statement: SET x TO calc(quantity, 3) With reference to the function code above, explain how the value 161 is assigned to the variable x. The ID for any towel to be restocked will be added to a queue data structure. Use pseudocode to design an algorithm used to add an item to the queue. The second prototype uses a linked list as shown below. Address 100 Address 431 Address 201 Address 351 Address 133 B224 Bath 12.99 134 B432 Bath 19.99 14 F127 Face 6.99 13 F411 Face 12.99 111 H121 Hand 18.99 34 NULL Head 100 431 201 351 133 A new towel ("H128", "Hand", 4.99, 200) is added at address 147. Describe the changes that will be made to the linked list when this new towel is added. Once fully developed, the new software will undergo beta testing. Describe what is involved in beta testing the software. The Perfect Towel Company sells a range of towels. The company needs new software to keep track of stock. The details stored about each towel include • towel ID • type • price • quantity in stock. Two prototypes of the solution are developed.
Having identified that towel B432 is out of place, the insertion sort algorithm: • works backwards down the array to identify the correct insertion position • towel B432 is copied and the towels from the insertion point upwards move one position to the right • towel B432 is then inserted into the correct position in the array Alternatively • compares towel B432 with towel F411 and swaps them • compares towel B432 with towel F127 and swaps them • compares towel B432 with towel B224 and doesn’t swap then because they are in the correct sequence and towel B432 is now in the correct position in the array 1 mark each for the following: • indication of working backwards from towel B432 to the insertion point • terminating the backwards comparisons once towel B432 has been positioned correctly 1 mark for either of the following: • swapping towel B432 with previous elements of the array • copying towel B432, moving elements with larger values to the right and then inserting towel B432 in the correct position Note: marks can be allocated to relevant lines of responses that are presented as an algorithm; to receive full marks, the algorithm must refer to the scenario (eg make use of towel B432) 7.1 if middle element of towelID array = towelIDtoFind then 7.2 set found to true 7.3 else if middle element of towelID array < towelIDtoFind then 7.4 set startPointer TO middle+1 7.5 else 7.6 set endPointer TO middle-1 7.7 end if 1 mark for each correct comparison and output Note: middle must be used as an array index and not a comparatpr Note: 3 separate IF statements are also acceptable (iii) A Recursion (iii) B The values of the first 3 elements in the quantity array are totalled. The array index is reduced each time the function is called until the base case is reached. 1 mark for totalling the quantity of towels 1 mark for explanation of how recursion is used to achieve a total of 161 1. if queue is full then 2. display queue full message 3. else 4. add towelID to queue 5. set rearPointer to rearPointer +1 6. end if 1 mark for checking for queue overflow 1 mark for adding data 1 mark for increasing pointer The null pointer on the last node of the linked list is replaced with address of the new node, 147. A new node is created to store the data (H128, Hand, 4, 99, 200) which has the pointer value NULL. 1 mark for each point Note: use of a diagram illustrating updating of pointers and use of additional node is acceptable alternative to complete description Company staff would use the software and provide feedback to the developers on any issues that arise. 1 mark for use by end users with feedback
Previous question: AH Computing Science 2019 Question 2 (CfE)