Finding the maximum: how it works

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

Finding the maximum: how it works

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the best way to start a maximum search on a list of values?
A Set Max to the first value in the list
B Set Max to the last value you check
2. Max is 14 and the next value is 9. What happens to Max?
A Max becomes 9
B Max stays 14
3. Max is 14 and the next value is 20. What happens to Max?
A Max becomes 20
B Max stays 14
4. Which line correctly replaces the maximum when a larger value is found?
A IF Num < Max THEN Max ← Num
B IF Num > Max THEN Max ← Num
5. The values are -5, -2, -9. Max is set to 0 before the loop. What is the result?
A Max stays 0, which is wrong because 0 is not in the data
B Max becomes -2, which is correct

Match the Pairs

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

Left columnRight column
1Max ← Numbers[1] AMax ← Num
2IF Numbers[Index] > Max THEN Max ← Numbers[Index] BStarting Max at 0 fails when all values are negative. Max would stay 0, which is not in the list. Start with Max ← Numbers[1].
3Compare each later value with the largest so far by looping from the second item to the last CReplace the largest value so far when a later value is bigger
4Keep the largest value seen so far in a variable which is updated when a bigger value is found DStart the maximum with the first value in the list, so it is a real value from the data
5Max ← 0 FOR Index ← 1 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index] NEXT Index EFOR Index ← 2 TO 5
6IF Num < Max THEN Max ← Num FThe wrong comparison is used. < finds a smaller value, so this finds the minimum. It should be IF Num > Max.

Definitions

IF Num > Max THEN Max ← Num

Fill in the Blanks

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

1.
Max ← Numbers[_]
2.
Max ← Numbers[1] FOR Index ← _ TO 5
3.
FOR Index ← 2 TO 5 IF Numbers[Index] _ Max THEN Max ← Numbers[Index]
4.
IF Numbers[Index] > Max THEN Max ← Numbers[_____] ENDIF
5.
NEXT Index OUTPUT "Largest: ", ___

Spot the Error

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

Question 1
A Max ← Scores[1]
B FOR i = 2 TO 5
C IF Scores[i] > Max THEN
D Max ← Scores[i]
E ENDIF
F NEXT i
Question 2
A Max ← 0
B FOR i ← 2 TO 6
C IF Ages[i] > Max THEN
D Max ← Ages[i]
E ENDIF
F NEXT i
Question 3
A Max ← Weights[1]
B FOR i ← 2 TO 6
C IF Weights[i] > Max THEN
D Max ← i
E ENDIF
F NEXT i
Question 4
A Max ← Heights[1]
B FOR i ← 2 TO 8
C IF Heights[i] < Max THEN
D Max ← Heights[i]
E ENDIF
F NEXT i
Question 5
A Highest ← Temp[1]
B FOR n ← 2 TO 7
C IF Temp[n] > Highest THEN
D Highest ← Temp[1]
E ENDIF
F NEXT n

Answer Key

Finding the maximum: how it works
Section A: Quick Questions
Q1
A — Set Max to the first value in the list
Q2
B — Max stays 14
Q3
A — Max becomes 20
Q4
B — IF Num > Max THEN Max ← Num
Q5
A — Max stays 0, which is wrong because 0 is not in the data
Section B: Match the Pairs
Q1
Max ← Numbers[1] → Start the maximum with the first value in the list, so it is a real value from the data
Q2
IF Numbers[Index] > Max THEN Max ← Numbers[Index] → Replace the largest value so far when a later value is bigger
Q3
Compare each later value with the largest so far by looping from the second item to the last → FOR Index ← 2 TO 5
Q4
Keep the largest value seen so far in a variable which is updated when a bigger value is found → Max ← Num
Q5
Max ← 0 FOR Index ← 1 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index] NEXT Index → Starting Max at 0 fails when all values are negative. Max would stay 0, which is not in the list. Start with Max ← Numbers[1].
Q6
IF Num < Max THEN Max ← Num → The wrong comparison is used. < finds a smaller value, so this finds the minimum. It should be IF Num > Max.
Section C: Definitions
Q1
IF Num > Max THEN Max ← Num — Keep the largest value seen so far in Max. Start Max with the first value, then replace it whenever a later value is larger. Starting at 0 can fail if all values are negative.
Section D: Fill in the Blanks
Q1
Max ← Numbers[1]
Q2
Max ← Numbers[1] FOR Index ← 2 TO 5
Q3
FOR Index ← 2 TO 5 IF Numbers[Index] > Max THEN Max ← Numbers[Index]
Q4
IF Numbers[Index] > Max THEN Max ← Numbers[Index] ENDIF
Q5
NEXT Index OUTPUT "Largest: ", Max
Section E: Spot the Error
Q1
B — A FOR loop in Cambridge pseudocode sets its counter with the assignment arrow ←, not =. It should be FOR i ← 2 TO 5. The loop starts at 2 because Scores[1] is already in Max.
Q2
A — Starting Max at 0 can fail. If every value is negative, none will beat 0 and the wrong answer stays in Max. Max should start with the first value, Max ← Ages[1].
Q3
D — This stores the index i in Max, not the value found at that position. It should be Max ← Weights[i].
Q4
C — The < operator replaces Max with smaller values, so this finds the minimum. To find the maximum, the test must be Heights[i] > Max.
Q5
D — Highest is reset to the first value every time instead of the larger value just found. It should be Highest ← Temp[n].