What you'll learn
Arrays form a fundamental data structure in programming, allowing you to store multiple values of the same data type under a single identifier. This revision guide covers everything you need to know about one-dimensional and two-dimensional arrays for the CIE IGCSE Computer Science examination, including declaration syntax, accessing elements through indexing, and manipulating array contents through common operations.
Key terms and definitions
Array — a data structure that stores multiple values of the same data type under a single identifier, with each element accessible by its index position.
Index — the numeric position of an element within an array, typically starting from 0 in most programming languages (zero-based indexing).
One-dimensional array (1D array) — a linear array structure that stores elements in a single row or column, accessed using one index value.
Two-dimensional array (2D array) — a table-like array structure organised in rows and columns, accessed using two index values (row index and column index).
Declaration — the process of creating an array by specifying its name, data type, and size (number of elements it can store).
Initialisation — the process of assigning initial values to array elements, either at declaration or afterwards.
Traversal — the process of systematically accessing each element in an array, typically using iteration structures like FOR loops.
Bounds checking — verifying that an index value falls within the valid range of the array to prevent runtime errors.
Core concepts
Understanding arrays and their purpose
Arrays solve a critical programming problem: storing related data efficiently. Rather than declaring separate variables (score1, score2, score3), an array allows you to store all scores under one name (scores[0], scores[1], scores[2]).
Key characteristics of arrays:
- All elements must be the same data type (integer, real, string, boolean, or char)
- Fixed size once declared (static allocation in most IGCSE contexts)
- Elements stored in contiguous memory locations
- Direct access to any element using its index
- Zero-based indexing is standard (first element at index 0)
Arrays are ideal for:
- Storing lists of related data (student names, test scores, temperatures)
- Processing multiple values with loops
- Implementing algorithms like searching and sorting
- Organising tabular data (2D arrays)
Declaring and initialising one-dimensional arrays
Declaration establishes the array's name, data type, and capacity. In CIE IGCSE pseudocode:
DECLARE StudentNames : ARRAY[0:29] OF STRING
DECLARE Temperatures : ARRAY[0:6] OF REAL
DECLARE Scores : ARRAY[1:10] OF INTEGER
The syntax ARRAY[lower:upper] specifies the index range. ARRAY[0:29] creates 30 elements (indices 0 through 29). You can use any starting index, though 0 or 1 is conventional.
Initialisation assigns values to elements:
// Individual assignment
Scores[1] ← 85
Scores[2] ← 92
Scores[3] ← 78
// Assignment in a loop
FOR Index ← 0 TO 6
Temperatures[Index] ← 0.0
ENDFOR
// Reading values from user
FOR Counter ← 0 TO 29
OUTPUT "Enter student name: "
INPUT StudentNames[Counter]
ENDFOR
Some languages allow declaration with simultaneous initialisation:
DECLARE DaysInMonth : ARRAY[0:11] OF INTEGER
DaysInMonth ← [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Accessing and manipulating 1D array elements
Direct access uses the index within square brackets:
FirstScore ← Scores[0]
OUTPUT Temperatures[3]
Scores[5] ← Scores[5] + 10
Traversal processes all elements systematically:
// Calculate sum of all scores
Total ← 0
FOR Index ← 0 TO 9
Total ← Total + Scores[Index]
ENDFOR
Linear search finds an element:
// Search for a specific student name
SearchName ← "Ahmed"
Found ← FALSE
Position ← 0
WHILE Position <= 29 AND Found = FALSE
IF StudentNames[Position] = SearchName THEN
Found ← TRUE
ELSE
Position ← Position + 1
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Student found at position ", Position
ELSE
OUTPUT "Student not found"
ENDIF
Finding maximum/minimum values:
// Find highest temperature
MaxTemp ← Temperatures[0]
FOR Index ← 1 TO 6
IF Temperatures[Index] > MaxTemp THEN
MaxTemp ← Temperatures[Index]
ENDIF
ENDFOR
OUTPUT "Highest temperature: ", MaxTemp
Bubble sort (common algorithm for arrays):
// Sort Scores array in ascending order
FOR Pass ← 0 TO 8
FOR Index ← 0 TO 9 - Pass
IF Scores[Index] > Scores[Index + 1] THEN
Temp ← Scores[Index]
Scores[Index] ← Scores[Index + 1]
Scores[Index + 1] ← Temp
ENDIF
ENDFOR
ENDFOR
Declaring and initialising two-dimensional arrays
A 2D array represents data in rows and columns, like a spreadsheet or table. The first index typically represents the row, the second represents the column.
Declaration syntax:
DECLARE SalesData : ARRAY[0:3, 0:11] OF REAL
DECLARE ChessBoard : ARRAY[1:8, 1:8] OF STRING
DECLARE ClassMarks : ARRAY[0:29, 0:4] OF INTEGER
ARRAY[0:3, 0:11] creates a structure with 4 rows (0-3) and 12 columns (0-11), totalling 48 elements.
Visualising a 2D array:
// ClassMarks: 30 students × 5 subjects
// Subject 0 Subject 1 Subject 2 Subject 3 Subject 4
// Student 0 78 82 90 75 88
// Student 1 85 79 88 92 81
// Student 2 90 95 87 89 93
// ...and so on
Initialisation:
// Individual assignment
SalesData[0, 0] ← 1250.50
SalesData[0, 1] ← 1890.75
// Nested loop initialisation
FOR Row ← 0 TO 3
FOR Column ← 0 TO 11
SalesData[Row, Column] ← 0.0
ENDFOR
ENDFOR
// Reading data from user
FOR StudentNum ← 0 TO 29
FOR SubjectNum ← 0 TO 4
OUTPUT "Enter mark for student ", StudentNum, " subject ", SubjectNum
INPUT ClassMarks[StudentNum, SubjectNum]
ENDFOR
ENDFOR
Accessing and manipulating 2D array elements
Direct access requires both indices:
FirstStudentMaths ← ClassMarks[0, 2]
OUTPUT "Sales for quarter 2, month 5: ", SalesData[1, 4]
ClassMarks[15, 3] ← 85
Nested traversal uses two loops:
// Display all sales data
FOR Row ← 0 TO 3
FOR Column ← 0 TO 11
OUTPUT SalesData[Row, Column], " "
ENDFOR
OUTPUT "" // New line after each row
ENDFOR
Processing by row (e.g., calculating each student's average):
FOR StudentNum ← 0 TO 29
Total ← 0
FOR SubjectNum ← 0 TO 4
Total ← Total + ClassMarks[StudentNum, SubjectNum]
ENDFOR
Average ← Total / 5
OUTPUT "Student ", StudentNum, " average: ", Average
ENDFOR
Processing by column (e.g., finding highest mark per subject):
FOR SubjectNum ← 0 TO 4
MaxMark ← ClassMarks[0, SubjectNum]
FOR StudentNum ← 1 TO 29
IF ClassMarks[StudentNum, SubjectNum] > MaxMark THEN
MaxMark ← ClassMarks[StudentNum, SubjectNum]
ENDIF
ENDFOR
OUTPUT "Subject ", SubjectNum, " highest mark: ", MaxMark
ENDFOR
Searching in 2D arrays:
// Find if any student scored 100 in any subject
Found ← FALSE
StudentIndex ← 0
WHILE StudentIndex <= 29 AND Found = FALSE
SubjectIndex ← 0
WHILE SubjectIndex <= 4 AND Found = FALSE
IF ClassMarks[StudentIndex, SubjectIndex] = 100 THEN
Found ← TRUE
FoundStudent ← StudentIndex
FoundSubject ← SubjectIndex
ELSE
SubjectIndex ← SubjectIndex + 1
ENDIF
ENDWHILE
IF Found = FALSE THEN
StudentIndex ← StudentIndex + 1
ENDIF
ENDWHILE
Common array operations and algorithms
Counting occurrences:
// Count how many temperatures were below freezing
Count ← 0
FOR Index ← 0 TO 6
IF Temperatures[Index] < 0 THEN
Count ← Count + 1
ENDIF
ENDFOR
OUTPUT "Days below freezing: ", Count
Validation with arrays:
// Check if all scores are in valid range (0-100)
AllValid ← TRUE
Index ← 0
WHILE Index <= 9 AND AllValid = TRUE
IF Scores[Index] < 0 OR Scores[Index] > 100 THEN
AllValid ← FALSE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
Copying arrays:
DECLARE OriginalScores : ARRAY[0:9] OF INTEGER
DECLARE BackupScores : ARRAY[0:9] OF INTEGER
FOR Index ← 0 TO 9
BackupScores[Index] ← OriginalScores[Index]
ENDFOR
Parallel arrays (storing related data in separate arrays):
DECLARE StudentID : ARRAY[0:29] OF INTEGER
DECLARE StudentName : ARRAY[0:29] OF STRING
DECLARE StudentGrade : ARRAY[0:29] OF CHAR
// Same index links related data
// StudentID[0], StudentName[0], StudentGrade[0] all refer to same student
Worked examples
Example 1: Processing rainfall data (1D array)
Question: A weather station records daily rainfall in millimetres for a week. Write pseudocode to:
- Declare an appropriate array
- Input 7 rainfall values
- Calculate the total weekly rainfall
- Find the day with maximum rainfall
Solution:
// Declare array to store 7 days of rainfall data
DECLARE Rainfall : ARRAY[0:6] OF REAL
// Input rainfall values
FOR Day ← 0 TO 6
OUTPUT "Enter rainfall for day ", Day + 1, " (mm): "
INPUT Rainfall[Day]
ENDFOR
// Calculate total rainfall
TotalRainfall ← 0
FOR Day ← 0 TO 6
TotalRainfall ← TotalRainfall + Rainfall[Day]
ENDFOR
OUTPUT "Total weekly rainfall: ", TotalRainfall, " mm"
// Find day with maximum rainfall
MaxRainfall ← Rainfall[0]
MaxDay ← 0
FOR Day ← 1 TO 6
IF Rainfall[Day] > MaxRainfall THEN
MaxRainfall ← Rainfall[Day]
MaxDay ← Day
ENDIF
ENDFOR
OUTPUT "Highest rainfall on day ", MaxDay + 1, ": ", MaxRainfall, " mm"
Mark scheme notes:
- Correct declaration with appropriate data type (REAL) [1 mark]
- Input loop accessing all elements [1 mark]
- Accumulator for total [1 mark]
- Algorithm to find maximum with position tracking [2 marks]
Example 2: Student test analysis (2D array)
Question: A class of 20 students takes 3 tests. Write pseudocode to:
- Declare a 2D array to store test marks
- Calculate each student's average mark
- Find the class average for Test 2
Solution:
// Declare 2D array: 20 students × 3 tests
DECLARE TestMarks : ARRAY[0:19, 0:2] OF INTEGER
// Assume marks already stored in array
// Calculate each student's average
FOR StudentNum ← 0 TO 19
Total ← 0
FOR TestNum ← 0 TO 2
Total ← Total + TestMarks[StudentNum, TestNum]
ENDFOR
StudentAverage ← Total / 3
OUTPUT "Student ", StudentNum + 1, " average: ", StudentAverage
ENDFOR
// Calculate class average for Test 2 (index 1)
TestTotal ← 0
FOR StudentNum ← 0 TO 19
TestTotal ← TestTotal + TestMarks[StudentNum, 1]
ENDFOR
ClassAverage ← TestTotal / 20
OUTPUT "Class average for Test 2: ", ClassAverage
Mark scheme notes:
- 2D array declaration with correct dimensions [1 mark]
- Nested loop for row processing (student averages) [2 marks]
- Column processing for specific test [2 marks]
- Correct calculation of averages [1 mark]
Example 3: Linear search with position return
Question: Write pseudocode that searches an array of 50 product codes for a specific code entered by the user. Output the position if found, or a suitable message if not found.
Solution:
DECLARE ProductCodes : ARRAY[0:49] OF STRING
// Assume array already populated with codes
OUTPUT "Enter product code to search: "
INPUT SearchCode
Found ← FALSE
Position ← 0
WHILE Position <= 49 AND Found = FALSE
IF ProductCodes[Position] = SearchCode THEN
Found ← TRUE
ELSE
Position ← Position + 1
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Product code found at position ", Position
ELSE
OUTPUT "Product code not found"
ENDIF
Mark scheme notes:
- Correct loop structure with bounds checking [1 mark]
- Boolean flag to control loop [1 mark]
- Correct comparison and position tracking [1 mark]
- Appropriate output for both outcomes [1 mark]
Common mistakes and how to avoid them
Off-by-one errors: Remember array declared as
ARRAY[0:9]has 10 elements, not 9. Always check your loop boundaries match the declared range. If declared[0:9], loop should beFOR Index ← 0 TO 9.Incorrect index order in 2D arrays: Convention is
Array[Row, Column], notArray[Column, Row]. Be consistent throughout your code. Think "RC" (rows then columns) like in mathematics.Forgetting bounds checking: Accessing
Scores[10]when array declared asARRAY[0:9]causes a runtime error. Always ensure index values fall within declared range.Wrong data type declaration: Declaring an array as INTEGER when storing decimal values (marks with decimal places) loses precision. Choose REAL for decimal values, STRING for text, INTEGER for whole numbers only.
Mixing up assignment and comparison: Use
←for assignment (Scores[3] ← 85) and=for comparison (IF Scores[3] = 85). Getting these backwards creates logic errors.Inefficient 2D array traversal: When processing by column, ensure your outer loop is the column loop and inner loop is the row loop. Match your loop structure to whether you're processing rows or columns.
Exam technique for "Programming concepts: arrays (1D and 2D) — declaration, access and manipulation"
Command word "Write pseudocode" (4-8 marks): Provide complete, syntactically correct pseudocode including declaration, appropriate loop structures, and clear variable names. Use CIE pseudocode conventions exactly as taught. Show your logic clearly with proper indentation.
Command word "Describe" (2-3 marks): Give a clear explanation in prose, not code. Include the purpose, the process, and any key details. For example, "Describe how a linear search works" requires explaining the sequential checking process, the use of a flag, and what happens when found or not found.
For declaration questions: Always specify the data type (INTEGER, REAL, STRING, BOOLEAN, CHAR), the array size with correct syntax
ARRAY[lower:upper], and use meaningful identifiers. One mark often awarded just for correct declaration syntax.Marks typically awarded for: Correct declaration (1 mark), appropriate loop structure (1-2 marks), correct index usage (1 mark), algorithm logic (2-3 marks), suitable output (1 mark). Show all steps clearly to maximise partial credit if you make an error.
Quick revision summary
Arrays store multiple values of the same data type under one identifier. 1D arrays use one index Array[Index], declared as ARRAY[0:n] OF datatype. 2D arrays use two indices Array[Row, Column], declared as ARRAY[0:m, 0:n] OF datatype. Access elements directly using indices; traverse arrays using FOR loops. Common operations include searching, finding maximum/minimum, sorting, and calculating totals/averages. Always check index bounds to prevent errors. Use nested loops for 2D arrays, with structure matching whether processing by row or column.