Arrays (OCR A-Level Computer Science): Revision Notes
Arrays
Overview
An array is a data structure that stores a collection of elements, typically of the same data type, in a contiguous block of memory. Arrays are used to efficiently store and access multiple values using a single variable name and an index. You need to know arrays up to 3 dimensions, as they are commonly used for organising data.
What is an Array?
- Definition: An array is a collection of elements, all of the same data type, identified by an index or set of indices.
- Indexing:
- Arrays are zero-indexed in many programming languages (e.g., Python, Java, C++), meaning the first element is accessed with index 0.
- Example: In an array arr = [10, 20, 30], arr[0] refers to 10.
- Fixed Size: Arrays are typically of a fixed size, meaning the number of elements is determined when the array is created.
Types of Arrays
One-Dimensional Array
A single row of elements.
Example: A list of student grades.
grades = [85, 90, 78, 92]
Two-Dimensional Array
Consists of rows and columns, like a table or grid.
Example: A matrix representing a chessboard.
chessboard = [
[1, 0, 0, 1], # Row 1
[0, 1, 1, 0], # Row 2
[1, 1, 0, 0], # Row 3
[0, 0, 1, 1] # Row 4
]
Three-Dimensional Array
Represents data in three dimensions, such as multiple layers of 2D grids.
Example: A 3D array for storing temperature values over time for different locations.
temperature = [
[[15, 16], [20, 21]], # Day 1: Locations 1, 2
[[18, 19], [22, 23]] # Day 2: Locations 1, 2
]
Uses of Arrays
- Storing Data: Arrays are used to store collections of related data, such as lists of names, scores, or positions in a game.
- Organising Complex Data: Multi-dimensional arrays help organise more complex data, such as images (2D arrays) or 3D models (3D arrays).
- Efficiency: Arrays allow for fast access to elements using their index.
Recognising When to Use Arrays
- When you have a collection of similar data: For example, storing a list of test scores.
- When you need to perform operations on a group of values: For example, summing all elements.
- When data needs to be structured in rows and columns: For example, storing a table of student marks or pixel data for images.
- When managing multiple datasets: For example, storing daily temperatures across multiple locations.
Examples
Example 1: One-Dimensional Array Store and print the names of 3 students.
students = ["Alice", "Bob", "Charlie"]
print(students[1]) # Output: Bob
Example 2: Two-Dimensional Array Store and access a table of marks for 3 students in 2 subjects.
marks = [
[85, 90], # Student 1: Subject 1, Subject 2
[78, 88], # Student 2: Subject 1, Subject 2
[92, 95] # Student 3: Subject 1, Subject 2
]
print(marks[2][1]) # Output: 95 (Student 3, Subject 2)
Example 3: Three-Dimensional Array Store and access daily temperature data for 2 cities over 2 days.
temperature = [
[[15, 16], [18, 19]], # Day 1: City 1, City 2
[[20, 21], [22, 23]] # Day 2: City 1, City 2
]
print(temperature[1][0][1]) # Output: 21 (Day 2, City 1, second time point)
Note Summary
Common Mistakes
- Index Out of Bounds: Trying to access an index that doesn't exist in the array (e.g., arr[5] when arr only has 3 elements).
- Incorrect Dimensional Indexing: Forgetting the correct number of indices for multi-dimensional arrays.
Example: Using arr[1] instead of arr[1][2] for a 2D array.
- Confusion Between Arrays and Lists: Some languages like Python use flexible lists, but in many languages (like Java), arrays are fixed in size.
:::
Key Takeaways
- Arrays are data structures that store multiple elements of the same type.
- They can be one-dimensional (like a list), two-dimensional (like a grid), or three-dimensional (like multiple layers of grids).
- Arrays are widely used for storing and efficiently accessing structured data.
- A proper understanding of indexing and dimensions is essential for effectively using arrays in programmes.