ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions
16 marks · Records and parallel arrays
Using the data in the readingsArray, the program must calculate the average rainfall for each local authority and store the results in an array of 32 records called rainfallArray. The structure of each record in the rainfallArray array is shown below. RECORD rainfall IS { STRING local, INTEGER averageRainfall } Using pseudocode, design an algorithm that will extract a unique list of local authority names from the readingsArray and store them in the rainfallArray. The local authority names stored in the rainfallArray array must be arranged in alphabetical order using the insertion sort algorithm. An incomplete algorithm for an insertion sort is shown below. 1. set temp = "" 2. set position = 0 3. for loop = 1 to 31 do 4. set temp to rainfallArray[loop].local 5. set position = loop 6. while ___________________________________ 7. ___________________________________________ 8. set position = position − 1 9. end while 10. _________________________________________ 11. end loop Using pseudocode, write the instructions needed to complete the insertion sort design at lines 6, 7 and 10. In the calculation of the average rainfall for each local authority, any calculated averages with a decimal value must be rounded to the nearest integer. Using pseudocode, design an algorithm to calculate the average rainfall for each local authority and store the results in the rainfallArray. The program is required to compare the average rainfall for four local authorities. Users should be able to enter the names of four local authorities. The program will then use the binary search algorithm to find each local authority and its associated average rainfall. The output from the program will be a diagram showing the average rainfall for each of the four local authorities entered by the user. Sample output from the program is shown below. Average rainfall (cm) for the requested local authorities Borders ************** Inverclyde *********** Stirling ***** Highlands **************** Using pseudocode, design an algorithm to: • enter the names of the local authorities requested by the user • apply the binary search algorithm to find the rainfall data for each local authority requested by the user • display the diagram as shown in the sample output with one asterisk (*) representing 1 cm of average rainfall. EnviroScot has set up 500 weather stations across Scotland to track the effect of climate change. Several weather stations have been placed in each of the 32 local authorities to ensure that the data gathered covers all areas of the country. The rainfall (cm) for 28 February 2023 from all 500 weather stations has been stored in a CSV file. A program is being developed to analyse the data. The structure diagram below shows the design of the program. Calculate average rainfall for each local authority Climate Tracker Extract unique local authority Insertion sort Read weather Compare local local authority data from file authority data names names At the start of the program, the data is read from the CSV file into the program and stored in an array of 500 records called readingsArray with the following record structure: RECORD reading IS { STRING place, STRING authority, INTEGER rainfall } A sample of the data held in readingsArray is shown below. place authority rainfall Inverurie Aberdeenshire 5 Plockton Highland 16 Inverary Argyll and Bute 14 Braemar Aberdeenshire 6 Cumbernauld North Lanarkshire 11 Dunstaffnage Argyll and Bute 15 Nairn Highland 6 Paisley Renfrewshire 11 Wick Highland 7 … … …
See one possible algorithm below. Award 1 mark for traversing readings array Award 1 mark for identifying unique value in readings array Award 1 mark for storing each unique LA found in an appropriate position of the rainfall array 1. set readings position = 0 2. set numLAFound = 0 3. while numLAFound < 32 4. if LA for current reading is not already stored in the rainfall array then 5. 6. store LA in rainfall array at numLAFound position add 1 to numLAFound 7. end if 8. add 1 to readings position 9. end while Line 6 (position > 0) and (temp < rainfallArray[position-1].local) do Line 7 set rainfallArray[position].local = rainfallArray[position-1].local Line 10 set rainfallArray[position].local = temp Award 1 mark for complex condition at Line 6. Note: Accept while(position > 0) and rainfallArray[position-1].local > temp as an alternative answer. Award 1 mark for moving larger values to the next position in the array at Line 7 Award 1 mark for inserting current value in gap created by move at Line 10 See possible algorithm below. Award 1 mark for loop needed to process each LA average (loop of 32) Award 1 mark for traversing readingsArray to find each occurrence of unique local authority names Award 1 mark for updating of counter to record number of readings for each local authority. Award 1 mark for calculation of average with rounding to the nearest integer. Note: no mark should be awarded for the use of the INT function. 1. for loop1 = 0 to 31 do 2. set total = 0 3. set average = 0 4. set count = 0 5. for loop2 = 0 to 499 do 6. if rainfallArray[loop1].local= readingsArray[loop2].authority then 7. set total = total + readingsArray[loop2].rainfall 8. set count = count + 1 9. end if 10. end loop 2 11. set average = round( total / count) 12. set rainfallArray[loop1].averageRainfall = average 13. end loop 1 Note: Candidates need to: Initial total and number of readings Traverse the readingsArray to find unique LA names Add the matching rainfall figure to the running total Update number of readings found Calculate and round the average Store in the rainfallArray array See possible algorithm below. Award 1 mark for asking user to enter 4 local authorities AND displaying bar chart heading Award 1 mark for correctly initialising and updating found and mid needed for the binary search Award 1 mark for conditional loop needed for the binary search Award 1 mark for correctly initialising and updating low and high needed for the binary search Award 1 mark for using result of binary search in the display (equivalent of Line 21) Award 1 mark for displaying location name together with stars used to represent the average rainfall for each selected LA. 1. display bar chart heading 2. for user = 1 to 4 do 3. ask user for the name of a local authority 4. store value entered in location 5. set low = 0 6. set high = 31 7. found = false 8. while not(found) and low <= high 9. set mid = (low + high)/2 10. if rainfallArray[mid].local = location then 11. set found = true 12. end if 13. if rainfallArray[mid].local < location then 14. set low = mid + 1 15. end if 16. if rainfallArray[mid].local > location then 17. set high = mid – 1 18. end if 19. end while loop 20. display location 21. for stars = 0 to (rainfallArray[mid].averageRainfall – 1) do 22. display "*" on same line as location 23. end stars loop 24. display new line 25. end user loop