ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
9 marks · Software analysis
Identify two boundaries of this problem. The function validNum is used to check if a number is already present in the array or not. When the code is tested an error is found. … Line 34 FUNCTION validNum(INTEGER randomNum, ARRAY OF INTEGER values) RETURNS BOOLEAN Line 35 DECLARE valid INITIALLY FALSE Line 36 FOR index FROM 0 TO length(values) − 1 DO Line 37 IF values[index] = randomNum THEN Line 38 SET valid TO FALSE Line 39 END IF Line 40 END FOR Line 41 RETURN valid Line 42 END FUNCTION … Line 63 DECLARE randomList as ARRAY OF INTEGER INITIALLY [0] * <size of array> Line 64 FOR index FROM 0 TO length(randomList) − 1 DO Line 65 randomVal = <random number between minimum value and maximum value> Line 66 WHILE validNum(randomVal,randomList) = FALSE Line 67 randomVal = <random number between minimum value and maximum value> Line 68 END WHILE Line 69 SET randomList[index] TO randomVal Line 70 END FOR … The error is found to be in the function validNum shown in lines 34 to 42. Identify the error in the function. Describe the effect of this error when lines 63 to 70 are executed. The error has been corrected and the code now executes as expected. Describe how the validNum function could be made more efficient. Formal and actual parameters are used in the code. Identify a formal parameter for the validNum function and its associated actual parameter. Formal parameter Actual parameter A program should generate a list of unique random integers between a lower and upper limit as chosen by the user. Enter the number of values 7 Enter the minimum value 0 Enter the maximum value 20 The values are 1,5,7,9,13,15,19
• All inputs must be integers • Number of values must be <= maximum value – minimum value • Maximum value > minimum value /minimum value < maximum value • Number of values >0 Award 1 mark for each bullet. Maximum 2 marks Do not award marks that refer to the output or generation of random numbers Line 35 valid should be set to TRUE OR Function always returns valid as FALSE Valid is initially set to false and, in the decision can only be set to false again, meaning it can never be set to true • While condition at 66 will always be met/valid is always FALSE • Infinite loop • Use conditional loop • To exit as soon as the number is present in the array/randomNum is present in values/array OR valid = False Formal randomNum values Actual randomVal randomList Award 2 marks for correct pair Award 1 mark for: • Correct pair of parameters the wrong way round OR • Two formal or two actual parameters