Algorithms and programming · GCSE Computer Science

Arrays and lists

GCSE Computer Science arrays and lists: indexing from 0 or 1, bounds, iteration through items, and the out-of-range errors that crash a dry-run.

UNDERSTANDRETRIEVEREMEMBER
THE MEMORY HOOK
An array is many values under one name. The first item is usually index 0. The last valid index is length − 1. Going past that is a bounds error.

The important bits

What you need to know

  1. 1

    An array (or list) stores several items of the same type under one identifier. You pick an item with an index, not by inventing a new variable for every score.

  2. 2

    Many exam languages index from 0: the first element is names[0]. Some board pseudocode indexes from 1. Read the paper. Mixing the two is an off-by-one factory.

  3. 3

    The last valid index in a 0-based array of n items is n−1. A loop FOR i ← 0 TO n or names[n] walks off the end. That is a bounds (out of range) error.

  4. 4

    You iterate with a counted loop on the index, or FOR EACH item IN list when the spec allows it. Use an index when you must know the position or swap two slots.

  5. 5

    Two-dimensional arrays are tables: scores[row, column] or scores[row][column]. Nested loops walk rows then columns. State which index is the row.

  6. 6

    Arrays have a fixed length in some languages; lists can grow with append. At GCSE, still check that an index exists before you write to it.

  7. 7

    Passing an array into a subroutine lets you process any length if you also pass the length (or use the language’s LEN). Hard-coding 5 when the array has 6 items misses the last mark.

  8. 8

    Bounds checks belong in your mental model: empty array (do not enter the loop), one-item array (only index 0), and the last item (the usual miss).

Quotations worth analysing

Short evidence. Real method.

Array indexes start at 0 in this language.
Typical paper convention (Python-style)

If the question says this, names[1] is the second item. Treating it as the first is an off-by-one that then poisons every later index.

A bounds error occurs when the index is outside the valid range.
Runtime / logic error mark scheme

Valid range is 0 to length−1 (0-based) or 1 to length (1-based). length itself is not a valid 0-based index.

LEN(array) returns the number of items, not the last index.
String / array handling phrase

The last index is LEN−1 when indexing from 0. A loop to LEN inclusive is the classic crash or missed mark.

Go deeper

Zero is a place, not an empty slot

Students treat index 0 as “nothing there” because 0 looks like a blank. In a 0-based array it is the first real item. A list of four test marks lives at 0, 1, 2 and 3. The length is 4. A total loop must include 0 and 3. FOR i ← 1 TO 3 skips the first mark and undercounts; FOR i ← 0 TO 4 hits a fifth slot that does not exist. That second version is a bounds error: the program may halt at runtime, or in a written trace you write “error” when the index is invalid. Boundary tests in a test plan are exactly these indexes: first, last, one past last, and empty. Write the expected output before you run. The last item is where most logic errors hide because the condition was < n written as <= n or the other way round.

Go deeper

Two dimensions are just nested indexes

A seating plan with 3 rows and 4 seats is not twelve separate variables. It is grid[0..2][0..3] if 0-based. Outer loop rows, inner loop seats, and you visit every cell. Swap the loops and you still visit every cell, but the order of output changes — papers sometimes ask for that order. An off-by-one on either bound misses a whole row or a whole column. When you search a 2D array, linear search becomes nested: scan row 0, then row 1, and so on. Do not invent binary search on a grid unless both the structure and the order are specified. In answers, name the indexes: “row then column”. Writing “the array position” without numbers is how a 3-mark trace becomes a paragraph.

WORKED EXAMPLE

See the idea in action

0-based array marks = [7, 9, 4, 10]. n = 4. Last valid index = 3. Total the list: total ← 0 FOR i ← 0 TO n − 1 total ← total + marks[i] ENDFOR i = 0: total = 7 i = 1: total = 16 i = 2: total = 20 i = 3: total = 30 Loop ends. Output 30. If the header is FOR i ← 0 TO n, the next step is i = 4, marks[4] — bounds error, and you never get a reliable total. If the header is FOR i ← 1 TO n − 1 you skip 7 and output 23. That is a logic error: the program runs, the answer is wrong, and the last item was not the problem — the first item was.

Exam technique

Turn knowledge into marks

Write “first index” and “last valid index” in the margin before you trace. Match 0-based or 1-based to the paper. LEN is the count; the last 0-based index is LEN − 1.

Common mistakes

Do not give these marks away

  1. 01

    Off-by-one loops that miss the last array item or overrun it with index = length.

  2. 02

    Assuming every paper indexes from 0, or from 1, without reading the question’s language.

  3. 03

    Using a separate variable for each item instead of an array when the question asked for an array.

QUICK RETRIEVAL

A 0-based array has 5 items. Which index is the last valid position?

A5

B4

C0

D6

Show the answer

4. Indexes are 0, 1, 2, 3, 4. Length is 5; last valid index is length − 1. Index 5 is out of bounds. Index 0 is the first item, not the last.

Quick questions

If this is the bit you searched

Do arrays start at 0 or 1 GCSE Computer Science?

It depends on the language in the paper. Python-style arrays start at 0. Some exam pseudocode starts at 1. Read the question and keep that convention for the whole answer.

What is a bounds error?

Using an index outside the valid range, such as names[5] in a 0-based array of 5 items (valid indexes 0–4). It is a runtime error in many languages.

How do you loop through every item in an array?

Use a FOR loop from the first index to the last valid index, or FOR EACH if allowed. Add a running total or compare with a target inside the loop.

What is a two-dimensional array?

An array of arrays: a table with a row index and a column index. Nested loops visit each cell. State which index is the row and which is the column.