GCSE · Computer Science · AQA · Spec 8525

One-dimensional arrays

You need to store 30 test scores. Are you really going to invent 30 variable names, then type a line that adds all 30 up by hand? There's a much better way.

Watch the loop walk the array

Four scores, one loop

The code is in simple pseudocode. ← means "store this in", LEN(scores) gives the number of values in scores, and FOR … TO counts up to and including its end value.

Before you press Next: scores holds 4 values. How many times will line 4 run, and what is the last value i takes?

1 scores ← [7, 4, 9, 5]
2 total ← 0
3 FOR i ← 0 TO LEN(scores) - 1
4 total ← total + scores[i]
5 ENDFOR
6 OUTPUT total
iscores[i]total
0

Output

 

Step 1: Line 1 builds the whole array in one statement: four values stored in order under one name, scores. LEN(scores) is 4, so the loop on line 3 will run from i = 0 to 4 - 1 = 3. Line 2 sets total to 0, ready for each value to be added.

1 / 6

Press Next one step at a time. Watch i and scores[i] change together: the index picks the slot, the slot gives the value.

Step 1 of 6: Line 1 builds the whole array in one statement: four values stored in order under one name, scores. LEN(scores) is 4, so the loop on line 3 will run from i = 0 to 4 - 1 = 3. Line 2 sets total to 0, ready for each value to be added..

Why bother with an array?

Four separate variablesvsOne array

The same four test scores, stored two different ways. Which side would you rather maintain?

Focus

Adding them all up

Four separate variables

total ← s1 + s2 + s3 + s4, with every name typed out

One array

A loop repeats total ← total + scores[i] as i counts through the indexes

The insight

This is the big one. Separate variables can't be looped through; an array can.

Going from 4 scores to 30

Four separate variables

26 new variables, and every line that uses them rewritten

One array

Put 30 values in scores. A loop that runs to LEN(scores) - 1 handles all 30 without changing a line

Names

Four separate variables

A new identifier for every value: s1, s2, s3, s4

One array

One identifier, scores, for the whole collection; each value is reached as scores[0], scores[1] and so on

Keeping related data together

Four separate variables

Nothing links s1 to s2 except the way you happened to name them

One array

The values are stored together, in order, as one data structure

Data types

Four separate variables

Each variable has its own type, whatever you chose for it

One array

Every element is the same data type: here, all integers

Predict, then check

Commit to an answer before you look. This is the one that catches most people out.

names ← ['Ali', 'Bea', 'Cal'] names[1] ← 'Ben' What does names hold now?

Your turn to fill the gaps

Find the largest value

temps holds five midday temperatures. Complete the program so it outputs the largest one. It must still work if more temperatures are added to temps later.

  1. temps ← [12, 18, 9, 21, 15] largest ← temps[0]Start by treating the first value as the largest so far. The loop will swap it out whenever it meets something bigger.
  2. missing step
Which line is step 2?

Spot the bug

It prints every name, then crashes

This program should output every name in the array. It outputs Ali, Bea and Cal, then stops with an error. Which line is to blame?

A student's program — which line goes wrong?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

One name, a row of numbered slots, and a single loop that can visit every one of them.

What you need to know

  • A data structure is a way of organising and storing related data so a program can use it efficiently.
  • A one-dimensional array is one data structure: an ordered collection of elements, all the same data type, under a single identifier.
  • Each element is identified by its position, called its index.
  • Array name plus index in square brackets picks out one element, such as scores[2], to read it or change it.
  • Arrays are commonly zero-indexed: the first element is at index 0, so n elements have indexes 0 to n - 1.
  • Have a gocolours ← ['red', 'blue', 'gold']. Before reading on: what does colours[1] give you?

    'blue'

    Counting starts at index 0, which is 'red', so index 1 is the second element. Answering 'red' means you counted positions from 1.

  • You can declare an array and give it its values in one statement: names ← ['Ali', 'Bea', 'Cal'].
  • Assigning to an index, like names[1] ← 'Ben', overwrites just that element. Nothing is added, and nothing else moves.
  • Have a goYour mate runs pets ← ['cat', 'dog', 'eel'] then pets[2] ← 'fox', and cheers: "Four pets!" How many elements does pets really hold?

    Three: ['cat', 'dog', 'fox'].

    Assigning to an index overwrites the element already there, so the fox replaces the eel. Nothing joins the end, so the length is still 3.

  • The length is the number of elements an array holds. Accessing any index outside 0 to length - 1 is an error.
  • Have a goAn array holds 5 elements. Which of these indexes causes an error: 0, 4 or 5?

    5

    Five elements have indexes 0 to 4. 5 is tempting because it's the number of elements, but the length is one past the last index.

  • A FOR loop from index 0 to length - 1 processes every element in turn: that's how totalling, finding the largest value and searching work.
  • Swap many separate variables for one array and a loop can process them all, so code stays shorter and copes with any number of values.

The big picture

A one-dimensional array keeps a collection of values, all the same data type, in order, under one name. Each value is an element, and each element sits at a numbered position called its index. Write the array name and the index in square brackets, like scores[2], to read or change one element. Arrays are commonly zero-indexed, so n elements have indexes 0 to n - 1, and any index outside that range is an error. Because one name plus a changing index can reach every element, a single loop can process the whole array, however many values it holds.

Key points

1One name, many values: an array stores values of the same type, in order.
2The index is the position. array[i] is the element at position i.
3First index 0, last index length - 1.
4To visit every element, loop i from 0 to length - 1 and use array[i] inside.
5An index outside 0 to length - 1 is an error.

Worked example

Problem

stock ← [3, 0, 8, 5]. The program then runs stock[2] ← stock[2] - 1, followed by OUTPUT LEN(stock) and then OUTPUT stock[LEN(stock) - 1]. What are the two outputs, and what does stock hold at the end?

⚠ Watch out

Counting positions from 1. It feels natural, but the first element is at index 0, so the last one is at length - 1. A loop that runs up to the length itself asks for one element too many, and that index is outside the array.

🧠

Memory hook

Picture a row of lockers with one name painted over the whole row, numbered from locker 0. The name gets you to the row; the number gets you to one locker. Four lockers means lockers 0, 1, 2 and 3, so there is no locker 4 to open.

✓

Check yourself

data holds 6 values. What are its first and last indexes? A loop runs FOR i ← 1 TO 6 reading data[i]. Which element does it miss, and which index goes wrong?

Flashcards

(14)
What is a data structure?
A way of organising and storing related data in a program so that it can be used efficiently. An array is one example.
What is a one-dimensional array?
An ordered collection of elements, all of the same data type, stored under a single identifier.
What is an element of an array?
One of the values stored in the array.
What is an index?
The position number that identifies one element of an array.
How do you read one element of an array?
Write the array name followed by the index in square brackets, for example scores[2].
A zero-indexed array holds n elements. What are its first and last indexes?
0 and n - 1.
What is the length of an array?
The number of elements it holds.
What happens if a program uses an index outside 0 to length - 1?
It is an error: there is no element at that position.
How do you create an array with its values already in it?
Declare and initialise it in one statement, for example names ← ['Ali', 'Bea', 'Cal'].
How do you change one element of an array?
Assign a new value to it by its index, for example names[1] ← 'Ben'. Only that element changes.
Which loop visits every element of an array exactly once?
A FOR loop with i running from 0 to LEN(array) - 1, using array[i] inside the loop.
Why use an array instead of lots of separate variables?
Related values stay together and one loop can process them all, so the code is shorter and works for any number of values.
Name three tasks done by looping through an array.
Totalling the values, finding the largest value, and searching for a value.
What must be true of the data types of an array's elements?
They are all the same data type, for example all integers or all strings.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 28 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.