Welcome to Data Structures!
Ever tried to find a specific LEGO brick in a giant, messy pile? It’s hard, right? But if you had those bricks organized by color or size in different bins, you’d find what you need in seconds.
In Computer Science, data structures are those "bins." They are specific ways of organizing and storing data in a computer so that we can find it and use it efficiently. In this chapter, we are going to look at the two main ways the AQA syllabus expects you to organize your data: Arrays and Records.
1. What is a Data Structure?
A data structure is a specialized format for organizing, processing, storing, and retrieving data. Without them, a program would just be a chaotic mess of individual variables.
Analogy: Imagine a school. If every student's information was just written on a random scrap of paper and thrown into a box, the principal could never find anything! Instead, they use a "Student File" (a data structure) to keep everything in order.
Quick Review: Why use them?
- They make code easier to read.
- They allow us to handle large amounts of data (like a list of 1,000 users) without creating 1,000 different variables.
- They make it faster for the computer to search through information.
2. Arrays (The Organised Rows)
An array is a collection of data items of the same data type, stored under a single name. Think of it like a row of lockers in a school hallway.
One-Dimensional (1D) Arrays
A 1D array is like a simple list. To find a specific item, you use its index (its position number).
Important Rule: In programming, we almost always start counting at 0, not 1! This is called zero-based indexing.
Example: A list of high scores: \( scores = [95, 87, 92, 78] \).
\( scores[0] \) is 95.
\( scores[1] \) is 87.
Two-Dimensional (2D) Arrays
A 2D array is like a grid or a table. It has rows and columns. To find an item here, you need two index numbers: one for the row and one for the column.
Analogy: Think of a cinema seating plan or a chessboard. To find your seat, you need a Row number and a Seat number.
Example: A 2D array for a Tic-Tac-Toe board:
\( board[0][0] \) would be the top-left corner.
\( board[2][2] \) would be the bottom-right corner.
Memory Aid: "RC" Cola
When looking at 2D arrays, always remember Rows then Columns! \( Array[Row][Column] \).
Key Takeaway: Use Arrays when you have a list of items that are all the same type (e.g., a list of all Integers or a list of all Strings).
3. Records (The Digital Folders)
Sometimes, we want to store different types of information about one thing. For example, if you are writing a program about cars, you might want to store the Make (String), the Price (Real), and the Number of Doors (Integer) all together. This is where a Record comes in.
A record allows you to group together different data types under one umbrella. Each piece of information in a record is called a field.
Example of a Record Definition
According to the AQA syllabus, a record for a car might look like this in pseudo-code:
RECORD Carmake : String
model : String
reg : String
price : Real
noOfDoors : Integer
ENDRECORD
Did you know? In languages like Python, we often use "Lists of Dictionaries" or "Classes" to do the job of a Record, but for your exam, you should understand the concept as a Record.
Don't worry if this seems tricky!
Just remember:
1. Arrays = A list of the same stuff (like a carton of eggs).
2. Records = A collection of different stuff about one thing (like a passport showing your name, age, and photo).
Key Takeaway: Use Records when you need to store related data of different types (e.g., a student's name, their test score, and whether they passed).
4. Common Mistakes to Avoid
- Starting at 1: Remember, the first item in an array is always at index \( 0 \). If an array has 5 items, the last index is \( 4 \).
- Mixing Types in Arrays: In most languages, you can't put a String and an Integer in the same Array. Use a Record instead!
- Confusing Rows and Columns: In a 2D array, always go across to find the row first, then down for the column.
Summary Checklist
Quick Review: Can you...
- Explain what a data structure is?
- Identify the difference between a 1D and 2D array?
- Explain why we use index numbers?
- Describe what a Record is and name its individual parts (fields)?
- State when it's better to use a Record instead of an Array?