ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
15 marks · Records and parallel arrays
The program used to allocate tickets reads the data from the file called hopeful.dbase and stores it in an array of records. Using a programming language with which you are familiar, write the code needed to define a record structure and matching array of records that could be used to store the 6129 applications. The data is to be read from the database file hopeful.dbase so it can be used within the program. To read the data, the program must connect to the database using the connection details: server name: festival18 user: password: admin ticket18 Using a server-side scripting language with which you are familiar, write the script required to connect to the database, read the data from the database file and store it in a server-side variable. A procedure is needed to sort the application details in order of last name. The sort procedure will use a bubble sort algorithm that makes use of a Boolean variable. Use pseudocode to design a procedure to sort the array of records in ascending order of last name. Your design should make use of the data structure defined in part (a) (i) above. The people who are allocated tickets will be chosen at random from the full list of 6129 applications. Depending on the number of tickets requested by individual applicants, it may not be possible to allocate exactly 4000 tickets. Use pseudocode to design a procedure which would allocate the tickets for the festival. Your design should: • randomly select an applicant from the full list • check that an applicant has not been previously selected • ensure successful applicants receive their full allocation • make appropriate use of the array of records defined in part (a) (i) above • allocate at least 3997 and no more than 4000 tickets. State any assumptions you make about how the allocation of tickets will be recorded. A one-day music festival is being held with a maximum of 4000 tickets available. Those who wish to attend the festival can submit their details via a website. No applicant can ask for more than four tickets. The data collected on the website is validated before being stored in a table called applicants which is part of the database called hopeful.dbase. Sample records from the applicants table are shown below. Contact Number Reference Number Last Name First Name Contact e-mail Number of Tickets 1234 Smith John 09987654321 js@hello.net.uk 3 1235 Anderson Louise 01999999999 louise@a.org.uk 2 1236 Ali Hussain 08876767676 hali@house.com 4 By the closing date, 6129 applications had been received.
RECORD Applicant IS { INTEGER refNumber STRING lastname STRING firstname STRING contactNo STRING contactEmail INTEGER numTickets } DECLARE people AS ARRAY OF Applicant INITIALLY [] * 6129 1 mark for suitable record structure with variable names and types for each item to be stored. 1 mark for declaration of array of records to store 6129 records (or 6128 depending on language used). NOTE: contactNo must be STRING $server="festival18"; $user="admin"; $password="ticket18"; $database="hopeful.dbase"; $conn=mysql_connect($server, $user, $password); mysql_select_db($conn, $database); $sql="SELECT * FROM Applicants"; $result=mysql_query($conn, $sql); close($conn); Note alternative to use of mysql_connect and mysql_select: mysqli_connect($server, $user, $password, $database); 1 mark for connection to database server and selection of database. 1 mark for appropriate SQL query. 1 mark for allocation of query results to server-side variable. NOTE: Many alternative answers are possible including those that make use of values rather than variables that have had values assigned to them. One possible answer is shown. Many alternative answers exist. 1 mark for outer loop that makes use of Boolean variable. 1 mark for inner loop from 1 to 6128 (or 0 to 6127). 1 mark for correct updating of Boolean variable. 1 mark for comparison of last name of 2 adjacent records. 1 mark for swapping adjacent records that indicates use of temporary variable. 1. set swapped to true 2. while swapped = true 3. set swapped to false 4. start fixed loop from 1 to 6128 5. if people[loop].lastname and people[loop-1].lastname are in wrong order then 6. set temp to people[loop] 7. set people[loop] to people[loop- 1] 8. set people[loop-1] to temp 9. set swapped to true 10. end if 11. end fixed loop 12. end while loop One possible answer is shown. Many alternative answers exist. The following solution assumes that a 1D array of integer values called allocated is used to store the number of tickets allocated to each applicant. All values in the array are initialised to 0 indicating that no tickets are allocated at the start. 1 mark for randomly selecting an applicant from the full list of applicants. 1 mark for check that an applicant has not been previously selected. 1 mark for ensuring successful applicants receive their full allocation. 1 mark for appropriate use of the array of records defined in part (a) (i) above (numTickets field). 1 mark for allocating at least 3997 and no more than 4000 tickets. 1. set totalAllocated = 0 2. start fixed loop from 0 to 6128 3. set each element of allocated array = 0 4. end loop 5. set betweenLimits to false 6. while totalAllocated <=4000 or betweenLimits = true 7. select random number btw 0 and 6128 8. if (people[random].numTickets + totalAllocated <=4000) and (allocated[random] = 0) then 9. add people[random].numTickets to totalAllocated 10. set allocated[random] to people[random].numTickets 11. If totalAllocated >=3997 then set betweenLimits to true 12. end if 13. end while
Previous question: AH Computing Science 2018 Question 3 (CfE)