8.2 Arrays

← Topic 8.1 Programming conceptsComputer Science contentsTopic 8.3 File handling →
Chapter 8 · Programming

8.2 Arrays

An array is a data structure that stores several elements of the same data type under one identifier. Each element is accessed by an index. In this topic you need to understand one-dimensional and two-dimensional arrays and how iteration is used to declare, populate and access them.

One-dimensional arraysTwo-dimensional arraysIndexesIterationNested loops

8.2.1 One- and Two-dimensional arrays

Arrays allow many related data items to be stored in a uniform way. Every item uses the same array identifier, while the index identifies the position of a particular element. This makes it possible to access items individually, search through a list and place values into a useful order.

Array ideaMeaning
IdentifierThe common name used for all elements in the array.
ElementOne individual data item stored in the array.
IndexThe position used to access a particular element.
Data typeAll elements in an array have the same data type.

The first index may be zero or one. The textbook notes that most programming languages automatically use zero as the first index. For Cambridge IGCSE Computer Science you need to work with both one-dimensional and two-dimensional arrays.

Exam idea: the index is the position, not the value stored at that position. If MyList[1] contains 19, then 1 is the index and 19 is the element value.
Check the core array ideas.

8.2.2 Declaring and populating arrays with iteration

One-dimensional arrays

A one-dimensional array can be treated as a list. The textbook example MyList contains ten integer elements with indexes from 0 to 9.

Figure 8.10 from the textbook showing a one-dimensional array MyList with indexes 0 to 9 and the first and last elements

When declaring a one-dimensional array in pseudocode, include the array name, first index, last index and data type:

DECLARE MyList : ARRAY[0:9] OF INTEGER

An individual position can be assigned directly. In the source example:

MyList[4] ← 27

The entire array can instead be populated using a loop. The loop counter is used as the index:

OUTPUT "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72"
FOR Counter ← 0 TO 9
  OUTPUT "Enter next value "
  INPUT MyList[Counter]
NEXT Counter

To display one element, refer to its index. For the array shown in Figure 8.10:

OUTPUT MyList[1]

This outputs 19.

Populating a one-dimensional array when it is declared

LanguageExample
PythonmyList = [27, 19, 36, 42, 16, 89, 21, 16, 55, 72]
Visual BasicDim myList = New Integer() {27, 19, 36, 42, 16, 89, 21, 16, 55, 72}
Javaint[] myList = {27, 19, 36, 42, 16, 89, 21, 16, 55, 72};

Two-dimensional arrays

A two-dimensional array can be treated as a table of rows and columns. The source example MyTable has ten rows and three columns, giving thirty elements. Its first element is at position 0,0.

Figure 8.11 from the textbook showing the two-dimensional array MyTable with rows and columns

When declaring a two-dimensional array in pseudocode, include the first and last row indexes, the first and last column indexes, and the data type:

DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER

Populating a two-dimensional array requires nested loops: one loop changes one index while the other changes the second index.

OUTPUT "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34"
OUTPUT "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76"
OUTPUT "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21"
FOR ColumnCounter ← 0 TO 2
  FOR RowCounter ← 0 TO 9
    OUTPUT "Enter next value "
    INPUT MyTable[RowCounter, ColumnCounter]
  NEXT RowCounter
NEXT ColumnCounter

For the table shown, the element at row index 2 and column index 1 is 98.

Textbook notation note: the source line for this access is printed as OUTPUT MyList[2,1], even though the two-dimensional array on the page is named MyTable. The intended access to the shown table is therefore MyTable[2,1], which gives 98.

Python and arrays

The textbook notes an important difference: Python normally uses lists rather than arrays. A Python list can contain different data types, while an array is defined here as containing elements of the same type. A two-dimensional structure in Python is created by placing lists inside another list.

Populating a two-dimensional array when it is declared

LanguageExample form
PythonMyTable = [[27, 31, 17], [19, 67, 48], ...]
Visual BasicDim MyTable = New Integer(8, 2) {{27, 31, 17}, ...}
Javaint[][] MyTable = {{27, 31, 17}, {19, 67, 48}, ...};

Regardless of the programming language, the key idea is the same: a two-dimensional structure stores values by two positions, and nested iteration is a natural way to process every element.

Practice declarations, indexes and iteration.

Topic 8.2 revision checklist

Define an array, element and index.
Explain why all array elements have the same data type.
Distinguish one-dimensional and two-dimensional arrays.
Recognise that indexes commonly begin at zero.
Declare a one-dimensional array with its index range and data type.
Access or assign an individual array element using an index.
Use a loop counter as an array index to populate a one-dimensional array.
Declare a two-dimensional array using row and column index ranges.
Use nested loops to populate or process a two-dimensional array.
Explain the textbook's note about Python lists and arrays.
Ready for a mixed Topic 8.2 check?
← Topic 8.1 Programming conceptsComputer Science contentsTopic 8.3 File handling →