Finding the largest value in an array

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

Finding the largest value in an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts the search for the largest value in the array Marks?
A Largest ← Marks[1]
B Largest ← 0
2. Which condition correctly checks for a new largest value inside the loop?
A IF Marks[Index] > Largest THEN
B IF Marks[Index] = Largest THEN
3. What should happen inside the IF when Marks[Index] > Largest is true?
A Marks[Index] ← Largest
B Largest ← Marks[Index]
4. The array Marks has bounds 1 to 5 and Largest ← Marks[1] is already set. Which loop is the most efficient and correct?
A FOR Index ← 2 TO 5
B FOR Index ← 0 TO 5
5. How do you change the algorithm to find the smallest value?
A Start with Smallest ← Marks[1] and keep IF Marks[Index] > Smallest THEN
B Start with Smallest ← Marks[1] and use IF Marks[Index] < Smallest THEN

Match the Pairs

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

Left columnRight column
1Largest ← Marks[1] AIF Marks[Index] < Smallest THEN
2IF Marks[Index] > Largest THEN BLargestIndex ← Index
3To find the smallest value, use the same method but replace it when an element is smaller CAssume the first element is the largest so far, before any comparing starts
4To know which element holds the largest value, store its index as well when you replace Largest DLargest starts at 0 instead of the first element. If every mark were negative, 0 would wrongly stay as the answer. It should start as Largest ← Marks[1].
5Largest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index ECheck whether the current element is bigger than the largest value found so far
6IF Marks[Index] > Largest THEN Largest ← Index ENDIF FIndex is assigned to Largest instead of the element value. It should be Largest ← Marks[Index].

Definitions

Largest ← Marks[1]

Fill in the Blanks

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

1.
Largest ← Marks[_]
2.
FOR Index ← 2 TO 10 IF Marks[Index] _ Largest THEN Largest ← Marks[Index]
3.
IF Marks[Index] > Largest ____ Largest ← Marks[Index] ENDIF
4.
IF Marks[Index] _ Smallest THEN Smallest ← Marks[Index] ENDIF
5.
IF Marks[Index] > Largest THEN Largest ← Marks[Index] LargestIndex ← _____

Spot the Error

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

Question 1
A Largest ← Marks[1]
B FOR N ← 2 TO 10
C IF Marks[N] > Largest THEN
D Largest ← N
E ENDIF
F NEXT N
Question 2
A Largest ← Marks[1]
B FOR N ← 2 UNTIL 5
C IF Marks[N] > Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Question 3
A Largest ← Marks[1]
B FOR N ← 0 TO 8
C IF Marks[N] > Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Question 4
A Largest ← Marks[1]
B FOR N ← 2 TO 6
C IF Marks[N] < Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Question 5
A Smallest ← 0
B FOR N ← 2 TO 10
C IF Marks[N] < Smallest THEN
D Smallest ← Marks[N]
E ENDIF
F NEXT N

Answer Key

Finding the largest value in an array
Section A: Quick Questions
Q1
A — Largest ← Marks[1]
Q2
A — IF Marks[Index] > Largest THEN
Q3
B — Largest ← Marks[Index]
Q4
A — FOR Index ← 2 TO 5
Q5
B — Start with Smallest ← Marks[1] and use IF Marks[Index] < Smallest THEN
Section B: Match the Pairs
Q1
Largest ← Marks[1] → Assume the first element is the largest so far, before any comparing starts
Q2
IF Marks[Index] > Largest THEN → Check whether the current element is bigger than the largest value found so far
Q3
To find the smallest value, use the same method but replace it when an element is smaller → IF Marks[Index] < Smallest THEN
Q4
To know which element holds the largest value, store its index as well when you replace Largest → LargestIndex ← Index
Q5
Largest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index → Largest starts at 0 instead of the first element. If every mark were negative, 0 would wrongly stay as the answer. It should start as Largest ← Marks[1].
Q6
IF Marks[Index] > Largest THEN Largest ← Index ENDIF → Index is assigned to Largest instead of the element value. It should be Largest ← Marks[Index].
Section C: Definitions
Q1
Largest ← Marks[1] — Start by assuming the first element is the largest, then compare every other element and replace Largest when a bigger one is found.
Section D: Fill in the Blanks
Q1
Largest ← Marks[1]
Q2
FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index]
Q3
IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF
Q4
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF
Q5
IF Marks[Index] > Largest THEN Largest ← Marks[Index] LargestIndex ← Index
Section E: Spot the Error
Q1
D — Largest must hold a mark, not the loop counter. N is only the index. It should be Largest ← Marks[N].
Q2
B — A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR N ← 2 TO 5.
Q3
B — Marks starts at index 1, so index 0 does not exist and causes an out-of-range error. The loop should start at 2, because Marks[1] is already in Largest.
Q4
C — To find the largest value you replace Largest when an element is bigger. The test must use >, not <. As written, this finds the smallest.
Q5
A — Starting Smallest at 0 means no mark can be smaller, so it never changes. Start with the first element: Smallest ← Marks[1].