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, and then write a line that adds up all 30 of them by hand?

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 creates 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 to have each value added to it.

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 creates 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 to have each value added to it..

Why bother with an array?

Four separate variablesvsOne array

The same four test scores, stored two different ways.

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 question 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 replace it whenever it meets something bigger.
  2. missing step
Which line is step 2?

Spot the bug

It prints every name, then stops with an error

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

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 in a program so it can be used efficiently. A one-dimensional array is one data structure.
  • An array is an ordered collection of elements, all of the same data type, stored under a single identifier.
  • Each element is identified by its position, its index. Write the array name and then the index in square brackets to read or change it: scores[2].
  • Arrays are commonly zero-indexed: the first element is at index 0, so an array of n elements has indexes 0 to n - 1.
  • An array can be declared and given its values in one statement, such as names ← ['Ali', 'Bea', 'Cal']. Assigning to an index, such as names[1] ← 'Ben', changes only that element.
  • A loop from index 0 to the length minus 1 processes every element in turn. That is how tasks such as totalling, finding the largest value and searching are done.
  • The length of an array is the number of elements it holds. Trying to access an index outside 0 to length - 1 is an error.
  • Using an array instead of many separate variables keeps related values together and lets one loop process them, so code is shorter and works for any number of values.

The big picture

A one-dimensional array stores a collection of values, all of 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. You reach an element by writing the array name followed by the index in square brackets, like scores[2]. Arrays are usually zero-indexed, so an array of n elements has indexes 0 to n - 1, and using an 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. 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 30 September 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.