Introduction to Data Structures
Hi there! Welcome to one of the most important chapters in your Computer Science journey. So far, you have probably used variables to store single pieces of information, like a score or a name. But what happens when you have 100 high scores to track, or a complex list of details about a car?
That is where Data Structures come in! Think of a data structure as a way of organizing and storing data so that we can find it and use it efficiently. It’s the difference between throwing all your clothes in a pile on the floor versus neatly organizing them in a chest of drawers. In this section, we will look at Arrays and Records.
1. What is a Data Structure?
At its simplest, a Data Structure is a specialized format for organizing, processing, retrieving, and storing data.
Analogy: Imagine you are a librarian. If you just throw every book into a giant pile in the middle of the room, it will take you hours to find one specific book. However, if you use bookshelves (a data structure) and organize them by genre or author, you can find what you need in seconds!
Quick Review:
• A variable stores one piece of data.
• A data structure stores multiple pieces of data in an organized way.
2. One-Dimensional (1D) Arrays
A 1D Array is like a single row of lockers in a school hallway. Each locker has a number, and inside each locker, you can store one item. In programming, all the items in an array must usually be of the same data type (for example, all integers or all strings).
How to find things in an Array (Indexing)
To get an item out of an array, we use its index.
Crucial Rule: In most programming languages (and in AQA pseudo-code), we start counting at 0, not 1! This is a very common mistake, so keep a close eye on it.
Example: If we have an array called Names:
Index 0: "Alice"
Index 1: "Bob"
Index 2: "Charlie"
To access "Alice", you would write Names[0].
Did you know?
Starting at 0 is called Zero-based indexing. Computer scientists do this because it makes the math behind the scenes much faster for the processor!
Key Takeaway: A 1D array is a list of related data items of the same type, accessed by a single index number starting at 0.
3. Two-Dimensional (2D) Arrays
If a 1D array is a row of lockers, a 2D Array is like a whole wall of lockers with rows and columns. You can also think of it like a chessboard or a spreadsheet.
Finding data in 2D
To find a specific piece of data in a 2D array, you need two index numbers: one for the row and one for the column. We usually write this as array[row][column] or array[row, column].
Analogy: Think of a cinema seating plan. To find your seat, you look for the Row (e.g., Row 5) and then the Seat Number (e.g., Seat 12).
Memory Aid: Remember "RC" (like a Remote Control car) — Row first, then Column!
Quick Review:
• 1D Array = A simple list.
• 2D Array = A table or grid with rows and columns.
4. Records
Don't worry if arrays feel a bit restrictive because they only store one type of data. That’s where Records save the day!
A Record is a data structure that can store a collection of related items where each item can be a different data type. We call each individual item in a record a Field.
Real-World Example: Imagine you are making a program to track cars in a garage. For each car, you want to store its Make (String), its Price (Real), and its Number of Doors (Integer). An array couldn't do this easily, but a record can!
The Syllabus Example: A Car Record
Here is how a record might be defined in pseudo-code:
RECORD Car
make : String
model : String
reg : String
price : Real
noOfDoors : Integer
ENDRECORD
Key Takeaway: Use an Array for a list of the same things. Use a Record for a collection of different details about one thing.
5. Why use Data Structures?
You might be thinking, "Why can't I just use 20 different variables?" Well, using data structures makes your code much "cleaner" and easier to manage.
• Efficiency: You can use iteration (loops) to look through an array of 1,000 items in just three lines of code.
• Organization: It keeps related data together, which makes the program easier for other humans to read.
• Reduced Errors: It's much harder to lose track of data when it's stored in a structured way.
Common Mistakes to Avoid
• Starting at 1: Always remember that the first item in an array is at index \( 0 \).
• Out of Bounds: If your array has 5 items, the last index is 4. If you try to access index 5, the program will crash! This is called an "index out of bounds" error.
• Mixing Types in Arrays: Remember that all items in an array must be the same type (e.g., all integers). If you need different types, use a Record.
Summary: The Big Ideas
• Data Structures organize data so programs are more efficient.
• 1D Arrays are like lists; they use one index starting at 0.
• 2D Arrays are like tables; they use two indices (Row and Column).
• Records are used to store different types of data about a single object using fields.
Don't worry if this seems tricky at first! The more you practice writing small programs with arrays, the more natural it will feel. You've got this!