TopMyGrade

GCSE/Computer Science/OCR

2.2.5Arrays: 1D and 2D arrays; iterating, reading and writing values

Notes

Arrays: 1D and 2D

An array is a data structure that stores multiple values of the same data type under a single variable name. Each value is accessed using an index (position number). OCR J277 Paper 2 tests array creation, access, iteration and 2D arrays — often alongside FOR loops.

1D arrays (one-dimensional)

A 1D array is like a list: a single row of elements.

Declaration and initialisation (OCR pseudocode)

// Declare an array of 5 integers
scores = [10, 25, 8, 42, 17]

Indexing

OCR arrays are zero-indexed: the first element is at index 0.

scores[0]   // 10  (first element)
scores[1]   // 25  (second element)
scores[4]   // 17  (last element — index = length - 1)

Reading and writing

// Read a value
OUTPUT scores[2]        // outputs 8

// Write a value (update element at index 3)
scores[3] = 99
// scores is now [10, 25, 8, 99, 17]

Iterating through an array with a FOR loop

total = 0
FOR i = 0 TO 4
    total = total + scores[i]
END FOR
OUTPUT total            // outputs 159

Or using LEN() to get the length automatically:

FOR i = 0 TO LEN(scores) - 1
    OUTPUT scores[i]
END FOR

Finding the largest value in an array

largest = scores[0]
FOR i = 1 TO LEN(scores) - 1
    IF scores[i] > largest THEN
        largest = scores[i]
    END IF
END FOR
OUTPUT largest

2D arrays (two-dimensional)

A 2D array is a grid (table) of values — rows and columns. Useful for grids, matrices, game boards, spreadsheets.

Declaration (OCR pseudocode)

// 3 rows, 4 columns
grid = [[1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12]]

Indexing: grid[row][column]

grid[0][0]   // 1   (row 0, column 0)
grid[1][2]   // 7   (row 1, column 2)
grid[2][3]   // 12  (row 2, column 3)

Iterating through a 2D array (nested FOR loops)

FOR row = 0 TO 2
    FOR col = 0 TO 3
        OUTPUT grid[row][col]
    END FOR
END FOR

Real-world use: seating plan

// seats[row][column] stores student name or "" if empty
seats = [["Alice", "Bob", ""],
         ["", "Charlie", "Daisy"],
         ["Eve", "", "Frank"]]

OUTPUT seats[1][1]   // Charlie

Why use arrays?

  • Store many values of the same type without needing separate variable names (no need for score1, score2, score3...).
  • Easy to iterate over using loops.
  • Random access by index — O(1) — very fast.

Common OCR exam mistakes

  1. Using index 1 for the first element — OCR arrays are zero-indexed (first = index 0).
  2. Forgetting LEN(array) - 1 as the last index — iterating to LEN(array) goes one step too far.
  3. Using the wrong order for 2D arrays — always grid[row][column], not grid[column][row].
  4. Trying to store different data types in one array — arrays hold one data type only.

AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Practice questions

Try each before peeking at the worked solution.

  1. Question 15 marks

    Accessing array elements

    An array called names is declared as:

    names = ["Alice", "Bob", "Charlie", "Daisy", "Eve"]
    

    (a) State the value of names[2]. [1]
    (b) Write a statement to change the value at index 0 to "Amelia". [1]
    (c) Write pseudocode to output every element of the array using a FOR loop. [3]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  2. Question 24 marks

    Finding the sum of an array

    Write OCR pseudocode to find and output the total of all values in an integer array called values that has 6 elements (index 0 to 5). [4 marks]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

  3. Question 34 marks

    2D array access

    A 2D array called grid is defined as:

    grid = [[3, 7, 1],
            [9, 4, 6],
            [2, 8, 5]]
    

    (a) State the value of grid[1][2]. [1]
    (b) Write a statement to change the value at row 2, column 0 to 11. [1]
    (c) Describe what nested FOR loops would be needed to output every element of grid. [2]

    Ask AI about this

    AI-generated · claude-opus-4-7 · v3-ocr-computer-science

Flashcards

2.2.5 — Arrays: 1D and 2D arrays; iterating, reading and writing values

8-card SR deck for OCR Computer Science (J277) topic 2.2.5

8 cards · spaced repetition (SM-2)