Arrays
Why use arrays?
Imagine you need to store the test scores for 50 students. Without arrays, you would need 50 separate variables: Score1, Score2, ..., Score50. This is inefficient and impossible to manage programmatically.
| Feature | Individual Variables | Array |
|---|---|---|
| Declaration | Declare Score1, Score2... (50 lines) |
Declare Scores[1:50] OF INTEGER (1 line) |
| Access | Score1, Score2 (hardcoded names) |
Scores[i] (using a loop variable i) |
| Iteration | Impossible to loop through easily | Easy with FOR loops |
| Scalability | Poor; adding more requires new code | High; change size in declaration only |
Key Constraint: Arrays have a fixed size. Once declared (e.g., Array[1:50]), you cannot add a 51st element. This is a critical limitation compared to dynamic lists in some other programming languages.
One-Dimensional (1D) Arrays
Used for simple lists (e.g., a list of names, temperatures).
- Structure: A single row or column.
- Indexing:
ArrayName[Index]
Two-Dimensional (2D) Arrays
Used for tabular data (e.g., a grid of cities and their populations, a matrix).
- Structure: Rows and columns.
- Indexing:
ArrayName[Row, Column] - Note: You must specify both dimensions. Forgetting the second dimension is a common error.
Index: The position number of an element within the array.
- In Cambridge pseudocode, arrays are typically 1-based (indices start at 1), unless specified otherwise.
- Example: In
Declare Scores[1:50], valid indices are 1, 2, ..., 50. - Accessing
Scores[0]orScores[51]is invalid and causes an Index Out of Bounds error.
1. Declaring a 1D Array
Declare Temperatures[1:30] OF REAL
This creates an array named Temperatures with 30 slots, indexed from 1 to 30.
2. Writing Values (Input)
To store data, use a loop and the counter as the index:
FOR i <- 1 TO 30
OUTPUT "Enter temperature for day " & i
INPUT Temperatures[i]
ENDFOR
- Why this works: The variable
ichanges from 1 to 30, allowing us to access each unique slotTemperatures[1],Temperatures[2], etc.
3. Reading Values (Output)
To process all data, iterate through the array again:
FOR i <- 1 TO 30
OUTPUT Temperatures[i]
ENDFOR
4. Declaring a 2D Array
Declare CityData[1:50, 1:2] OF STRING
CityDatahas 50 rows (cities) and 2 columns.- Column 1 might store the City Name.
- Column 2 might store the Population.
- Accessing a city:
CityData[1, 1]is the name of the first city;CityData[1, 2]is its population.
Mistake 1: Assuming Arrays are Dynamic
- Error: Thinking you can add elements to an array after it is declared (e.g.,
Array[51] = NewValuewhen size is 50). - Correct Understanding: Arrays have a fixed upper bound. You must check if the current count of items is less than the maximum size before adding new data. If the array is full, you cannot add more.
Mistake 2: Ignoring the Second Dimension in 2D Arrays
- Error: Using
CityData[1]for a 2D array. - Correct Understanding: A 2D array requires two indices. You must specify both row and column, e.g.,
CityData[1, 1]. Forgetting the second index is a frequent conceptual error in examiner reports.
Mistake 3: Off-by-One Errors in Loops
- Error: Looping
FOR i <- 0 TO 49for an array declared[1:50]. - Correct Understanding: Ensure the loop range matches the array declaration. If declared
[1:50], the loop must beFOR i <- 1 TO 50.
Examiner Acceptance Criteria: Examiners look for a check against the maximum size before writing to the array. You must explicitly state that you are comparing a counter (number of items entered) with the array's upper bound.
Correct Phrasing/Logic:
- Declare a variable, e.g.,
ItemCount, initialized to 0. - Before inputting data, check:
IF ItemCount < MaximumSize THEN. - If true, proceed with input and increment
ItemCount. - If false, output "Array is full" or stop the process.
Why this is accepted: It demonstrates an understanding that arrays are fixed-size structures and prevents logical errors where a program tries to write to non-existent memory locations.
Examiner Acceptance Criteria: Examiners require the use of iteration (loops) to access elements. You cannot just say "find the value." You must show how you traverse the array.
Correct Phrasing/Logic:
- Use a
FORloop from1toArraySize. - Inside the loop, use an
IFstatement to compare the current elementArray[i]with the target value. - Example:
IF Array[i] = TargetValue THEN ...
Why this is accepted: It shows you understand that arrays are accessed sequentially via indices and that searching requires checking each position until a match is found.
Temperatures that can store 30 real numbers. Write pseudocode to input 30 temperatures into this array.Declaration:
Declare Temperatures[1:30] OF REALInput Loop:
FOR i <- 1 TO 30OUTPUT "Enter temperature for day " & iINPUT Temperatures[i]ENDFOR
CityData[Row, Column], where rows represent cities and columns represent attributes.Scores[1:50].Declare HighestScore REALHighestScore <- Scores[1] // Initialize with first elementFOR i <- 2 TO 50IF Scores[i] > HighestScore THENHighestScore <- Scores[i]ENDIFENDFOROUTPUT HighestScore