Arrays and Records
1. One-Dimensional (1D) Arrays
A 1D array is a linear list of elements. Each element is identified by its Index.
DECLARE StudentNames : ARRAY[1:5] OF STRING
// Storing data (Assignment)
StudentNames[1] ← "Alice"
StudentNames[2] ← "John"
// Accessing data in a loop
FOR i ← 1 TO 5
OUTPUT StudentNames[i]
NEXT i
// Storing data (Assignment)
StudentNames[1] ← "Alice"
StudentNames[2] ← "John"
// Accessing data in a loop
FOR i ← 1 TO 5
OUTPUT StudentNames[i]
NEXT i
2. Two-Dimensional (2D) Arrays
A 2D array is a grid structure, effectively a "table" with rows and columns. It requires two indices to locate an element: Array[Row, Column].
| Row/Col | [1] | [2] | [3] |
|---|---|---|---|
| [1] | (1,1) | (1,2) | (1,3) |
| [2] | (2,1) | (2,2) | (2,3) |
DECLARE GameBoard : ARRAY[1:3, 1:3] OF CHAR
// Assigning a value to Row 2, Column 1
GameBoard[2, 1] ← 'X'
// Using nested loops to process a 2D array
FOR Row ← 1 TO 3
FOR Col ← 1 TO 3
GameBoard[Row, Col] ← '.'
NEXT Col
NEXT Row
// Assigning a value to Row 2, Column 1
GameBoard[2, 1] ← 'X'
// Using nested loops to process a 2D array
FOR Row ← 1 TO 3
FOR Col ← 1 TO 3
GameBoard[Row, Col] ← '.'
NEXT Col
NEXT Row
3. Records (Composite Data Types)
A Record is a user-defined type that groups related variables together. This is a Composite Type because it is built from other types (Integer, String, etc.).
// 1. Define the Template (TYPE)
TYPE TStudent
DECLARE Name : STRING
DECLARE ID : INTEGER
DECLARE IsActive : BOOLEAN
ENDTYPE
// 2. Create an instance of the Record
DECLARE NewStudent : TStudent
// 3. Use "Dot Notation" to access fields
NewStudent.Name ← "Nanjala"
NewStudent.ID ← 4055
NewStudent.IsActive ← TRUE
TYPE TStudent
DECLARE Name : STRING
DECLARE ID : INTEGER
DECLARE IsActive : BOOLEAN
ENDTYPE
// 2. Create an instance of the Record
DECLARE NewStudent : TStudent
// 3. Use "Dot Notation" to access fields
NewStudent.Name ← "Nanjala"
NewStudent.ID ← 4055
NewStudent.IsActive ← TRUE
4. Array of Records
The most powerful way to use these structures is to store multiple records inside an array. This mimics a database table structure.
DECLARE ClassList : ARRAY[1:30] OF TStudent
// Accessing a field within an array index
ClassList[1].Name ← "Musa"
ClassList[1].ID ← 1001
OUTPUT ClassList[1].Name
// Accessing a field within an array index
ClassList[1].Name ← "Musa"
ClassList[1].ID ← 1001
OUTPUT ClassList[1].Name
⚠️ Exam Note: Memory & Speed
Arrays provide Random Access, meaning you can jump directly to any element using its index (very fast). However, arrays are Static structures in 9618—you must declare their size upfront, and it cannot change during execution.