# Arrays

> Computer Science · CIE A-Level 9618
> Source: https://www.owlsprep.com/study/cie-9618-u10-arrays/

This module covers 1-dimensional and 2-dimensional arrays, core static data structures for storing homogeneous collections of data. You will learn declaration, indexing, common operations, and how to avoid exam traps.

**Prerequisites:** [Primitive data types](https://www.owlsprep.com/study/cie-9618-u10-primitive-data-types/); [Basic memory concepts](https://www.owlsprep.com/study/cie-9618-u09-memory-allocation/)

## Learning objectives

- Explain the core structure and memory allocation of arrays
- Declare and access elements in 1D and 2D arrays correctly
- Implement common array operations for exam problems
- Avoid common off-by-one and indexing errors

## Core Concepts & 1D Arrays

**One-Dimensional (1D) Array** — A linear collection of same-type elements stored in contiguous memory. Each element is accessed via a single index offset from the array's starting address.

*Notation:* DECLARE name[size] : DATA_TYPE

*Example:* An array of 10 integers to store student test scores

Unlike individual variables, arrays group related data together, making it easy to process all elements with a loop. They are the most widely used data structure in introductory programming, and appear in almost every Paper 2 programming question.

**Worked example:** Declare a 1D array of 5 floating-point numbers called `temperatures`, initialize the first element to 22.5, and output the third element (zero-indexed).

1. 1. Declare the array with correct type, name and size:
2. $$\text{DECLARE } temperatures[5] : \text{REAL}$$
3. 2. Initialize the first element at index 0:
4. $$temperatures[0] = 22.5$$
5. 3. The third element is at index 2, so output it:
6. $$\text{OUTPUT } temperatures[2]$$

> **Exam tip:** Always confirm the indexing convention stated in the question before writing your code.

## Two-Dimensional (2D) Arrays

**Two-Dimensional (2D) Array** — An array of arrays, structured as a grid with rows and columns. Each element is accessed using two indices: the first for the row, the second for the column.

*Notation:* DECLARE name[rows][columns] : DATA_TYPE

*Example:* A 3x3 grid for a tic-tac-toe game board

2D arrays are commonly used to represent grids, matrices, tables, and game boards in exam questions. Paper 1 may also ask you to calculate the memory address of a 2D element for row-major or column-major order storage.

**Worked example:** Declare a 4-row, 6-column 2D integer array called `gameGrid`, and set the element in the 2nd row, 5th column (zero-indexed) to 10.

1. 1. Declare the array with rows first, then columns:
2. $$\text{DECLARE } gameGrid[4][6] : \text{INTEGER}$$
3. 2. 2nd row = index 1, 5th column = index 4, so assign the value:
4. $$gameGrid[1][4] = 10$$

**Check your understanding**

Test your understanding:

1. What index accesses the first row, first column of a zero-indexed 2D array?

   - [0][0]
   - [1][1]
   - [0][1]
   - [1][0]

   *Why:* Correct! Zero-indexing starts counting from 0, so the first element in each dimension gets index 0.

## Common Examined Array Operations

The most common array operations tested in exams are traversal (iterating over all elements), finding maximum/minimum values, calculating sum/average, linear search, insertion, and deletion. All of these use loops to access elements by index.

**Worked example:** Write pseudocode to find the maximum value in a zero-indexed 1D array `scores` of 10 integers.

1. 1. Initialize the maximum value to the first element of the array:
2. $$maxScore = scores[0]$$
3. 2. Traverse the array starting from the second element (index 1):
4. $$\text{FOR } i = 1 \text{ TO } 9$$
5. $$\quad \text{IF } scores[i] > maxScore \text{ THEN}$$
6. $$\quad \quad maxScore = scores[i]$$
7. $$\quad \text{END IF}$$
8. $$\text{END FOR}$$
9. 3. After the loop completes, `maxScore` holds the maximum value of the array.

> **warning**
>
> For a zero-indexed array of size n, valid indices run only from 0 to n-1. Accessing any index outside this range causes an out-of-bounds error.

## Common pitfalls

- **Wrong:** Off-by-one errors when looping: looping from 1 to 10 for a 10-element zero-indexed array.
  - Why it fails: The last valid index of a 10-element zero-indexed array is 9, so accessing index 10 is out of bounds.
  - Correct: Loop from 0 to n-1 for zero-indexed arrays of size n, matching the convention given in the question.
- **Wrong:** Swapping row and column indices when declaring or accessing 2D arrays.
  - Why it fails: CIE pseudocode uses [rows][columns] convention by default, so swapping leads to incorrect size declarations and wrong element access.
  - Correct: Always write the row index/size first, followed by the column index/size.
- **Wrong:** Assuming arrays are dynamic and can grow beyond their declared size.
  - Why it fails: Static arrays (the standard array taught in 9618) are fixed-size at declaration. They cannot be resized during program execution.
  - Correct: Declare an array large enough to hold all possible input values at the start of your program.
- **Wrong:** Assuming all arrays are zero-indexed without checking the question.
  - Why it fails: Some exam questions explicitly use one-indexing for simplicity, so using zero-indexing leads to wrong results.
  - Correct: Always read the question statement to confirm the indexing convention before writing any code.

## Cheatsheet

| Concept | Key Fact | Pseudocode Example |
| --- | --- | --- |
| 1D Array Declaration | Fixed size, same type | DECLARE scores[10] : INTEGER |
| 2D Array Declaration | Rows first, then columns | DECLARE grid[3][3] : INTEGER |
| Zero-index 1D | Indices 0 to n-1 for size n | FOR i ← 0 TO 9 |
| One-index 1D | Indices 1 to n for size n | FOR i ← 1 TO 10 |
| 2D Element Access | Row index first, column second | grid[row][col] |
| Find Maximum Value | Start with first element, iterate | max = arr[0]   IF arr[i] > max THEN max = arr[i] |

## What's next

Arrays are the foundation for almost all more complex data structures you will learn in A-Level Computer Science. Understanding how arrays store data in contiguous memory is critical for understanding how linked lists, stacks, queues, and hash tables work, as all of these often use arrays as an underlying storage layer. Mastering array operations like traversal and search also builds the core algorithmic thinking you need for solving complex programming problems in Paper 2. Next, you will learn to combine different data types using records, before moving on to more advanced algorithms and dynamic data structures.

- [Records](https://www.owlsprep.com/study/cie-9618-u10-records/)
- [Linked lists](https://www.owlsprep.com/study/cie-9618-u10-linked-lists/)
- [Stacks](https://www.owlsprep.com/study/cie-9618-u10-stacks/)

---

From [OwlsPrep](https://www.owlsprep.com) — free study guides for A-Level, IB, AP and IGCSE, written against the official syllabus. Canonical page: https://www.owlsprep.com/study/cie-9618-u10-arrays/
