Finding the minimum in Cambridge pseudocode

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

Finding the minimum in Cambridge pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly finds a new smallest mark inside the loop?
A IF Marks[Index] < Smallest THEN
B IF Marks[Index] > Smallest THEN
2. Which is the best starting value for Smallest when the marks are 45, 72 and 60?
A Smallest ← 0
B Smallest ← Marks[1]
3. What goes wrong if Smallest starts at 0 and all the marks are positive?
A Smallest stays 0, which is not in the array
B Smallest becomes the largest mark
4. Which statement is true about the code inside the IF...ENDIF?
A Smallest ← Marks[Index] runs on every pass of the loop
B Smallest ← Marks[Index] runs only when a lower mark is found
5. Marks are 5, -3, 8, 2. Which start value for Smallest gives the correct minimum?
A Smallest ← Marks[1]
B Smallest ← 0

Match the Pairs

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

Left columnRight column
1Smallest ← Marks[1] AThe > operator is used instead of <, so this finds the largest value, not the smallest. Change it to Marks[Index] < Smallest.
2IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF BStarts the minimum as the first mark in the array, so the first real value is the one to beat.
3To find the minimum, you must test whether the current item is LESS than Smallest, not greater. CReplaces Smallest with the current mark only when that mark is lower than the smallest found so far.
4Start the minimum from an actual array value, so it works even if all the marks are above 0 or are negative. DIF Marks[Index] < Smallest THEN
5Smallest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF NEXT Index ESmallest starts at 0, so if every mark is above 0 nothing is ever smaller and the output is wrongly 0. Start with Smallest ← Marks[1].
6IF Marks[Index] > Smallest THEN Smallest ← Marks[Index] ENDIF FSmallest ← Marks[1]

Definitions

IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF

Fill in the Blanks

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

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

Spot the Error

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

Question 1
A Smallest ← 0
B FOR Index ← 2 TO 8
C IF Marks[Index] < Smallest
D THEN Smallest ← Marks[Index]
E ENDIF
F NEXT Index
Question 2
A Smallest ← Marks[1]
B FOR Index ← 2 TO 10
C IF Marks[Index] < Smallest
D THEN Marks[Index] ← Smallest
E ENDIF
F NEXT Index
Question 3
A Smallest ← Temps[1]
B FOR Index ← 2 TO 7
C IF Temps[Index] > Smallest
D THEN Smallest ← Temps[Index]
E ENDIF
F NEXT Index
Question 4
A Smallest ← Marks[1]
B FOR Index ← 2 TO 5
C IF Smallest < Marks[Index]
D THEN Smallest ← Marks[Index]
E ENDIF
F NEXT Index
Question 5
A Smallest ← Scores[1]
B FOR Count ← 2 TO 6
C IF Scores[Count] < Smallest
D THEN Smallest ← Count
E ENDIF
F NEXT Count

Answer Key

Finding the minimum in Cambridge pseudocode
Section A: Quick Questions
Q1
A — IF Marks[Index] < Smallest THEN
Q2
B — Smallest ← Marks[1]
Q3
A — Smallest stays 0, which is not in the array
Q4
B — Smallest ← Marks[Index] runs only when a lower mark is found
Q5
A — Smallest ← Marks[1]
Section B: Match the Pairs
Q1
Smallest ← Marks[1] → Starts the minimum as the first mark in the array, so the first real value is the one to beat.
Q2
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF → Replaces Smallest with the current mark only when that mark is lower than the smallest found so far.
Q3
To find the minimum, you must test whether the current item is LESS than Smallest, not greater. → IF Marks[Index] < Smallest THEN
Q4
Start the minimum from an actual array value, so it works even if all the marks are above 0 or are negative. → Smallest ← Marks[1]
Q5
Smallest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF NEXT Index → Smallest starts at 0, so if every mark is above 0 nothing is ever smaller and the output is wrongly 0. Start with Smallest ← Marks[1].
Q6
IF Marks[Index] > Smallest THEN Smallest ← Marks[Index] ENDIF → The > operator is used instead of <, so this finds the largest value, not the smallest. Change it to Marks[Index] < Smallest.
Section C: Definitions
Q1
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF — Inside a loop, this compares each item with Smallest and replaces Smallest when a lower value is found. Start Smallest as Marks[1], not 0, so the minimum is correct.
Section D: Fill in the Blanks
Q1
Smallest ← Marks[1]
Q2
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF
Q3
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF
Q4
Smallest ← Marks[1] FOR Index ← 2 TO 10 IF Marks[Index] < Smallest THEN
Q5
IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF
Section E: Spot the Error
Q1
A — Starting Smallest at 0 gives the wrong minimum. Marks are never below 0, so the IF would never replace it and the answer would stay 0. It should be Smallest ← Marks[1].
Q2
D — The assignment is the wrong way round. This overwrites the array item instead of updating Smallest. It should be THEN Smallest ← Marks[Index].
Q3
C — Using > finds the largest temperature, not the smallest. Even with negative values, the test must be IF Temps[Index] < Smallest.
Q4
C — The comparison is reversed, so Smallest is replaced by a larger value and the loop finds the maximum. It should be IF Marks[Index] < Smallest.
Q5
D — This stores the position (Count) instead of the score. Smallest must hold the value, so it should be THEN Smallest ← Scores[Count].