A shop has a unique product code for each item it sells, for example X756 - Scottish Highers Computing Science - Question 10 - 2019
Question 10
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 ... show full transcript
Worked Solution & Example Answer:A shop has a unique product code for each item it sells, for example X756 - Scottish Highers Computing Science - Question 10 - 2019
Step 1
State the value that will be returned by the function if target is X756.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The function will return the position of the target X756, which is 2, as it is located at the third index of the array (considering a zero-based index).
Step 2
Describe how the algorithm could be made more efficient.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To make the algorithm more efficient, we could use a conditional loop with an early termination. For instance, we could introduce a boolean variable to track whether the target has been found. If target is found in the array, we can break out of the loop immediately, preventing unnecessary iterations through the rest of the list.
Step 3
State the value returned by the function.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The value returned by the function will be 0, as the element F333 is not found in the array.
Step 4
State the type of error. Explain your answer.
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
The type of error is a logic error. The function returns 0, which indicates a position, but it may lead to confusion, as 0 could also correspond to the first item in the list (C232). It does not clearly differentiate between a valid index and the absence of the target.
Join the Scottish Highers students using SimpleStudy...