← IGCSE Computer Science Paper 2

Finding the largest value in an array

Start by assuming the first element is the largest, then compare every other element and replace Largest when a bigger one is found.

Understanding Finding the largest value in an array in IGCSE Computer Science Paper 2

What it does: Finding the smallest works like finding the largest, but the comparison is reversed from > to <. Inside the IF, saving the current Index in a second variable records the position of the largest value as well as the value itself. Starting with the first element works for any data. Starting at 0 fails if every mark is negative, and it does not use the array's own values. A new largest is found only when the current element is greater than Largest. Checking for equality finds duplicates, not bigger values. Element 1 is already in Largest, so the loop can start at 2. Index 0 is outside the bounds 1 to 5 and causes an index out of range error.

Common mistakes: Largest must hold a mark, not the loop counter. N is only the index. It should be Largest ← Marks[N]. A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR N ← 2 TO 5.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 459–468) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly starts the search for the largest value in the array Marks?
Largest ← Marks[1]Largest ← 0
Matches
Largest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index
↓
Largest starts at 0 instead of the first element. If every mark were negative, 0 would wrongly stay as the answer. It should start as Largest ← Marks[1].
Applying
Meanings
Term
Largest ← Marks[1]
Meaning
Start by assuming the first element is the largest, then compare every other element and replace Largest when a bigger one is found.
Practice
Largest ← Marks[1]
Testing
Gaps
Largest ← Marks[]
Check Answer
Spots
3 IF Marks[N] > Largest THEN
4 Largest ← N

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Fennec Fox' badge — Listener, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly starts the search for the largest value in the array Marks?
A. Largest ← Marks[1] ✓
B. Largest ← 0
Starting with the first element works for any data. Starting at 0 fails if every mark is negative, and it does not use the array's own values.
2. Which condition correctly checks for a new largest value inside the loop?
A. IF Marks[Index] > Largest THEN ✓
B. IF Marks[Index] = Largest THEN
A new largest is found only when the current element is greater than Largest. Checking for equality finds duplicates, not bigger values.
3. What should happen inside the IF when Marks[Index] > Largest is true?
A. Marks[Index] ← Largest
B. Largest ← Marks[Index] ✓
Largest must be replaced by the bigger element. The wrong answer overwrites the array element with the old largest and destroys data.
4. The array Marks has bounds 1 to 5 and Largest ← Marks[1] is already set. Which loop is the most efficient and correct?
A. FOR Index ← 2 TO 5 ✓
B. FOR Index ← 0 TO 5
Element 1 is already in Largest, so the loop can start at 2. Index 0 is outside the bounds 1 to 5 and causes an index out of range error.
5. How do you change the algorithm to find the smallest value?
A. Start with Smallest ← Marks[1] and keep IF Marks[Index] > Smallest THEN
B. Start with Smallest ← Marks[1] and use IF Marks[Index] < Smallest THEN ✓
Finding the smallest works the same way, but the comparison is reversed. Replace Smallest when a smaller value is found, using <.

Match the Pairs

Largest ← Marks[1] → Assume the first element is the largest so far, before any comparing starts
IF Marks[Index] > Largest THEN → Check whether the current element is bigger than the largest value found so far
To find the smallest value, use the same method but replace it when an element is smaller → IF Marks[Index] < Smallest THEN
To know which element holds the largest value, store its index as well when you replace Largest → LargestIndex ← Index
Largest ← 0 FOR Index ← 1 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF NEXT Index → Largest starts at 0 instead of the first element. If every mark were negative, 0 would wrongly stay as the answer. It should start as Largest ← Marks[1].
IF Marks[Index] > Largest THEN Largest ← Index ENDIF → Index is assigned to Largest instead of the element value. It should be Largest ← Marks[Index].

Key Term

Largest ← Marks[1]
Start by assuming the first element is the largest, then compare every other element and replace Largest when a bigger one is found.

Fill in the Blanks

1. Largest ← Marks[1]
Hint: Type the index of the first element, which is used as the starting largest value.
2. FOR Index ← 2 TO 10 IF Marks[Index] > Largest THEN Largest ← Marks[Index]
Hint: Type the comparison operator that checks whether the current element is bigger than the largest so far.
3. IF Marks[Index] > Largest THEN Largest ← Marks[Index] ENDIF
Hint: Type the keyword that follows the condition in an IF statement.
4. IF Marks[Index] < Smallest THEN Smallest ← Marks[Index] ENDIF
Hint: Type the comparison operator that checks whether the current element is smaller than the smallest so far.
5. IF Marks[Index] > Largest THEN Largest ← Marks[Index] LargestIndex ← Index
Hint: Type the variable that holds the current position in the array so the index of the largest can be stored.

Spot the Error

Question 1
A Largest ← Marks[1]
B FOR N ← 2 TO 10
C IF Marks[N] > Largest THEN
D Largest ← N
E ENDIF
F NEXT N
Line D — Largest must hold a mark, not the loop counter. N is only the index. It should be Largest ← Marks[N].
Question 2
A Largest ← Marks[1]
B FOR N ← 2 UNTIL 5
C IF Marks[N] > Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Line B — A count-controlled loop uses the keyword TO, not UNTIL. It should be FOR N ← 2 TO 5.
Question 3
A Largest ← Marks[1]
B FOR N ← 0 TO 8
C IF Marks[N] > Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Line B — Marks starts at index 1, so index 0 does not exist and causes an out-of-range error. The loop should start at 2, because Marks[1] is already in Largest.
Question 4
A Largest ← Marks[1]
B FOR N ← 2 TO 6
C IF Marks[N] < Largest THEN
D Largest ← Marks[N]
E ENDIF
F NEXT N
Line C — To find the largest value you replace Largest when an element is bigger. The test must use >, not <. As written, this finds the smallest.
Question 5
A Smallest ← 0
B FOR N ← 2 TO 10
C IF Marks[N] < Smallest THEN
D Smallest ← Marks[N]
E ENDIF
F NEXT N
Line A — Starting Smallest at 0 means no mark can be smaller, so it never changes. Start with the first element: Smallest ← Marks[1].

Student with a class code? Join your class →