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.
| 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. |
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 |
DockCharge.
DockCharge[14].
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 |
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.
Declaring a 1D Array
A declaration states the array identifier, lower bound, upper bound and element data type.
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 |
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
Dock contains one valid index. The expression
DockCharge[Dock] therefore refers to a different element each time.
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. |
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.
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
- State the lower bound.
- State the upper bound.
- Calculate the number of elements.
- Write the expression that selects the element at index 8.
- State whether index 4 is valid and explain your answer.
Task 2: Write pseudocode
- Declare
ParcelCodeto store nine STRING values using indexes 30 to 38. - Assign
"PX-441"to the element at index 34. - Output the element at index 37.
- Write a loop that inputs all nine parcel codes.
- 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.
- Explain why the loop uses invalid indexes.
- Write corrected loop limits.
- 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. |