Searching an array for a value

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

Searching an array for a value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Before the search loop starts, how should the Found flag be set up?
A Found ← FALSE
B Found ← TRUE
2. Names is declared as ARRAY[1:10] OF STRING. Which loop header checks every element?
A FOR Index ← 1 TO 10
B FOR Index ← 1 TO 9
3. Which line correctly checks whether the current element is the wanted value?
A IF Names[Index] = SearchName THEN
B IF Names[Index] ← SearchName THEN
4. A match has been found. Which pair of statements should run?
A Found ← FALSE and Position ← SearchName
B Found ← TRUE and Position ← Index
5. Which statement about a linear search is correct?
A It only checks the middle element of the array
B It compares the elements one at a time, using a loop

Match the Pairs

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

Left columnRight column
1Found ← FALSE AThe not found message is output inside the loop for every element that does not match, instead of once after the loop has checked all elements.
2IF Names[Index] = Target THEN BIF Found = FALSE THEN OUTPUT "Not found"
3When a match is found, record its position by storing the loop counter in a variable CPosition ← Index
4After the loop ends, if the flag is still FALSE, tell the user that the value is not in the array DThe Found flag is set to TRUE before the loop and is never set inside it, so the program thinks the value was found even when it was not.
5FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Found ← TRUE ELSE OUTPUT "Not found" ENDIF NEXT Index ECompare the current element with the wanted value to see if it is a match
6Found ← TRUE FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Position ← Index ENDIF NEXT Index FSet a flag before the loop starts to show that the wanted value has not been found yet

Definitions

linear search

Fill in the Blanks

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

1.
Found ← _____ FOR Index ← 1 TO 10 IF Marks[Index] = Target THEN
2.
DECLARE Names : ARRAY[1:10] OF STRING FOR Index ← 1 TO __ IF Names[Index] = Target THEN
3.
FOR Index ← 1 TO 10 IF Marks[Index] _ Target THEN Found ← TRUE
4.
IF Marks[Index] = Target THEN Found ← TRUE Position ← _____
5.
IF Found = FALSE THEN ______ "Target not found" ENDIF

Spot the Error

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

Question 1
A Found ← FALSE
B FOR Index ← 1 TO 10
C IF Marks[Index] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT Index
Question 2
A Found ← FALSE
B FOR Index ← 1 TO 5
C IF Names[Index] ← Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index
Question 3
A Found ← TRUE
B FOR Index ← 1 TO 8
C IF Codes[Index] = Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index
Question 4
A Found ← FALSE
B FOR Index ← 1 TO 8
C IF Codes[Index] = Wanted
D Found ← TRUE
E ENDIF
F NEXT Index
Question 5
A Found = FALSE
B FOR Index ← 1 TO 4
C IF Ids[Index] = Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index

Answer Key

Searching an array for a value
Section A: Quick Questions
Q1
A — Found ← FALSE
Q2
A — FOR Index ← 1 TO 10
Q3
A — IF Names[Index] = SearchName THEN
Q4
B — Found ← TRUE and Position ← Index
Q5
B — It compares the elements one at a time, using a loop
Section B: Match the Pairs
Q1
Found ← FALSE → Set a flag before the loop starts to show that the wanted value has not been found yet
Q2
IF Names[Index] = Target THEN → Compare the current element with the wanted value to see if it is a match
Q3
When a match is found, record its position by storing the loop counter in a variable → Position ← Index
Q4
After the loop ends, if the flag is still FALSE, tell the user that the value is not in the array → IF Found = FALSE THEN OUTPUT "Not found"
Q5
FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Found ← TRUE ELSE OUTPUT "Not found" ENDIF NEXT Index → The not found message is output inside the loop for every element that does not match, instead of once after the loop has checked all elements.
Q6
Found ← TRUE FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Position ← Index ENDIF NEXT Index → The Found flag is set to TRUE before the loop and is never set inside it, so the program thinks the value was found even when it was not.
Section C: Definitions
Q1
linear search — A loop checks every element against the wanted value. On a match, set Found to TRUE and record the index. If Found is still FALSE at the end, OUTPUT a not found message.
Section D: Fill in the Blanks
Q1
Found ← FALSE FOR Index ← 1 TO 10 IF Marks[Index] = Target THEN
Q2
DECLARE Names : ARRAY[1:10] OF STRING FOR Index ← 1 TO 10 IF Names[Index] = Target THEN
Q3
FOR Index ← 1 TO 10 IF Marks[Index] = Target THEN Found ← TRUE
Q4
IF Marks[Index] = Target THEN Found ← TRUE Position ← Index
Q5
IF Found = FALSE THEN OUTPUT "Target not found" ENDIF
Section E: Spot the Error
Q1
D — When the element matches the target, Found must be set to TRUE. Setting it to FALSE means a match is never recorded. It should be Found ← TRUE.
Q2
C — The IF condition compares two values, so it needs the comparison operator =. The ← arrow is only for assignment. It should be IF Names[Index] = Wanted THEN.
Q3
A — The flag must start as FALSE before the search begins, because nothing has been found yet. Starting as TRUE would make the search report a match even when there is none. It should be Found ← FALSE.
Q4
C — An IF statement must have THEN after its condition. It should be IF Codes[Index] = Wanted THEN.
Q5
A — Giving the flag its starting value is an assignment, so it needs the ← arrow. Using = only compares values and does not store FALSE in Found. It should be Found ← FALSE.