ExamBank Scot · Higher Computing Science question bank · All Higher Computing Science questions
6 marks · Standard algorithms
State the value that will be returned by the function if target is X756. This linear search algorithm is inefficient. Describe how the algorithm could be made more efficient. The product code ‘F333’ is entered. It is not in the array. State the value returned by the function. State the type of error. Explain your answer. Error Explanation A shop has a unique product code for each item it sells, for example X756. The linear search algorithm shown below is used to find the position of a product code in an array. Line 1 FUNCTION linearSearch (ARRAY OF STRING list) RETURNS INTEGER Line 2 DECLARE position INITIALLY 0 Line 3 DECLARE target INITIALLY "" Line 4 SEND "Enter target value" TO DISPLAY Line 5 RECEIVE target FROM KEYBOARD Line 6 FOR index FROM 0 TO length(list)-1 DO Line 7 IF target=list[index] THEN Line 8 SET position TO index Line 9 END IF Line 10 END FOR Line 11 RETURN position Line 12 END FUNCTION The array of unique product codes is shown below. C232 T546 X756 W482 B629 …….
2 • Use a conditional loop • With termination 1 mark for each bullet Termination examples include target is found, target=list[index], found =true 0 • Logic • Returns a position for an item that is not in the list OR Returns the position of the first item in the list/C232 1 mark for each bullet