Home Notes Papers

Arrays

Paper 2

This section is examined in Paper 2.

What is an Array?
Definition: An array is a fixed-size data structure that stores a collection of elements of the same data type in contiguous memory locations. Each element is accessed by its unique index (position number).

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

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] or Scores[51] is invalid and causes an Index Out of Bounds error.
Declaring and Using Arrays

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 i changes from 1 to 30, allowing us to access each unique slot Temperatures[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
  • CityData has 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.
⚠︎ Conceptual and Syntax Errors

Mistake 1: Assuming Arrays are Dynamic

  • Error: Thinking you can add elements to an array after it is declared (e.g., Array[51] = NewValue when 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 49 for an array declared [1:50].
  • Correct Understanding: Ensure the loop range matches the array declaration. If declared [1:50], the loop must be FOR i <- 1 TO 50.
Handling Array Limits and Validation
When to use this tip: When asked to write an algorithm that adds data to an array or ensures the array size is not exceeded.

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:

  1. Declare a variable, e.g., ItemCount, initialized to 0.
  2. Before inputting data, check: IF ItemCount < MaximumSize THEN.
  3. If true, proceed with input and increment ItemCount.
  4. 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.

When to use this tip: When asked to search or sort data within an array.

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:

  1. Use a FOR loop from 1 to ArraySize.
  2. Inside the loop, use an IF statement to compare the current element Array[i] with the target value.
  3. 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.

Past Paper Style Questions
Q:
Declare an array called Temperatures that can store 30 real numbers. Write pseudocode to input 30 temperatures into this array.
A:
  1. Declaration:
    Declare Temperatures[1:30] OF REAL

  2. Input Loop:
    FOR i <- 1 TO 30
    OUTPUT "Enter temperature for day " & i
    INPUT Temperatures[i]
    ENDFOR

Q:
Explain why a 2D array is more appropriate than a 1D array for storing data about cities (Name, Population, Area).
A:
A 2D array is more appropriate because the data is tabular (rows and columns). Each city has multiple related attributes (Name, Population, Area) that need to be stored together. A 1D array would require separate arrays for each attribute, making it harder to keep related data linked. The 2D structure allows access via CityData[Row, Column], where rows represent cities and columns represent attributes.
Q:
Write pseudocode to find the highest value in a 1D array Scores[1:50].
A:
Declare HighestScore REAL
HighestScore <- Scores[1] // Initialize with first element
FOR i <- 2 TO 50
IF Scores[i] > HighestScore THEN
HighestScore <- Scores[i]
ENDIF
ENDFOR
OUTPUT HighestScore
Beta v0.7.8 Free while we're in beta — it transitions to paid post launch. Thank you for supporting us at this stage!