Finding the maximum in Cambridge pseudocode

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

Finding the maximum in Cambridge pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly sets up the maximum BEFORE the loop starts?
A Largest ← 0 (always safe, whatever the marks are)
B Largest ← Marks[1]
2. Marks has 10 elements and Largest ← Marks[1] has already been done. Which loop header is best?
A FOR Index ← 2 TO 10
B FOR Index ← 0 TO 10
3. Which IF statement keeps the biggest value inside the loop?
A IF Marks[Index] < Largest THEN
B IF Marks[Index] > Largest THEN
4. What goes inside the IF when a bigger mark is found?
A Largest ← Marks[Index]
B Marks[Index] ← Largest
5. Where should OUTPUT Largest be placed?
A After NEXT Index, once the loop has finished
B Inside the loop, before the IF

Match the Pairs

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

Left columnRight column
1Largest ← Marks[1] ALargest has no starting value, so the first comparison uses an undefined value. It should be set first with Largest ← Marks[1].
2IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF BCompare the current mark with Largest and replace Largest only if the current mark is bigger.
3The loop can start at the second element because the first element was already stored in Largest. CFOR Index ← 2 TO 10
4To also record where the maximum is, save the Index whenever a new largest value is found. DStart by assuming the first mark is the biggest so far, so Largest has a real value from the array to compare against.
5FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index OUTPUT Largest (Largest was never set before the loop) EOUTPUT Largest is inside the loop, so it prints after every item. It should come after NEXT Index so the final maximum is output once.
6FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF OUTPUT Largest NEXT Index FIF Marks[Index] > Largest THEN Largest ← Marks[Index] Position ← Index ENDIF

Definitions

IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF

Fill in the Blanks

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

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

Spot the Error

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

Question 1
A Largest ← Marks[1]
B FOR Index ← 2 TO 10
C IF Marks[Index]<Largest THEN
D Largest ← Marks[Index]
E ENDIF
F NEXT Index
Question 2
A Largest ← Marks[0]
B FOR Index ← 2 TO 10
C IF Marks[Index]>Largest THEN
D Largest ← Marks[Index]
E ENDIF
F NEXT Index
Question 3
A Largest ← Marks[1]
B FOR Index ← 2 TO 11
C IF Marks[Index]>Largest THEN
D Largest ← Marks[Index]
E ENDIF
F NEXT Index
Question 4
A FOR Index ← 2 TO 10
B IF Marks[Index]>Marks[1] THEN
C Largest ← Marks[Index]
D Position ← Index
E ENDIF
F NEXT Index
Question 5
A FOR Index ← 2 TO 10 STEP 2
B IF Marks[Index]>Largest THEN
C Largest ← Marks[Index]
D ENDIF
E NEXT Index
F OUTPUT Largest

Answer Key

Finding the maximum in Cambridge pseudocode
Section A: Quick Questions
Q1
B — Largest ← Marks[1]
Q2
A — FOR Index ← 2 TO 10
Q3
B — IF Marks[Index] > Largest THEN
Q4
A — Largest ← Marks[Index]
Q5
A — After NEXT Index, once the loop has finished
Section B: Match the Pairs
Q1
Largest ← Marks[1] → Start by assuming the first mark is the biggest so far, so Largest has a real value from the array to compare against.
Q2
IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF → Compare the current mark with Largest and replace Largest only if the current mark is bigger.
Q3
The loop can start at the second element because the first element was already stored in Largest. → FOR Index ← 2 TO 10
Q4
To also record where the maximum is, save the Index whenever a new largest value is found. → IF Marks[Index] > Largest THEN Largest ← Marks[Index] Position ← Index ENDIF
Q5
FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index OUTPUT Largest (Largest was never set before the loop) → Largest has no starting value, so the first comparison uses an undefined value. It should be set first with Largest ← Marks[1].
Q6
FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF OUTPUT Largest NEXT Index → OUTPUT Largest is inside the loop, so it prints after every item. It should come after NEXT Index so the final maximum is output once.
Section C: Definitions
Q1
IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF — Used inside a loop to compare each mark with Largest. If the mark is bigger, it replaces Largest. When the loop ends, Largest holds the biggest value in the array.
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
FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index]
Q4
IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF
Q5
NEXT Index OUTPUT Largest
Section E: Spot the Error
Q1
C — The comparison is the wrong way round. Using < only updates Largest when a mark is smaller, so it would find the minimum. It should be IF Marks[Index]>Largest THEN.
Q2
A — Cambridge pseudocode arrays start at index 1, so Marks[0] does not exist. Largest should start as the first mark: Largest ← Marks[1].
Q3
B — The array only has 10 marks, so Marks[11] does not exist and the loop would go out of bounds. The loop should end at 10: FOR Index ← 2 TO 10.
Q4
B — Each mark must be compared with Largest, which holds the biggest value so far, not always with the first mark. It should be IF Marks[Index]>Largest THEN.
Q5
A — STEP 2 makes the loop skip every other mark, so the biggest mark could be missed. Every element must be checked, so remove STEP 2.