Learning Objectives
By the end of this lesson, you will be able to:
- Define what an array is and identify its key characteristics
- Declare and use one-dimensional (1D) arrays in pseudocode
- Declare and use two-dimensional (2D) arrays in pseudocode
- Read from and write to array elements using loops
- Explain the advantages of using arrays over separate variables
- Apply array concepts to solve real-world problems
- Trace through array algorithms and predict their output
Key Terms
Array
A data structure containing several elements of the same data type that can be accessed using the same identifier name.
Element
An individual data item stored in an array.
Index (Subscript)
A value used to identify the position of an element in an array.
Lower Bound
The index of the first element in an array (usually 0 or 1).
Upper Bound
The index of the last element in an array.
One-Dimensional Array (1D)
An array with a single dimension, often called a list.
Two-Dimensional Array (2D)
An array with two dimensions, often called a table with rows and columns.
Identifier
The name used to refer to an array.
Data Type
The type of data stored in an array (e.g., INTEGER, STRING).
Nested Loop
A loop inside another loop, commonly used with 2D arrays.
Pseudocode
A simplified programming language used to describe algorithms.
Declaration
The process of defining an array's name, size, and data type.
Array Fundamentals
An array is a data structure containing several elements of the same data type. These elements can be accessed using the same identifier name. The position of each element in an array is identified using the array's index (subscript).
Key Characteristics of Arrays
- Fixed size: The number of elements is determined when the array is declared
- Same data type: All elements must be of the same type (e.g., all integers or all strings)
- Indexed access: Elements are accessed using an index (subscript)
- Contiguous memory: Elements are stored in consecutive memory locations
- Lower and upper bounds: The index range is defined by lower and upper bounds
Array Indexing
The index of the first element in an array is called the lower bound, and the index of the last element is the upper bound. The lower bound is usually set as zero or one.
Example: 1D Array with 9 elements
Square brackets are used to indicate array indices: mylist[7] ← 16
Array Visualization
Visualize how a 1D array stores data. Each element has an index and a value. Change the values to see how the array updates.
How it works: This 9-element array has indices from 0 to 8 (lower bound = 0, upper bound = 8). Each box shows an element with its index above and value inside.
Real-Life Example: Student Test Scores
Imagine you need to store test scores for a class of 30 students. Instead of creating 30 separate variables (score1, score2, score3...), you can use a single array:
This is much more efficient than managing 30 separate variables, especially when you need to process all scores (e.g., calculate average).
Check Your Understanding: Array Basics
1. What is an array? [2 marks]
Answer
- [1 mark] A data structure containing several elements of the same data type
- [1 mark] These elements can be accessed using the same identifier name
- [Additional] Elements are accessed using an index (subscript) that identifies their position
2. What are the lower bound and upper bound of an array? [2 marks]
Answer
- [1 mark] Lower bound: The index of the first element in an array
- [1 mark] Upper bound: The index of the last element in an array
- [Additional] The lower bound is usually set as zero or one, defining the starting index
3. What is the purpose of an index (subscript) in an array? [2 marks]
Answer
- [1 mark] To identify the position of each element in an array
- [1 mark] To access specific elements using the array name followed by the index in square brackets
- [Additional] Example: mylist[7] accesses the 8th element if lower bound is 0, or 7th if lower bound is 1
4. Why must all elements in an array be of the same data type? [2 marks]
Answer
- [1 mark] To ensure consistent memory allocation for each element
- [1 mark] To allow predictable operations on array elements (e.g., arithmetic operations on integers)
- [Additional] Different data types require different amounts of memory, making array operations complex if mixed
5. How would you declare an array called "prices" that can store 15 decimal numbers? [2 marks]
Answer
- [1 mark] DECLARE prices : ARRAY [1:15] OF REAL
- [1 mark] OR DECLARE prices : ARRAY [0:14] OF REAL (if using zero-based indexing)
- [Additional] The data type REAL is used for decimal numbers in pseudocode
One-Dimensional (1D) Arrays
A one-dimensional (1D) array can be referred to as a list that can have many rows but single column. When a 1D array is declared in pseudocode, the lower bound (LB), upper bound (UB) and data type are included.
Declaration and Initialization
Array Declaration
This declares an array named "mylist" with 9 elements (indices 0 to 8), all of type INTEGER.
Accessing Elements
Declared array can then be used as follows:
This assigns the value 16 to the element at index 7 (8th element if counting from 0).
Working with Arrays and Loops
A FOR...TO...NEXT loop uses a fixed number of repeats so it is ideal to use with an array, when the number of elements is known, as the loop counter can be used as array index.
Example: Reading Values
This prints all 6 elements of the marks array.
Reading and Writing Data
Writing Data to an Array
Example 1: Inputting Values
Data: 25, 34, 98, 7, 41, 19, 5
Example 2: Interactive Input
Resulting Array
After executing Example 1 with the given data, the array would contain:
| Index | MyList |
|---|---|
| [0] | 25 |
| [1] | 34 |
| [2] | 98 |
| [3] | 7 |
| [4] | 41 |
| [5] | 19 |
| [6] | 5 |
The loop counter (Index) starts at the lower bound (0) and goes to the upper bound (6), allowing access to each array element in sequence.
1D Array Operations Simulator
Practice reading from and writing to a 1D array. The pseudocode on the left shows an algorithm, and the visualization on the right shows how the array changes as the algorithm executes.
Pseudocode Algorithm
How it works: This simulation shows how an array changes as an algorithm executes. Click "Next Step" to see each operation applied to the array.
Activity 1: 1D Array Operations
Given the following pseudocode:
- What is the lower bound and upper bound of the array?
- What value is stored in scores[3]?
- What is the final value of 'total' after the loop completes?
- What is the final value of 'average'?
- Write pseudocode to find and print the highest score in the array.
Solution:
-
Lower bound and upper bound:
Lower bound = 1, Upper bound = 5 -
scores[3]:
scores[3] = 78 (index 3 in 1-based indexing is the third element) -
Final value of 'total':
total = 85 + 92 + 78 + 90 + 88 = 433 -
Final value of 'average':
average = 433 / 5 = 86.6 -
Pseudocode to find highest score:
highest ← scores[1]FOR i ← 2 TO 5IF scores[i] > highest THENhighest ← scores[i]ENDIFNEXT iPRINT "Highest score: ", highest
Check Your Understanding: 1D Arrays
1. Why is a FOR loop ideal for working with arrays? [2 marks]
Answer
- [1 mark] A FOR loop uses a fixed number of repeats which matches the fixed size of an array
- [1 mark] The loop counter can be used as the array index to access each element in sequence
- [Additional] Example: FOR i ← 0 TO 5 allows access to array[0], array[1], ..., array[5]
2. What does the declaration "DECLARE names : ARRAY [1:20] OF STRING" mean? [3 marks]
Answer
- [1 mark] Declares an array called "names"
- [1 mark] The array has 20 elements with indices from 1 to 20
- [1 mark] All elements are of data type STRING (text values)
- [Additional] Lower bound = 1, Upper bound = 20, Size = 20 elements
3. Write pseudocode to input 8 temperatures into an array and then calculate their average. [4 marks]
Answer
Key points: Array declaration, loop for input, accumulation of total, calculation of average.
4. What is the output of the following pseudocode? [3 marks]
Answer
- [3 marks] Output: 10, 6, 16, 2, 12 (each on a new line or separated by spaces)
- [Explanation] Each element of the array is doubled: 5×2=10, 3×2=6, 8×2=16, 1×2=2, 6×2=12
5. Explain the difference between "DECLARE arr : ARRAY [0:9] OF INTEGER" and "DECLARE arr : ARRAY [1:10] OF INTEGER". [2 marks]
Answer
- [1 mark] Both arrays have 10 elements, but they use different indexing systems
- [1 mark] The first uses zero-based indexing (indices 0-9), the second uses one-based indexing (indices 1-10)
- [Additional] The choice affects how elements are accessed: arr[0] vs arr[1] for the first element
Two-Dimensional (2D) Arrays
A 2D array can be referred to as a table, with rows and columns. When a 2D array is declared in pseudocode, the lower bound for rows (LBR) and upper bound for rows (UBR), lower bound for columns (LBC) and upper bound for columns (UBC), and data type are included.
Declaration and Structure
2D Array Declaration
This declares a 2D array with 9 rows (0-8) and 3 columns (0-2), for a total of 27 elements.
Accessing Elements
To initialize a single element in a 2D array:
This assigns the value 16 to the element at row 7, column 0.
Example 2D Array Structure
A table with 9 rows and 3 columns (27 elements) and lower bounds of zero:
| Row index | [r,0] | [r,1] | [r,2] |
|---|---|---|---|
| [0,c] | 27 | 31 | 17 |
| [1,c] | 19 | 67 | 48 |
| [2,c] | 36 | 98 | 29 |
| [3,c] | 42 | 22 | 95 |
| [4,c] | 16 | 35 | 61 |
| [5,c] | 89 | 46 | 47 |
| [6,c] | 21 | 71 | 28 |
| [7,c] | 16 | 23 | 13 |
| [8,c] | 55 | 11 | 77 |
Row indices: 0-8 (lower bound row to upper bound row)
Column indices: 0-2 (lower bound column to upper bound column)
Working with 2D Arrays and Nested Loops
Initializing a 2D Array
Write algorithm using pseudocode to set each element of array ThisTable to zero:
This uses nested FOR loops to access every element in the 2D array (5 rows × 3 columns = 15 elements).
Output Contents of a 2D Array
This outputs the array in a table format, with each row on a separate line.
Expected Output Format
2D Array Visualization
Visualize a 2D array as a table with rows and columns. Each cell shows its [row, column] index and value.
How it works: This 5×3 array has row indices 0-4 and column indices 0-2. Each cell shows [row,column] and its value. The top row shows column indices, and the left column shows row indices.
Advantages of Using Arrays Instead of Separate Variables
- Easier to implement algorithms for searching and organizing data. Values may be accessed via a loop-controlled variable used as index of an array.
- Makes algorithm easier to design, amend, code, test and understand. Using a single array with indexing is more systematic than many individual variables.
- Fewer identifiers needed so less storage required. Instead of 40 variable names for 40 students, you use one array name.
For 40 students: Without arrays = 40 variables (Name1, Name2, ..., Name40). With arrays = 1 array (Name[1:40]).
Real-Life Example: School Timetable
A school timetable can be represented as a 2D array:
This makes it easy to find what subject is taught on any given day and period using timetable[day, period].
Activity 2: 2D Array Operations
A firm records the number of completed amplifiers made by 3 workers over 4 days in a 2D array:
| Day\Worker | Worker 1 | Worker 2 | Worker 3 |
|---|---|---|---|
| Day 1 | 10 | 20 | 9 |
| Day 2 | 11 | 16 | 11 |
| Day 3 | 10 | 24 | 13 |
| Day 4 | 14 | 20 | 17 |
The array is declared as:
- What is the value of ProductionData[3, 2]?
- What does ProductionData[2,1] + ProductionData[2,2] + ProductionData[2,3] represent?
- Write pseudocode to calculate the total production for each worker over all 4 days.
- Write pseudocode to calculate the average daily production for each worker and flag workers with average less than 2 amplifiers per day for investigation.
Solution:
-
ProductionData[3, 2]:
Row 3, Column 2 = 24 (Worker 2 on Day 3) -
ProductionData[2,1] + ProductionData[2,2] + ProductionData[2,3]:
Represents the total number of amplifiers produced by all three workers on Day 2.
Calculation: 11 + 16 + 11 = 38 amplifiers -
Pseudocode for total per worker:
DECLARE WorkerTotal : ARRAY [1:3] OF INTEGERFOR WorkerNum ← 1 TO 3WorkerTotal[WorkerNum] ← 0FOR DayNum ← 1 TO 4WorkerTotal[WorkerNum] ← WorkerTotal[WorkerNum] + ProductionData[DayNum, WorkerNum]NEXT DayNumNEXT WorkerNum -
Pseudocode for average and investigation:
FOR WorkerNum ← 1 TO 3WorkerAverage ← WorkerTotal[WorkerNum] / 4IF WorkerAverage < 2 THENOUTPUT "Investigate Worker ", WorkerNumENDIFNEXT WorkerNum
Check Your Understanding: 2D Arrays
1. What is a 2D array and how is it different from a 1D array? [3 marks]
Answer
- [1 mark] A 2D array is a table-like structure with rows and columns
- [1 mark] A 1D array is a list with a single dimension, while a 2D array has two dimensions
- [1 mark] 2D arrays require two indices to access elements: [row, column] vs [index] for 1D arrays
- [Additional] Example: Student marks for multiple subjects (2D) vs marks for one subject (1D)
2. How would you declare a 2D array to store test scores for 5 subjects across 30 students? [2 marks]
Answer
- [1 mark] DECLARE scores : ARRAY [1:30, 1:5] OF INTEGER
- [1 mark] OR DECLARE scores : ARRAY [0:29, 0:4] OF INTEGER (zero-based indexing)
- [Additional] First dimension = students (30), second dimension = subjects (5)
3. Why are nested loops commonly used with 2D arrays? [2 marks]
Answer
- [1 mark] To systematically access every element in the 2D structure
- [1 mark] The outer loop typically controls rows, and the inner loop controls columns
- [Additional] Example: FOR row ← 1 TO 5 (outer), FOR col ← 1 TO 3 (inner) accesses 5×3=15 elements
4. Write pseudocode to find the highest value in a 2D array scores[1:4, 1:3]. [4 marks]
Answer
Key points: Initialize with first element, nested loops to check all elements, update when finding higher value.
5. What are three advantages of using arrays instead of separate variables? [3 marks]
Answer
- [1 mark] Easier to implement algorithms for searching and organizing data
- [1 mark] Makes algorithms easier to design, amend, code, test and understand
- [1 mark] Fewer identifiers needed so less storage required
- [Additional] Values can be accessed via a loop-controlled variable used as array index
Key Takeaways
- An array is a data structure containing several elements of the same data type accessed using a single identifier
- Array elements are accessed using an index (subscript) that identifies their position
- The lower bound is the index of the first element; the upper bound is the index of the last element
- One-dimensional (1D) arrays are lists with a single dimension, declared as ARRAY [LB:UB] OF type
- Two-dimensional (2D) arrays are tables with rows and columns, declared as ARRAY [LBR:UBR, LBC:UBC] OF type
- FOR loops are ideal for array processing because the loop counter can be used as the array index
- Nested loops are used with 2D arrays to systematically access all elements (rows × columns)
- Arrays make algorithms easier to design, implement, and understand compared to using separate variables
- Arrays require fewer identifiers and less storage than multiple individual variables
- Arrays enable efficient implementation of algorithms for searching, sorting, and organizing data
- In pseudocode, arrays are declared using the DECLARE keyword with bounds and data type specified
- Array elements are accessed using square brackets: arrayName[index] for 1D, arrayName[row, column] for 2D
- Real-world applications of arrays include: student marks, timetables, game boards, image pixels, and spreadsheet data
Question Bank
1. Following pseudocode assigns a value to an element of an array: ThisArray[n] ← 42
Complete the table: [3 marks]
Answer
| Question | Answer |
|---|---|
| The number of dimensions of ThisArray | 1 (one-dimensional array) |
| The technical terms for minimum and maximum values that variable n may take | Lower bound, upper bound |
| The technical term for the variable n in the pseudocode expression | Index / Subscript |
2. A program is being written to input names of all students in a class of 40.
(a) Rewrite the pseudocode to perform the task efficiently using an array. [4 marks]
(b) Give one advantage of your solution. [1 mark]
Answer
(a) Efficient pseudocode using array:
(b) One advantage:
- Program code easier to read / modify / debug
- OR: Easier to access individual elements
- OR: Single identifier used instead of 40 separate variables
3. Production data for 3 workers over 4 days is stored in a 2D array ProductionData[1:4, 1:3].
(a) Describe two features of an array. [2 marks]
(b) What is the value of ProductionData[3, 2]? [1 mark]
(c) Describe what ProductionData[2,1] + ProductionData[2,2] + ProductionData[2,3] represents. [2 marks]
Answer
(a) Two features of an array:
- Set of data items have a common name
- Items are referenced using a subscript/index
- All data items are of the same data type
(b) ProductionData[3, 2]:
24 (Worker 2 on Day 3)
(c) ProductionData[2,1] + ProductionData[2,2] + ProductionData[2,3]:
- The total number of amplifiers produced by workers 1, 2 and 3 (all three workers)
- On day 2
4. Trace through the following pseudocode and complete the trace table: [8 marks]
Answer
| WorkerNum | DayNum | WorkerTotal[1] | WorkerTotal[2] | WorkerTotal[3] |
|---|---|---|---|---|
| 1 | 1 | 10 | 0 | 0 |
| 1 | 2 | 21 | 0 | 0 |
| 1 | 3 | 31 | 0 | 0 |
| 1 | 4 | 45 | 0 | 0 |
| 2 | 1 | 45 | 20 | 0 |
| 2 | 2 | 45 | 36 | 0 |
| 2 | 3 | 45 | 60 | 0 |
| 2 | 4 | 45 | 80 | 0 |
| 3 | 1 | 45 | 80 | 9 |
| 3 | 2 | 45 | 80 | 20 |
| 3 | 3 | 45 | 80 | 33 |
| 3 | 4 | 45 | 80 | 50 |
Final WorkerTotal values: Worker 1 = 45, Worker 2 = 80, Worker 3 = 50
5. Write pseudocode to: [6 marks]
(a) Declare a 2D array to store monthly sales figures for 5 products over 12 months
(b) Input sales figures for all products and months
(c) Calculate and output the total annual sales for each product
Answer
6. Explain why the following pseudocode would not work correctly and correct it: [4 marks]
Answer
Problem: The array is declared with indices 1 to 10 (1-based indexing), but the second FOR loop uses indices 0 to 9 (0-based indexing). This causes an "out of bounds" error when trying to access values[0].
Corrected pseudocode:
Alternative correction: Change array declaration to DECLARE values : ARRAY [0:9] OF INTEGER and keep the second loop as is.
7. A 2D array matrix[1:3, 1:3] contains the following values: {{1,2,3},{4,5,6},{7,8,9}}
What is the output of the following pseudocode? [3 marks]
Answer
The pseudocode outputs elements where row index equals column index (diagonal elements).
- When row=1, col=1: matrix[1,1] = 1 → OUTPUT 1
- When row=2, col=2: matrix[2,2] = 5 → OUTPUT 5
- When row=3, col=3: matrix[3,3] = 9 → OUTPUT 9
Output: 1, 5, 9 (each on a new line or separated)
8. Compare and contrast 1D and 2D arrays, giving an example use case for each. [5 marks]
Answer
| Aspect | 1D Array | 2D Array |
|---|---|---|
| Dimensions | One dimension (list) | Two dimensions (table) |
| Declaration | ARRAY [LB:UB] OF type | ARRAY [LBR:UBR, LBC:UBC] OF type |
| Indexing | Single index: array[index] | Two indices: array[row, column] |
| Memory structure | Linear sequence of elements | Grid/Rectangular structure |
| Loop usage | Single FOR loop | Nested FOR loops (row and column) |
| Example use case | Store student marks for one subject | Store student marks for multiple subjects |
| Real-world example | Daily temperature readings for a month | Chess board (8×8 grid) |
Key similarity: Both store multiple elements of the same data type under a single identifier.