Three searches in exam notation — Activities 2.9 and 2.10. Two are binary searches, which is a recurring exam pattern: you are given a list and a target and asked what happens, with no computer in the room.
Watch the indexing. Every list on this page starts at 1. The same search in C# would give an index one lower.
Work these on paper. There is no computer on exam day.
a ← expression assign the result to aDISPLAY(expression) show it, FOLLOWED BY A SPACE= ≠ > < ≥ ≤ comparison; = means EQUALSaList[1] THE FIRST ELEMENT, not aList[0]LENGTH(aList) how many elementsa MOD b remainder: 17 MOD 5 is 2a / b ORDINARY division: 17 / 5 is 3.4REPEAT UNTIL(condition) repeats UNTIL it becomes true, the opposite of a while loopIF(condition) { } ELSE { }There is no rounding on the reference sheet, which is why the binary search below works out its middle with MOD, as Activity 2.10's exam panel showed.
What does this display? Write down every character, spaces included.
aList ← [8, 3, 9, 3, 5]target ← 3i ← 1count ← 0REPEAT UNTIL(i > LENGTH(aList)){ IF(aList[i] = target) { DISPLAY(i) count ← count + 1 } i ← i + 1}DISPLAY(count)Then answer this: DISPLAY puts a space after every value. Write out the exact output, and say how many characters long it is.
Build a table with a row for each pass: low, high, mid, and aList[mid]. Then say what is displayed and what is returned.
aList ← [4, 11, 19, 26, 33, 41, 58, 70, 82]
PROCEDURE search(list, target){ low ← 1 high ← LENGTH(list) REPEAT UNTIL(low > high) { sum ← low + high mid ← (sum - (sum MOD 2)) / 2 DISPLAY(mid) IF(list[mid] = target) { RETURN(mid) } IF(list[mid] < target) { low ← mid + 1 } ELSE { high ← mid - 1 } } RETURN(-1)}
result ← search(aList, 58)DISPLAY(result)Then answer this: the list has nine items and this search looked at very few of them. How many comparisons did it make, and what is the most it could ever make on a list of nine?
The same list and the same procedure as Problem 2, called with a different target. 30 is not in the list. Build the table again: what is displayed, and what is returned?
result ← search(aList, 30)DISPLAY(result)Then answer this: the loop ended because low passed high. Write down the values they held at the end, and explain what it means for the range between them to be empty.
And this: mid is written as (sum - (sum MOD 2)) / 2 rather than (low + high) / 2. Give an example where the second one gives something that is not a valid index — and say why the same line in C# would have been fine.
This is graded on completion, not accuracy. Show your working — a wrong answer with visible reasoning is worth more to both of us than a right one with none.