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 ← 03 FOR i ← 0 TO LEN(scores) - 14 total ← total + scores[i]5 ENDFOR6 OUTPUT total
| i | scores[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.
Press Next one step at a time. Watch i and scores[i] change together: the index picks the slot, the slot gives the value.
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?
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
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?
What is a one-dimensional array?
What is an element of an array?
What is an index?
How do you read one element of an array?
A zero-indexed array holds n elements. What are its first and last indexes?
What is the length of an array?
What happens if a program uses an index outside 0 to length - 1?
How do you create an array with its values already in it?
How do you change one element of an array?
Which loop visits every element of an array exactly once?
Why use an array instead of lots of separate variables?
Name three tasks done by looping through an array.
What must be true of the data types of an array's elements?
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 postedMore AQA GCSE Computer Science topics
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.