Outputting the result of a linear search

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Outputting the result of a linear search

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where does the IF Found check go in a linear search?
A After the loop has finished
B Inside the loop, before the first comparison
2. Which statement outputs the position when the target has been found?
A IF Found = FALSE THEN OUTPUT "Found at ", Index
B IF Found = TRUE THEN OUTPUT "Found at ", Index
3. Data = [4, 8, 15, 16, 23] with positions 1 to 5. The target is 4, the FIRST item. What is output?
A Found at 1
B Not found
4. Data = [4, 8, 15, 16, 23] with positions 1 to 5. The target is 23, the LAST item. What is output?
A Found at 4
B Found at 5
5. Data = [4, 8, 15, 16, 23]. The target is 99, which is MISSING. What is output?
A Not found
B Found at 5

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1IF Found = TRUE THEN OUTPUT "Found at ", Index ENDIF AOutputs the position of the target, but only when the search loop has found it
2ELSE OUTPUT "Not found" BThe IF…ELSE on Found is inside the loop, so it outputs a result on every pass. It should come after NEXT Index, once the search is finished.
3If the target is the first item, the search loop finds it on its first pass, so Found is TRUE and Index is 1 for the output CRuns when the Found flag is still FALSE, so the target is missing from the array
4If the target is missing, the loop ends without changing Found, so the ELSE branch outputs a not-found message DThe two outputs are the wrong way round. TRUE should output Found at with the Index, and ELSE should output Not found.
5FOR Index ← 1 TO 10 IF List[Index] = Target THEN Found ← TRUE ENDIF IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF NEXT Index EIF Found = FALSE THEN OUTPUT "Not found" ENDIF
6IF Found = TRUE THEN OUTPUT "Not found" ELSE OUTPUT "Found at ", Index ENDIF FFound ← TRUE Index ← 1 OUTPUT "Found at ", Index

Definitions

IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
IF _____ = TRUE THEN OUTPUT "Found at ", Index ELSE
2.
IF Found = TRUE THEN OUTPUT "Found at ", _____ ELSE
3.
IF Found = TRUE THEN OUTPUT "Found at ", Index ____
4.
ELSE ______ "Not found" ENDIF
5.
ELSE OUTPUT "Not found" _____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A IF Found = TRUE THEN
B OUTPUT "Found at ", Index
C ELSE THEN
D OUTPUT "Not found"
E ENDIF
Question 2
A IF Found ← TRUE THEN
B OUTPUT "Found at ", Index
C ELSE
D OUTPUT "Not found"
E ENDIF
Question 3
A NEXT Index
B IF Found = TRUE
C OUTPUT "Found at ", Index
D ELSE
E OUTPUT "Not found"
F ENDIF
Question 4
A IF Found = TRUE THEN
B OUTPUT "Found at " Index
C ELSE
D OUTPUT "Not found"
E ENDIF
Question 5
A IF Found = TRUE THEN
B OUTPUT "Found at ", Index
C ELSE
D OUTPUT "Found at ", Index
E ENDIF

Answer Key

Outputting the result of a linear search
Section A: Quick Questions
Q1
A — After the loop has finished
Q2
B — IF Found = TRUE THEN OUTPUT "Found at ", Index
Q3
A — Found at 1
Q4
B — Found at 5
Q5
A — Not found
Section B: Match the Pairs
Q1
IF Found = TRUE THEN OUTPUT "Found at ", Index ENDIF → Outputs the position of the target, but only when the search loop has found it
Q2
ELSE OUTPUT "Not found" → Runs when the Found flag is still FALSE, so the target is missing from the array
Q3
If the target is the first item, the search loop finds it on its first pass, so Found is TRUE and Index is 1 for the output → Found ← TRUE Index ← 1 OUTPUT "Found at ", Index
Q4
If the target is missing, the loop ends without changing Found, so the ELSE branch outputs a not-found message → IF Found = FALSE THEN OUTPUT "Not found" ENDIF
Q5
FOR Index ← 1 TO 10 IF List[Index] = Target THEN Found ← TRUE ENDIF IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF NEXT Index → The IF…ELSE on Found is inside the loop, so it outputs a result on every pass. It should come after NEXT Index, once the search is finished.
Q6
IF Found = TRUE THEN OUTPUT "Not found" ELSE OUTPUT "Found at ", Index ENDIF → The two outputs are the wrong way round. TRUE should output Found at with the Index, and ELSE should output Not found.
Section C: Definitions
Q1
IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF — After the search loop, check the Found flag: if TRUE, output the position (Index) where the target was found, otherwise output a not-found message. Covers first, last and missing items.
Section D: Fill in the Blanks
Q1
IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Q2
IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Q3
IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Q4
ELSE OUTPUT "Not found" ENDIF
Q5
ELSE OUTPUT "Not found" ENDIF
Section E: Spot the Error
Q1
C — ELSE never has THEN after it in Cambridge pseudocode. Only IF takes THEN. The line should be just ELSE.
Q2
A — ← assigns a value. A condition must compare, so it should use =: IF Found = TRUE THEN.
Q3
B — The IF line is missing THEN. It should be IF Found = TRUE THEN.
Q4
B — A comma is needed between the message and the variable. It should be OUTPUT "Found at ", Index.
Q5
D — The ELSE branch runs when the target was never found, so it must output "Not found", not a position.