AS Level · Topic 10.2
A-Level Computer Science 9618 / Topic 10 / AS

Many values, one name.

An array stores a fixed number of values of the same data type under one identifier, each reached by its index. One index gives a list; two give a table. Learn the vocabulary examiners use, then process whole arrays with loops.

The key idea

An array is a fixed-length data structure of elements that all have the same data type, held under one identifier. Each element is reached by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last, so ARRAY[1:30] has 30 elements. A 1D array is a list with one index; a 2D array is a table reached by [row, column]. Arrays are processed one element at a time with a FOR loop; a 2D array needs nested loops.

Scores : ARRAY[1:6] OF INTEGER 1231731259 123456 lower bound upper bound Marks : ARRAY[1:3, 1:4] OF INTEGER 5869 77104 3986 row 1row 2row 3 Marks[2, 3] = 10
Fig. 1: a 1D array of six integers and a 2D array of 3 rows by 4 columns
Section 01

One dimension.

Declare the array with its bounds and data type, then use a loop variable as the index to visit every element in turn.

DECLARE Scores : ARRAY[1:6] OF INTEGER
DECLARE Max : INTEGER
DECLARE MaxIndex : INTEGER
DECLARE i : INTEGER
Max ← Scores[1]
MaxIndex ← 1
FOR i ← 2 TO 6
    IF Scores[i] > Max THEN
        Max ← Scores[i]
        MaxIndex ← i
    ENDIF
NEXT i
OUTPUT "Highest ", Max, " at position ", MaxIndex
Worked example Trace table
Scores holds 12, 31, 7, 31, 25, 9 (Fig. 1). Trace the pseudocode above and state the output.
  1. Before the loop: Max = 12, MaxIndex = 1.
  2. i = 2: 31 > 12 is TRUE, so Max = 31, MaxIndex = 2.
  3. i = 3: 7 > 31 is FALSE. i = 4: 31 > 31 is FALSE (not strictly greater), so MaxIndex stays 2.
  4. i = 5: 25 > 31 FALSE. i = 6: 9 > 31 FALSE. The loop ends.
Answer:
iScores[i]Scores[i] > MaxMaxMaxIndex
121
231TRUE312
37FALSE
431FALSE
525FALSE
69FALSE

Output: Highest 31 at position 2. Because the test is > and not >=, the first position of a tied maximum is kept.

Your turn
Write a FOR loop that sets every element of Names : ARRAY[1:30] OF STRING to the empty string.
Examiner trap

Start the maximum at the first element, Max ← Scores[1], not at 0: if every value were negative, a start of 0 would give the wrong answer.

Section 02

Two dimensions.

In the lab, run the 2D array program: it fills a 2 by 3 table with nested loops and totals each row. Watch the inner loop finish all its columns before the outer loop moves to the next row.

DECLARE Marks : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
FOR Row ← 1 TO 3
    RowTotal ← 0
    FOR Column ← 1 TO 4
        RowTotal ← RowTotal + Marks[Row, Column]
    NEXT Column
    OUTPUT "Student ", Row, ": ", RowTotal
NEXT Row
Worked example Row totals
Marks holds the values in Fig. 1 (rows 5 8 6 9 / 7 7 10 4 / 3 9 8 6). State the output of the pseudocode above.
  1. Row 1: 5 + 8 + 6 + 9 = 28.
  2. Row 2: 7 + 7 + 10 + 4 = 28.
  3. Row 3: 3 + 9 + 8 + 6 = 26.
Answer: Student 1: 28, Student 2: 28, Student 3: 26
Your turn
Swap the loops so that the outer loop is FOR Column ← 1 TO 4. What total is output for column 3?
Examiner trap

Keep the index order the same as the declaration. If the array is [1:3, 1:4], then Marks[4, 2] is out of range: there is no row 4.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

How many elements does DECLARE Data : ARRAY[0:15] OF INTEGER create?

Quick check+10 XP

In the lab's 2D array program the table has 2 rows and 3 columns. How many times does the statement inside the inner loop run?

Quick check+10 XP

Using Marks in Fig. 1, what is the value of Marks[3, 2]?

Quick check+10 XP

What is the upper bound of an array?

Section 03

Choosing a structure.

Examiners ask you to select a 1D or 2D array and justify it. Ask: is the data a single list, or does each value belong to a row and a column? And is it all the same data type?

DataChoiceDeclaration
Rainfall for each day of one year1D: one list of 365 valuesARRAY[1:365] OF REAL
Rainfall for each day of each of 52 weeks2D: week (row) and day (column)ARRAY[1:52, 1:7] OF REAL
Which seats in a 10-row, 20-seat cinema are booked2D of BOOLEANARRAY[1:10, 1:20] OF BOOLEAN
Name, age and fee for 30 members1D array of records (mixed types)ARRAY[1:30] OF MemberRecord
Worked example Select and declare
A noughts and crosses game stores 'X', 'O' or a space in each square of a 3 by 3 board. Choose and declare a suitable array, and write a statement that places an O in the middle square.
  1. Each square is found by a row and a column, so a 2D array.
  2. Each square holds one character, so the data type is CHAR.
  3. Declaration: DECLARE Board : ARRAY[1:3, 1:3] OF CHAR.
  4. The middle square is row 2, column 2.
Answer: Board[2, 2] ← 'O'
Examiner trap

Do not mix data types in one array. "A 2D array with a column of names and a column of marks" would need every element to be STRING, so the marks could not be used in arithmetic without conversion.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which declaration is correct for an 8 by 8 board where each square holds one character?

Exam style+20 XP

Values holds 4, 9, 2, 7, 5 in elements 1 to 5. After Total ← 0 and FOR i ← 2 TO 5 STEP 2 / Total ← Total + Values[i] / NEXT i, what is Total?

Exam style+20 XP

Which data is best stored in a 2D array?

Exam style+20 XP

Nums is declared as ARRAY[1:10] OF INTEGER. A loop runs FOR i ← 0 TO 10 and outputs Nums[i]. What happens?

Skill unlocked

1D and 2D arrays, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Array terms, declaring and processing 1D and 2D arrays with loops, tracing array code and choosing a suitable array for a task.