Arrays (Copy)
IGCSE / O Level Computer Science Cheat Sheet
Topic: 8.2 Arrays
📚 What is an Array?
- An array is a collection of elements (values) stored under one name, where each element is accessed by an index.
🔢 One-Dimensional (1D) Arrays
- Stores a list of items in a single row or column
- Index starts at 0 or 1 (depends on the language)
Declaration Example (Pseudocode):
DECLARE marks[5] AS INTEGER
Accessing Elements:
marks[0] ← 90 // Store value
OUTPUT marks[0] // Retrieve value
🧮 Using Loops with 1D Arrays
FOR i ← 0 TO 4
INPUT marks[i]
NEXT i
sum ← 0
FOR i ← 0 TO 4
sum ← sum + marks[i]
NEXT i
🧩 Two-Dimensional (2D) Arrays
- Stores data in rows and columns
- Often visualized as a grid or table
Declaration Example:
DECLARE grid[3][4] AS INTEGER
Accessing Elements:
grid[0][2] ← 5
OUTPUT grid[0][2]
🔁 Nested Iteration for 2D Arrays
FOR row ← 0 TO 2
FOR col ← 0 TO 3
INPUT grid[row][col]
NEXT col
NEXT row
🎯 When to Use Arrays
- When handling lists of similar data (e.g. scores, names)
- When looping through items
- For storing tabular (2D) data
⚙️ Key Operations
| Operation | Description |
|---|---|
| Input | Store data using index |
| Output | Retrieve data from a specific index |
| Traversal | Loop through all elements |
| Totalling | Add all values |
| Counting | Count values meeting a condition |
📌 Important Notes
- Array indexes are fixed-size
- Cannot store mixed data types (e.g. integers and strings in same array)
- Index out of range errors occur if you access an invalid position
