Finding the maximum and minimum in pseudocode

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

Finding the maximum and minimum in pseudocode

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 an array called Scores?
A Max ← Scores[0] + 1
B Max ← Scores[1]
2. Which comparison replaces Max when a larger value is found?
A IF Value > Max THEN
B IF Value < Max THEN
3. Which comparison is used to find the smallest value?
A IF Value > Min THEN
B IF Value < Min THEN
4. All the numbers in a list are negative, for example -5, -2 and -9. Which way of starting Max works?
A Set Max to the first number in the list
B Set Max to 0 before the loop
5. What is the main reason for setting Max to the first element rather than 0?
A The starting value is guaranteed to be one of the real data values
B Zero is not allowed as a value in pseudocode

Match the Pairs

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

Left columnRight column
1Max ← Numbers[1] AReplace the minimum whenever the current value is smaller than the smallest found so far
2IF Value < Min THEN Min ← Value BStarting Max at 0 is wrong because if every value is negative, Max stays 0, which is not in the data. Start with the first element instead.
3To find the largest value, replace Max only when the current value is bigger than Max CIF Value > Max THEN Max ← Value
4Start Max as the first element, then loop through the remaining elements from position 2 DStart the maximum by storing the first value in the list, so Max begins as a real value from the data
5Max ← 0 EFOR Index ← 2 TO 10
6IF Value > Min THEN Min ← Value FThe wrong operator is used. To find the minimum, the test must be Value < Min, so Min is replaced by smaller values.

Definitions

IF Value > Max THEN Max ← Value

Fill in the Blanks

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

1.
Max ← Scores[_] FOR Index ← 2 TO 10
2.
IF Scores[Index] _ Max THEN Max ← Scores[Index] ENDIF
3.
IF Value _ Min THEN Min ← Value ENDIF
4.
IF Value < Min THEN ___ ← Value ENDIF
5.
Max ← Scores[1] FOR Index ← _ TO 10 IF Scores[Index] > Max THEN

Spot the Error

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

Question 1
A Max ← Mark[1]
B FOR Index ← 2 TO 10
C IF Mark[Index] > Max THEN
D Mark[Index] ← Max
E ENDIF
F NEXT Index
Question 2
A Max ← 0
B FOR Day ← 2 TO 7
C IF Temp[Day] > Max THEN
D Max ← Temp[Day]
E ENDIF
F NEXT Day
Question 3
A Min ← Cost[1]
B FOR Item ← 2 TO 6
C IF Cost[Item] > Min THEN
D Min ← Cost[Item]
E ENDIF
F NEXT Item
Question 4
A Max ← Height[1]
B FOR N ← 2 TO 5
C IF Height[N] > Max THEN
D Max ← N
E ENDIF
F NEXT N
Question 5
A Min ← 0
B FOR Pos ← 2 TO 10
C IF Age[Pos] < Min THEN
D Min ← Age[Pos]
E ENDIF
F NEXT Pos

Answer Key

Finding the maximum and minimum in pseudocode
Section A: Quick Questions
Q1
B — Max ← Scores[1]
Q2
A — IF Value > Max THEN
Q3
B — IF Value < Min THEN
Q4
A — Set Max to the first number in the list
Q5
A — The starting value is guaranteed to be one of the real data values
Section B: Match the Pairs
Q1
Max ← Numbers[1] → Start the maximum by storing the first value in the list, so Max begins as a real value from the data
Q2
IF Value < Min THEN Min ← Value → Replace the minimum whenever the current value is smaller than the smallest found so far
Q3
To find the largest value, replace Max only when the current value is bigger than Max → IF Value > Max THEN Max ← Value
Q4
Start Max as the first element, then loop through the remaining elements from position 2 → FOR Index ← 2 TO 10
Q5
Max ← 0 → Starting Max at 0 is wrong because if every value is negative, Max stays 0, which is not in the data. Start with the first element instead.
Q6
IF Value > Min THEN Min ← Value → The wrong operator is used. To find the minimum, the test must be Value < Min, so Min is replaced by smaller values.
Section C: Definitions
Q1
IF Value > Max THEN Max ← Value — Finds the largest value: set Max to the first value, loop through the rest, and replace Max whenever a larger value is found. The minimum works the same way using < instead.
Section D: Fill in the Blanks
Q1
Max ← Scores[1] FOR Index ← 2 TO 10
Q2
IF Scores[Index] > Max THEN Max ← Scores[Index] ENDIF
Q3
IF Value < Min THEN Min ← Value ENDIF
Q4
IF Value < Min THEN Min ← Value ENDIF
Q5
Max ← Scores[1] FOR Index ← 2 TO 10 IF Scores[Index] > Max THEN
Section E: Spot the Error
Q1
D — The assignment is the wrong way round. This line would overwrite the array element with Max and Max would never change. It should be Max ← Mark[Index] so the new largest value is stored in Max.
Q2
A — Max must start as the first element, not 0. If every temperature is below 0 (for example -5), Max would stay at 0, which is not in the data. It should be Max ← Temp[1].
Q3
C — To find the minimum you replace Min when a SMALLER value is found, so the condition needs < not >. With > this would find the maximum. It should be IF Cost[Item] < Min THEN.
Q4
D — Max would store the position (the counter N) instead of the height value. It should be Max ← Height[N] so that Max holds the largest value found.
Q5
A — Starting Min at 0 means no age will ever be smaller, so Min would never be replaced and the answer would be wrong. Min must start as the first element: Min ← Age[1].