Tracing maximum and minimum algorithms

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

Tracing maximum and minimum algorithms

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table row has Largest = 5 and Number = 3. What is Largest after IF Number > Largest THEN?
A Largest becomes 3, because Number is the newest value
B Largest stays 5, because 3 > 5 is false
2. Largest = 7 and the next Number is also 7. What happens in the trace table?
A Largest is updated to 7, because the values are equal
B Largest stays 7, because 7 > 7 is false
3. Smallest = 8 and Number = 4. What goes in the comparison column for IF Number < Smallest?
A TRUE, so Smallest changes to 4
B FALSE, so Smallest stays 8
4. Largest = 12 and Number = 20. Which is correct for this row of the trace table?
A Comparison TRUE, Largest becomes 20
B Comparison FALSE, Largest stays 12
5. The data is 4, 9, 6, 9, 11 and Largest starts at 4. How many times does the Largest cell change after the start?
A 2 times (to 9, then to 11)
B 3 times (to 9, to 9 again, then to 11)

Match the Pairs

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

Left columnRight column
1IF Number > Largest THEN Largest ← Number ANumber | Number > Largest | Largest 9 | TRUE | 9 4 | FALSE | 9
2IF Number < Smallest THEN Smallest ← Number B3 > 8 is FALSE, not TRUE. Largest must stay 8 because 3 is smaller, so it must not change to 3.
3A comparison column in the trace table records TRUE or FALSE for the test. Largest changes only on TRUE rows. COn the row where Number is 2, Smallest should change to 2 because 2 < 5 is TRUE. Smallest was wrongly left at 5.
4A tie does not change the stored value: if Number equals Largest, the test Number > Largest is FALSE. DSmallest changes only on rows where Number is strictly lower than the stored Smallest; otherwise its cell stays the same as the row above.
5Number = 3, Largest = 8 3 > 8 is TRUE, so Largest becomes 3 ELargest = 7, Number = 7 7 > 7 is FALSE, so Largest stays 7
6Number | Largest | Smallest 5 | 5 | 5 2 | 5 | 5 FLargest changes only on rows where Number is strictly bigger than the stored Largest; otherwise its trace table cell is copied down unchanged.

Definitions

IF Number > Largest THEN

Fill in the Blanks

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

1.
IF Number _ Largest THEN
2.
IF Number _ Smallest THEN
3.
IF Number > Largest THEN Largest ← ______ ENDIF
4.
IF Number > _______ THEN Largest ← Number ENDIF
5.
IF Number < Smallest THEN Smallest ← Number _____

Spot the Error

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

Question 1
A INPUT Number
B Largest ← Number
C INPUT Number
D IF Number < Largest THEN
E Largest ← Number
F ENDIF
Question 2
A Smallest ← 100
B INPUT Number
C IF Number < Smallest
D Smallest ← Number
E ENDIF
Question 3
A Largest ← 0
B Smallest ← 100
C INPUT Number
D IF Number > Smallest THEN
E Largest ← Number
F ENDIF
Question 4
A Largest ← Mark[1]
B FOR N ← 2 TO 8
C IF Mark[N] > Mark[N-1] THEN
D Largest ← Mark[N]
E ENDIF
F NEXT N
Question 5
A Largest ← 0
B INPUT Number
C IF Number > 0 THEN
D Largest ← Number
E ENDIF

Answer Key

Tracing maximum and minimum algorithms
Section A: Quick Questions
Q1
B — Largest stays 5, because 3 > 5 is false
Q2
B — Largest stays 7, because 7 > 7 is false
Q3
A — TRUE, so Smallest changes to 4
Q4
A — Comparison TRUE, Largest becomes 20
Q5
A — 2 times (to 9, then to 11)
Section B: Match the Pairs
Q1
IF Number > Largest THEN Largest ← Number → Largest changes only on rows where Number is strictly bigger than the stored Largest; otherwise its trace table cell is copied down unchanged.
Q2
IF Number < Smallest THEN Smallest ← Number → Smallest changes only on rows where Number is strictly lower than the stored Smallest; otherwise its cell stays the same as the row above.
Q3
A comparison column in the trace table records TRUE or FALSE for the test. Largest changes only on TRUE rows. → Number | Number > Largest | Largest 9 | TRUE | 9 4 | FALSE | 9
Q4
A tie does not change the stored value: if Number equals Largest, the test Number > Largest is FALSE. → Largest = 7, Number = 7 7 > 7 is FALSE, so Largest stays 7
Q5
Number = 3, Largest = 8 3 > 8 is TRUE, so Largest becomes 3 → 3 > 8 is FALSE, not TRUE. Largest must stay 8 because 3 is smaller, so it must not change to 3.
Q6
Number | Largest | Smallest 5 | 5 | 5 2 | 5 | 5 → On the row where Number is 2, Smallest should change to 2 because 2 < 5 is TRUE. Smallest was wrongly left at 5.
Section C: Definitions
Q1
IF Number > Largest THEN — In a trace table, Largest only changes on rows where this test is true. If Number is equal or smaller, the Largest cell stays the same as the row above. Smallest uses < instead.
Section D: Fill in the Blanks
Q1
IF Number > Largest THEN
Q2
IF Number < Smallest THEN
Q3
IF Number > Largest THEN Largest ← Number ENDIF
Q4
IF Number > Largest THEN Largest ← Number ENDIF
Q5
IF Number < Smallest THEN Smallest ← Number ENDIF
Section E: Spot the Error
Q1
D — Largest should only change when the new Number is bigger, so the test must use >. With < the trace table would store smaller values and Largest would go down instead of up.
Q2
C — Every IF in Cambridge pseudocode needs THEN at the end of the condition line. It should be IF Number < Smallest THEN.
Q3
D — The test that decides whether Largest changes must compare Number with Largest, not Smallest. It should be IF Number > Largest THEN.
Q4
C — This compares each mark with the previous mark, not with the stored maximum. Largest must be the thing being compared: IF Mark[N] > Largest THEN.
Q5
C — The comparison must be against the stored value Largest, not 0. Testing against 0 is true for every positive number, so Largest would change on almost every row of the trace table.