Chapter 8.2: Arrays - Organising Your Data

Hello IGCSE Computer Scientists! Arrays might sound complicated, but they are one of the most useful tools you will learn in programming.

Think of an array as a super-organised filing cabinet or a set of labelled lockers. Instead of having hundreds of separate variables (like Student1, Student2, Student3...), you can group all related data under one name. This makes handling large amounts of information much faster and simpler.

In this chapter, we will learn how to declare these structured storage containers (arrays) and, crucially, how to fill them up and read them using loops (iteration).


1. Understanding the Basic Concept of an Array

What is an Array?

An array is a data structure used in programming that stores a fixed number of items of the same data type.

  • Fixed Length: Once you create an array, you define its size, and that size cannot usually be changed while the program is running.
  • Homogeneous Data: All the items (called elements) stored inside the array must be the same data type (e.g., all INTEGERs, or all STRINGs).

Key Terminology: Index and Element

To get information out of an array (or put information in), you need to know where it is located. We use an Index (also known as a subscript).

Analogy: Imagine a block of 10 student lockers. The array is the block of lockers, and the index is the number written on each locker door (1, 2, 3, ... 10). The item inside the locker is the element.

  • Element: The actual piece of data stored in one slot of the array.
  • Index: The position number used to access a specific element.

Important Note on Indexing (Syllabus 8.2): In Computer Science, indexing can start at 0 (Zero-based) or 1 (One-based). For IGCSE Pseudocode, we generally assume 1-based indexing unless specified otherwise, but be aware that some programming languages start counting positions from 0. The declaration syntax allows you to explicitly set the start (lower bound) and end (upper bound) index.

Quick Review: Array Properties

Array: All items are the Alpha (same) type.
Remember: The size is Rigid (fixed length).
Read/write data using the Row number (index).


2. One-Dimensional (1D) Arrays (8.2.1)

A one-dimensional array is the simplest type. It stores data in a single line or list.

Step 1: Declaring a 1D Array

When you declare a 1D array, you must state three things: the array name, the index range (lower bound:upper bound), and the data type of the elements.

Pseudocode Syntax:

DECLARE <Identifier> : ARRAY[<Lower Bound>:<Upper Bound>] OF <data type>

Example: Declaring an array called TestScores to hold 50 INTEGER values. The index runs from 1 to 50.

DECLARE TestScores : ARRAY[1:50] OF INTEGER

Step 2: Accessing and Assigning Values (Using Variables as Indexes)

To read or write a specific element, you use the array name followed by the index number inside square brackets [ ].

To make your code flexible, you should always use a variable (like i or Index) as the index inside the loop structure (8.2.2).

Example of accessing:

OUTPUT TestScores[5] // Displays the score stored in the 5th position

Example of assigning:

TestScores[12] ← 95 // Sets the score in the 12th position to 95

Step 3: Reading and Writing Using Iteration (Loops) (8.2.3)

Since arrays store many elements consecutively, we almost always use a loop (iteration) to process them efficiently. A count-controlled loop (FOR loop) is perfect for this, as we know the exact number of times (the size of the array) we need to repeat the process.

Example: Filling a 1D Array

This pseudocode fills the TestScores array (size 50) by asking the user for input 50 times.

DECLARE TestScores : ARRAY[1:50] OF INTEGER
DECLARE Counter : INTEGER

FOR Counter ← 1 TO 50
OUTPUT "Enter score number ", Counter
INPUT TestScores[Counter] // Counter is used as the index variable
NEXT Counter

Key Takeaway (1D Arrays)

1D arrays are lists. We use a single variable as an index, and typically a single FOR loop to manage data entry or retrieval for the entire list.


3. Two-Dimensional (2D) Arrays (8.2.1)

If a 1D array is like a list, a two-dimensional array is like a table or a spreadsheet. It has two dimensions: Rows and Columns.

Analogy: Imagine a class seating plan. The data (student name) is located by its Row number and its Column number.

Step 1: Declaring a 2D Array

When declaring a 2D array, you must specify the index range for both dimensions: the rows and the columns.

Pseudocode Syntax:

DECLARE <Identifier> : ARRAY[<L1>:<U1>, <L2>:<U2>] OF <data type>

  • L1:U1 refers to the index range of the Rows (the first dimension).
  • L2:U2 refers to the index range of the Columns (the second dimension).

Example: Declaring an array called Board to represent an 8x8 chessboard (Characters).

DECLARE Board : ARRAY[1:8, 1:8] OF CHAR

Step 2: Accessing Elements in a 2D Array

To access an element, you need two index values separated by a comma within the square brackets. Conventionally, we use [Row, Column].

Example of accessing:

OUTPUT Board[2, 5] // Displays the character stored in Row 2, Column 5

Example of assigning:

Board[8, 1] ← 'R' // Sets the element at Row 8, Column 1 to 'R' (Rook)

Step 3: Processing 2D Arrays using Nested Iteration (8.2.3)

Since a 2D array is a grid, reading or writing data for the *entire* array requires Nested Loops (a loop inside another loop).

  • The Outer Loop typically controls the Rows.
  • The Inner Loop controls the Columns within that specific row.
Example: Initializing a 2D Board Array

This pseudocode initializes the entire 8x8 Board array to hold a blank space (' ').

DECLARE Board : ARRAY[1:8, 1:8] OF CHAR
DECLARE RowIndex : INTEGER
DECLARE ColIndex : INTEGER

FOR RowIndex ← 1 TO 8
FOR ColIndex ← 1 TO 8
Board[RowIndex, ColIndex] ← ' '
NEXT ColIndex
NEXT RowIndex

Step-by-Step Explanation of Nested Loop:

  1. The RowIndex starts at 1.
  2. The inner loop, ColIndex, runs completely (1, 2, 3, 4, 5, 6, 7, 8). It sets Board[1, 1] through to Board[1, 8].
  3. The inner loop finishes.
  4. The outer loop moves to the NEXT RowIndex (now 2).
  5. The inner loop runs completely again (1 through 8). It sets Board[2, 1] through to Board[2, 8].
  6. This repeats until RowIndex reaches 8.
Key Takeaway (2D Arrays)

2D arrays are tables. We use two index variables (one for row, one for column), and we need nested loops (usually two FOR loops) to process every single element.


4. Common Mistakes and Memory Aids

Watch out for Data Type Mismatches!

If you declare an array to be OF INTEGER, you cannot try to store a STRING or a BOOLEAN value in it.

Mistake Example:
DECLARE Ages : ARRAY[1:10] OF INTEGER
Ages[3] ← "Twenty-One" // ERROR: Trying to store a STRING in an INTEGER array.

The Off-By-One Error (A Programmer's Worst Nightmare)

This happens when your loop runs one time too many or one time too few for the size of your array.

Example: If your array is declared ARRAY[1:10], it has 10 slots (1 to 10). If you write a loop FOR i ← 0 TO 9, you will access the wrong slots (and potentially crash the program if index 0 is invalid).

Tip: Always match your loop limits exactly to your array bounds.
DECLARE Names : ARRAY[1:10] requires FOR i ← 1 TO 10.

Did You Know? Arrays and RAM

When a computer allocates memory for an array, it sets aside a single, continuous block of memory big enough for *all* the elements. This is why arrays are so fast to access; the computer knows exactly where every element is located based on the starting address and the index number.

Summary Checklist for Arrays

  • Declaration: Define the name, the bounds (start and end index), and the data type.
  • Access: Use square brackets [ ] with an index (or indices for 2D).
  • Processing: Use loops (iteration). Use a single FOR loop for 1D arrays, and nested FOR loops for 2D arrays.
  • Flexibility: Always use a variable (like a counter or loop index) to access the elements inside a loop.