ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
13 marks · Connecting to a database
Using pseudocode, refine step 2 of the design to show the steps needed to read the data from the database server and store it in the array of records called animalList. The number of each mammal is recorded regularly. Whenever new data about a mammal becomes available, the name of the mammal, number remaining and reading number are stored in a text file readings.txt. The contents of the latest readings.txt are shown below: 24/05/2024 Polar Bear,29140,2 Giant Panda,1864,1 Tiger,3745,3 African Wild Dog,3012,2 Step 3 of the top-level design processes the readings file. The refinement for this step is shown below. 3.1 declare temporary array temp as array of Animal 3.2 read data from readings.txt file into temp 3.3 sort temp into descending order of readingNumber 3.4 move existing records in animalList array to create empty rows required 3.5 copy contents of temp into empty rows of animalList array Once step 3.2 has been completed, the contents of the array temp will be: mammal numberRemaining readingNumber readingDate Polar Bear 29140 2 24/05/2024 Giant Panda 1864 1 24/05/2024 Tiger 3745 3 24/05/2024 African Wild Dog 3012 2 24/05/2024 The incomplete design of the insertion sort used to arrange the contents of the temp array into descending order of readingNumber at step 3.3 is shown below. 3.3.1 procedure insertionSort(list) IN/OUT: list 3.3.2 set value = 0 3.3.3 set index = 0 3.3.4 loop from n = 1 to length(list)−1 … … … … … … … … … end loop … end procedure Write the pseudocode needed to complete the insertion sort algorithm. At step 3.4, the data stored in the array temp is copied into the animalList array. The table below shows the contents of the animalList array before step 3.4 is executed. mammal numberRemaining readingNumber readingDate Tiger 3890 2 12/05/2024 Blue Whale 5000 2 12/05/2024 Polar Bear 31000 1 12/05/2024 Rhinoceros 5600 1 12/05/2024 Orangutan 105000 1 12/05/2024 Blue Whale 7632 1 05/05/2024 Tiger 4432 1 05/05/2024 African Wild Dog 3000 1 05/05/2024 Step 3.4 requires the contents of the animalList array from index 0 to be moved to accommodate the data currently stored in the temp array. The table below shows the contents of the animalList array after step 3.4 is executed. mammal numberRemaining readingNumber readingDate Tiger 3890 2 12/05/2024 Blue Whale 5000 2 12/05/2024 Polar Bear 31000 1 12/05/2024 Rhinoceros 5600 1 12/05/2024 Orangutan 105000 1 12/05/2024 Blue Whale 7632 1 05/05/2024 Tiger 4432 1 05/05/2024 African Wild Dog 3000 1 05/05/2024 Using pseudocode, refine step 3.4 of the top-level design to show the steps needed to move the rows of data within the animalList array. Once the update has taken place, the contents of the animalList array are: mammal numberRemaining readingNumber readingDate Tiger 3745 3 24/05/2024 Polar Bear 29140 2 24/05/2024 African Wild Dog 3012 2 24/05/2024 Giant Panda 1864 1 24/05/2024 Tiger 3890 2 12/05/2024 Blue Whale 5000 2 12/05/2024 Polar Bear 31000 1 12/05/2024 Rhinoceros 5600 1 12/05/2024 Orangutan 105000 1 12/05/2024 Blue Whale 7632 1 05/05/2024 Tiger 4432 1 05/05/2024 African Wild Dog 3000 1 05/05/2024 At step 5 of the top-level design, the program will analyse the contents of the updated animalList array to identify any mammals at risk of extinction. A mammal at risk of extinction is one where the number remaining in the most recent update represents a reduction of 5% or more when compared with the previous reading. Using pseudocode, write an algorithm to display the name and percentage reduction of any mammal at risk of extinction. A wildlife society stores data on mammals around the world. This mammal data is held in a database. Whenever new readings become available they are stored in a text file. The text file is read into a program to update the mammal data. The data is then analysed to identify any mammals at risk of extinction. A sample of the data in the database is shown below. Table name: MammalData mammal numberRemaining readingNumber readingDate Tiger 3890 2 12/05/2024 Blue Whale 5000 2 12/05/2024 Polar Bear 31000 1 12/05/2024 Rhinoceros 5600 1 12/05/2024 Orangutan 105000 1 12/05/2024 Blue Whale 7632 1 05/05/2024 Tiger 4432 1 05/05/2024 African Wild Dog 3000 1 05/05/2024 Part of the top-level design of the program is shown. 1. declare data structures used in the program 2. read mammal data from database and store in array of records 3. update array of records using data from the readings file 4. add new readings to database 5. analyse the data 6. … Some of the code used to implement step 1 of the design is shown below. RECORD Animal IS { STRING mammal, INTEGER numberRemaining, INTEGER readingNumber, STRING readingDate } DECLARE animalList AS ARRAY OF Animal INITIALLY [NULL] * 1000
2.1 open connection to database server 2.2 execute SQL query to retrieve all records from database table MammalData 2.3 loop for number of rows of data 2.4 assign individual data item to an individual field 2.5 end loop 2.6 close connection to database server 1 mark for open/close connection to database server. 1 mark to import the records using SQL query. 1 mark for writing data to the array of records within a loop. Notes: - do not accept file instead of database or database server - do not penalise for loop of 1000 See sample solution below. Alternative solutions possible. 1 mark for correct complex condition for inner loop at Line 3.3.7 of the sample solution. Also accept: list(index - 1).recordNumber <= value.recordNumber and index > 0 1 mark for using inner loop to work backwards through the array (Line 3.3.9 of the sample solution). 1 mark for updating contents of array at Line 3.3.11 of the sample solution. 3.3.4 loop from n = 1 to length(list) - 1 3.3.5 set value to array(n) 3.3.6 set index to n 3.3.7 loop while index > 0 and value.readingNumber > list(index - 1) .readingNumber 3.3.8 set list(index) = list(index - 1) 3.3.9 set index to index - 1 3.3.10 end loop 3.3.11 set list(index) = value 3.3.12 end loop See sample solution below. 1 mark each for any four of the following: • counting rows of new data in temp array • counting number of animals currently in animalList array • backwards loop used to move data in animalList array • assignment of data to correct position in animalList array • creating empty records at the start of the animalList array Note: candidates may used fixed loop of 1000 to traverse animalList array. 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.10 3.4.11 3.4.12 3.4.13 3.4.14 3.4.15 3.4.16 procedure moveData (animalList()) set numberUpdateRecords = 0 set newPosition = 0 loop while field species of temporary array is not empty add 1 to numberUpdateRecords end loop loop while field species of animalList array is not empty add 1 to newPosition end loop loop backwards from (newPosition – 1) to 0 set animalList(counter + numberUpdateRecords) = animalList(counter) end loop loop from 0 to numberUpdateRecords – 1 set animalList(loop) to NULL end loop end procedure See sample solution below. Alternative solutions possible. 1 mark for matching individual update record with previous record. 1 mark for calculating percentage difference and comparing with 5% of previous value. 1 mark for displaying details required. 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.5.10 3.5.11 start loop1 from 0 to (numberUpdateRecords - 1) start loop2 from numberUpdateRecords to (number of records in animalList array – 1) if animalList(loop2).mammal = animalList(loop1).mammal and animalList (loop2).readingNumber = animalList(loop1).readingNumber - 1 then set difference = animalList(loop2).numberRemaining – animalList (loop1). numberRemaining set percentage = difference / animalList(loop2).numberRemaining * 100 if percentage >= 5 then display animalList(loop1).mammal and percentage end if end if end loop end loop