A-Level Computer Science / Unit 10: Organising Data in Programs

10.2.1 One-Dimensional Arrays: Indices and Bounds

πŸ”’ Lesson slides are available to signed-in users. Sign in

10.2.1 One-Dimensional Arrays: Indices and Bounds

A one-dimensional array stores an ordered sequence of values under one identifier. Each value occupies a valid indexed position, so a program can select, update or process the elements systematically.

By the end of this section, you should be able to:

  • Explain why a one-dimensional array is suitable for a list of related values.
  • Use the terms array, element, index, lower bound and upper bound accurately.
  • Calculate the number of elements from an array's bounds.
  • Write pseudocode to declare a 1D array with an appropriate element type.
  • Read, assign, input and output individual array elements.
  • Use a loop to process every element in a 1D array.
  • Recognise when a problem needs one index rather than a row-and-column structure.

Why Use a One-Dimensional Array?

Consider an e-bike station with six charging docks numbered 12 to 17. The program stores one battery percentage for each dock. The values belong to the same list, use the same data type and are identified naturally by their dock number.

Creating six unrelated variables such as Charge12, Charge13 and Charge14 would make repeated processing awkward. An array keeps the values under one identifier and lets a loop visit each dock in turn.

One-dimensional array: an ordered sequence of elements accessed using one index.
Approach Example Consequence
Separate variables Charge12, Charge13, Charge14... Each value needs a different identifier, making loops difficult to use.
One 1D array DockCharge[12], DockCharge[13]... One identifier is combined with changing index values.
Exam tip: A strong justification mentions an ordered list of related values, one shared element type and the ability to process the values using an index.

The Anatomy of a 1D Array

The array below stores one whole-number battery percentage for each charging dock. The index row shows the valid positions; the value row shows the data currently stored in those positions.

Index 12 13 14 15 16 17
DockCharge 86 41 73 95 58 67
Array identifier: the name used for the complete array, such as DockCharge.
Element: one value stored at a particular index, such as the value held in DockCharge[14].
Index: the value written in square brackets to select an element.
Common mistake: DockCharge[14] is the third element in this array, not the fourteenth element. The lower bound is 12, so ordinal position and index value are not the same.

Lower Bound, Upper Bound and Element Count

The bounds define the complete valid index range. Both endpoints are included.

Term Meaning In ARRAY[12:17]
Lower bound The smallest valid index 12
Upper bound The largest valid index 17
Valid indexes Every integer from the lower bound to the upper bound 12, 13, 14, 15, 16, 17
Number of elements: upper bound - lower bound + 1
17 - 12 + 1 = 6 elements

The extra 1 is needed because both 12 and 17 are included in the valid range.

Index 11 and index 18 are outside the declared range. Accessing an invalid index can cause an execution error in a real program.

Declaring a 1D Array

A declaration states the array identifier, lower bound, upper bound and element data type.

General form:
DECLARE <identifier> : ARRAY[<lowerBound>:<upperBound>] OF <dataType>

Worked example

DECLARE DockCharge : ARRAY[12:17] OF INTEGER

This creates six indexed positions. Every element is intended to hold an INTEGER.

Declaration Valid indexes Element type Element count
DECLARE WindSpeed : ARRAY[3:8] OF REAL 3 to 8 REAL 6
DECLARE RouteCode : ARRAY[20:24] OF STRING 20 to 24 STRING 5
DECLARE KeyPressed : ARRAY[0:7] OF CHAR 0 to 7 CHAR 8
Choose the element type from the values being stored. The bounds describe valid positions; they do not determine the element data type.

Reading and Changing Individual Elements

Square brackets select one element. The index may be a literal value, a variable or an expression, provided that its result is within the declared bounds.

Assign a value

DockCharge[12] ← 86
DockCharge[16] ← 58

Read a value

OUTPUT DockCharge[14]

Save input in an element

INPUT DockCharge[17]

Update an existing value

DockCharge[16] ← DockCharge[16] + 5
Expression What it refers to
DockCharge The complete array
DockCharge[14] One element selected by index 14
DockCharge[Dock] The element selected by the current value of variable Dock

Processing Every Element with a Loop

A loop counter can take each valid index value in order. This is one of the main reasons arrays are more manageable than many separately named variables.

Input one value for every dock

FOR Dock ← 12 TO 17
    INPUT DockCharge[Dock]
NEXT Dock

Output every stored value

FOR Dock ← 12 TO 17
    OUTPUT DockCharge[Dock]
NEXT Dock

Calculate a total

TotalCharge ← 0

FOR Dock ← 12 TO 17
    TotalCharge ← TotalCharge + DockCharge[Dock]
NEXT Dock

OUTPUT TotalCharge
During each iteration, Dock contains one valid index. The expression DockCharge[Dock] therefore refers to a different element each time.
Common mistake: The loop limits must match the array bounds. A loop from 1 to 6 would not access this array correctly because its valid indexes are 12 to 17.

When Is a 1D Array Suitable?

Choose a 1D array when the data form one sequence and each element can be identified using one position.

Problem Suitable structure Reason
Store the noise level recorded at eight checkpoints 1D array One ordered value is stored for each checkpoint.
Store one answer for each question in a quiz 1D array Each answer is selected with one question index.
Store temperatures for several rooms across several days Likely 2D array The data require a room position and a day position.
Section 10.2.2 develops two-dimensional arrays. For this page, focus on structures that need exactly one index. Linear search and bubble sort are covered separately in Sections 10.2.3 and 10.2.4.

Interactive: Array Index Visualiser

Use the existing widget to compare arrays with different lower and upper bounds. Select a declaration, then click an element to see the relationship between the index and the stored value.

Declaration DECLARE Names : ARRAY[1:4] OF STRING
Lower bound 1
Upper bound 4
Number of elements 4

Selected element

Names[1]

The element at index 1 stores "Ava".

Common Mistakes and Misconceptions

  • Assuming that the first valid index must be 0 or 1. Read the declared lower bound.
  • Forgetting that both bounds are included when calculating the number of elements.
  • Confusing an index value with the ordinal position of an element.
  • Using an index below the lower bound or above the upper bound.
  • Confusing the whole array with one selected element.
  • Using loop limits that do not match the valid index range.
  • Assigning a value that does not match the declared element type.
  • Using a 1D array for data that naturally require both a row and a column.

Practice

Task 1: Read the declaration

DECLARE AirQuality : ARRAY[5:11] OF REAL

  1. State the lower bound.
  2. State the upper bound.
  3. Calculate the number of elements.
  4. Write the expression that selects the element at index 8.
  5. State whether index 4 is valid and explain your answer.

Task 2: Write pseudocode

  1. Declare ParcelCode to store nine STRING values using indexes 30 to 38.
  2. Assign "PX-441" to the element at index 34.
  3. Output the element at index 37.
  4. Write a loop that inputs all nine parcel codes.
  5. Write a loop that outputs every parcel code.

Task 3: Explain the error

An array is declared as ARRAY[20:24] OF INTEGER. A student writes a loop from 1 to 5 to process it.

  1. Explain why the loop uses invalid indexes.
  2. Write corrected loop limits.
  3. Explain why the array still contains five elements.

Review

Question Strong answer should include
What is a 1D array? An ordered sequence of same-type elements accessed through one index.
What is the lower bound? The smallest valid index.
What is the upper bound? The largest valid index.
How is the element count calculated? upper bound - lower bound + 1
How is one element selected? Use the array identifier and one index in square brackets.
Why are loops useful with arrays? The loop counter can take every valid index and process each element in turn.
Final exam tip: Use precise vocabulary and show the complete declaration. When tracing an array, write the actual index values rather than assuming that the first element has index 0 or 1.