ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
15 marks · Top-level design and refinement
Use pseudocode to create the top level design for the program. Your top level design should define the required data structure and call all necessary modules. Use pseudocode to refine the binary search used to display the details of the pupil with PupilID 112213. During testing of the program, Dawid changes the registration class of the pupil with PupilID 112213 from 6A to 6B. Using pseudocode or a language you are familiar with, write the code needed to edit the required details in the external database file called “pupils.db”. Dawid decides to add a new module to his program. This module sorts the data in the array of records into ascending order of registration class. Part of Dawid’s code is shown. Line 1 # Name of Sort Algorithm Used: ____________________________ Line 2 REPEAT Line 3 SET swapped TO false Line 4 FOR counter FROM 1 TO 9 Line 5 IF ________________________________________________ Line 6 SET swapped TO true Line 7 < swap data > Line 8 END IF Line 9 END FOR Line 10 UNTIL swapped = false Line 1 and Line 5 of the code are incomplete. Provide the missing details by rewriting both lines of code. Dawid’s school has 2000 pupils. Explain why it may be more appropriate to use a quick sort rather than the sort algorithm used in part (c) above. Dawid Mahyne is studying Advanced Higher Computing Science. His teacher has asked him to compare the computational constructs provided by a procedural programming language with those provided by a database. Dawid starts by creating a database file called “pupils.db”. The file contains one table called “pupildata” which stores the pupil data shown. LastName PupilID FirstName DateOfBirth RegClass 112211 112212 112213 112214 112215 112216 112217 112218 112219 112220 Joan John Alison Brian Bilal Lian Charles Janet Raymond Theresa Simpson Adam Brown Morgan Ali Wong West Smith Thomas Cameron 23/02/1999 12/04/1998 30/10/1998 18/11/1998 12/09/1998 27/05/1998 23/06/1998 18/02/1999 07/12/1998 29/01/1999 6A 6B 6A 6C 6C 6A 6B 6B 6B 6A Dawid writes a program to import the pupil data from the database file and store it in an array of records called “details”. His program then applies a binary search to the array of records to display the details of the pupil with PupilID 112213.
1. RECORD pupil IS { INTEGER PupilID, STRING FirstName, STRING LastName, STRING DateOfBirth, STRING RegClass } 2. DECLARE details AS ARRAY OF pupil * 10 INITIALLY [ ] 3. importData (details[ ]) 4. search(details[ ]) 1 mark for definition of record structure with 5 fields and correct field types 1 mark for declaration of array of records with 10 records 1 mark for both procedure calls (a) 1. SET lower TO 0 2. SET upper TO 9 3. SET found TO false 4. REPEAT 5. SET mid TO (lower + upper)/2 6. IF pupilData[mid].PupilID = 112213 THEN SET found TO true 7. IF pupilData[mid].PupilID < 112213 THEN SET lower TO mid + 1 8. IF pupilData[mid].PupilID > 112213 THEN SET upper TO mid - 1 9. UNTIL upper < lower OR found = true 10. IF found = true THEN 11. SEND pupilData[mid].pupilID, pupilData[mid].FirstName, pupilData[mid].LastName, pupilData[mid].DateOfBirth, pupilData[mid].RegClass TO DISPLAY 12. ELSE 13. SEND "Pupil ID not found" TO DISPLAY 14. END IF 1 mark for initialisation 1 mark for conditional loop with correct complex condition 1 mark for use of array of records with correct indexing 1 mark for comparisons and appropriate updates 1 mark for display of correct pupil details (note that the check for found is not essential). 1. < connect to database server > 3. SET query TO "update pupildata set RegClass = '6B' where PupilID = 112213" 4. < execute SQL query > 5. < close database connection > 1 mark for connect / close connection with database server 1 mark for correct record (where clause) 1 mark for update query Line 1 # Sort Algorithm Used: Bubble Sort Line 5 IF pupilData[counter-1] > pupilData[counter] THEN 1 mark for Line 1 correct 1 mark for Line 5 correct Alternative / equivalent forms of line 5 are acceptable. Quicksort takes less time than bubble sort to sort large data sets since fewer comparisons are needed. Based on average case scenario, quicksort would need 21932 comparisons (ie nlog2(n) ie 2000 x log2(2000) = 21,932) whereas bubble sort will need n2 (ie 2000² = 4,000,000). 1 mark for efficiency of quicksort. Note that ‘quicker’ without further detail is not sufficient. 1 mark for justification of this efficiency. Note that justification in terms of Big O notation is not essential.
Previous question: AH Computing Science 2016 Question 3 (CfE)