ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
14 marks · Sub-programs, parameters and scope
The code above limits the scope of several variables. Explain what is meant by the scope of a variable. Describe one benefit of limiting the scope of a variable in modular programming. Describe the operation of line 22 during the execution of this program. One test case is to enter the number 99 when prompted at line 21. The expected output would be [42,12,67]. Complete the trace table below by identifying the missing values in the highlighted boxes. Line number newList position index 5 [0,0] 6 0 7 0 9 [42,0] 10 7 1 9 10 7 Describe how a debugging technique could be used to check the values in the trace table. Explain why the code does not produce the correct output. Describe the additional code that should be added to the function to make it fit for purpose. Evaluate the robustness of the function. Explain your answer. A function is written to remove a specified number from an array and return a new array without that number. If the number is not present then the original array should be returned. For example, if the value 12 is to be removed from the array [42, 12, 67] then the array [42, 67] is returned. … Line 4 FUNCTION removal(ARRAY OF INTEGER values, INTEGER target) RETURNS ARRAY OF INTEGER Line 5 DECLARE newList INITIALLY [0] * length(values)−1 Line 6 DECLARE position INITIALLY 0 Line 7 FOR index FROM 0 TO length(values)−1 DO Line 8 IF values[index] ≠ target THEN Line 9 SET newList[position] TO values[index] Line 10 SET position TO position+1 Line 11 END IF Line 12 END FOR Line 13 RETURN newList Line 14 END FUNCTION … … Line 20 DECLARE numbers INITIALLY [42,12,67] Line 21 DECLARE deleteValue AS INTEGER INITIALLY FROM KEYBOARD Line 22 newNums = removal(numbers,deleteValue) Line 23 SEND newNums TO DISPLAY
Area of code in which a variable is usable/accessible. • prevents errors with variables with the same name in different modules • more memory efficient as variables will not be in memory when not in use Award 1 mark for any one bullet. • calls the function removal • assigns the result to the variable newNums • passed in the (actual) parameters Award 1 mark for each bullet. Maximum 2 marks. Accept expression of line 4 as removal function. • both position updates • update of index • array update to [42,12] Line Number newList position index 5 [0,0] 6 0 7 0 9 [42,0] 10 1 7 1 9 [42,12] 10 2 7 2 • allows execution of the program to be halted • allows the contents of the variables to be compared • array to move values into is set up as one less than the original array • so cannot hold all values if no value was removed • (add code to) check if the value is present • (add code to) run original code/return original array instead of running code OR • (add code to) declare array of the same size as the target array (that is passed in) Candidates can count occurrences of the value to be removed for bullet 1 and then may reference adding code to reduce arrays by multiple elements. • (it is not robust) it will crash when trying to reference an array element that doesn’t exist • (it is not robust) program will crash if the array passed in is empty Award 1 mark for any one bullet.