👋 Welcome to the World of Organized Data!

Hello future Computer Scientists! This chapter, Data Structures, is incredibly important because it moves you beyond storing data one piece at a time. Think of it as upgrading from keeping individual coins in your pockets to organizing them neatly in a cash register.

In the "Programming" section, learning how to structure data allows you to write programs that handle huge amounts of information efficiently—like managing scores for a whole class or storing details for hundreds of customers. Don't worry if this seems tricky at first; we will break down the concepts using simple analogies!

What is a Data Structure?

A data structure is simply a way of storing and organizing data in a computer so that it can be used and accessed efficiently.

Think about your kitchen:

  • A single variable (like Age) is like a single potato.
  • A data structure (like an Array) is like a bag of potatoes—many related items grouped together.
  • Another data structure (like a Record) is like a recipe card, holding different types of information (Name: string, Prep Time: integer, Rating: real number).

Key Takeaway: Data structures help us manage large amounts of related data using a single name instead of hundreds of separate variable names.


1. One-Dimensional (1D) Arrays

What is a 1D Array?

A 1D Array is the simplest form of a structured list. It is a collection of data items that are all of the same data type, stored one after the other in a sequence.

Analogy: Imagine a row of numbered mailboxes. Each mailbox holds one piece of information, and the number on the box is how we find it.

Key Concepts of 1D Arrays
  • Homogeneous: All items (called elements) must be the same type (e.g., all integers, or all strings).
  • Index (or Subscript): This is the position number used to access a specific element in the array. Indices usually start at 0 or 1, depending on the programming language.
Accessing Data in a 1D Array (Indexing)

Let’s say we have an array called Scores that stores the points gained by five students.

Index 1 2 3 4 5
Value 15 22 18 30 10

To find the score 18, we would ask for Scores[3]. The number 3 is the index.

💡 Memory Aid: 1D means you only need One Direction to find your data—you just move along the line.

Using 1D Arrays in Programming

Arrays are perfect for:

  • Storing daily temperatures for a week.
  • Holding the names of all players on a team.
  • Storing a list of password attempts.

Quick Review: 1D Arrays

They are lists. They hold one type of data. They use one index to locate items.


2. Two-Dimensional (2D) Arrays

What is a 2D Array?

A 2D Array is an array where the elements are organized in a grid format, like a table or a spreadsheet. You can think of it as a list of 1D arrays stacked on top of each other.

Analogy: This is like a seating plan in a cinema. To find your seat, you need two pieces of information: the Row number AND the Seat number.

Key Concepts of 2D Arrays

To locate any element in a 2D array, you need two indices:

  1. The first index usually refers to the Row.
  2. The second index usually refers to the Column.

The structure is often described as (Row, Column).

Accessing Data in a 2D Array

Let’s imagine an array called Grades that stores the scores of 3 students across 4 different tests.

Col 1 (Test A) Col 2 (Test B) Col 3 (Test C) Col 4 (Test D)
Row 1 (Student A) 85 70 92 75
Row 2 (Student B) 60 95 88 80
Row 3 (Student C) 90 80 70 99

If we want to find Student B’s score on Test C, we would look for the value at Grades[2, 3], which is 88.

Don't worry if this seems tricky at first! Just remember: the first number tells you which horizontal line (row) to look at, and the second number tells you how far across (column) to go.

Uses of 2D Arrays

2D arrays are essential for:

  • Storing board games (like coordinates on a chessboard).
  • Representing images (where each element is a pixel color).
  • Storing monthly sales data for multiple years.

Common Mistake to Avoid: Students sometimes swap the row and column indices. Always confirm which way your program indexes first (Row, Column) or (Column, Row) and stick to it!

Key Takeaway: 2D Arrays store data in grids and require two indices to specify location.


3. Records (Structures)

Grouping Different Data Types Together

We learned that an Array must hold data of the same type (all integers, or all strings). But what if we need to store information about one complex entity, like a student, where the data includes text, numbers, and perhaps Boolean values?

This is where a Record (sometimes called a Structure) comes in.

What is a Record?

A Record is a collection of related items (called fields) that can be of different data types, grouped together under a single name.

Analogy: A library catalogue card. It groups the Book Title (string), the Author (string), and the Shelf Number (integer) together, even though they are different types of data.

Defining a Record Structure

Before we can create a record, we must define its structure (the fields it contains).

Example: Defining a structure for a single item in a shop's inventory:

    

DEFINE RECORD ItemDetails

Name : STRING

ItemID : INTEGER

Price : REAL

InStock : BOOLEAN

END RECORD

Accessing Data in a Record (Field Access)

Once the structure is defined, we can create variables of that type. Let's create a variable called Laptop using the ItemDetails structure.

To access a specific piece of information (a field), you typically use the Record variable name followed by a dot (`.`) and the field name:

  • To find the price: Laptop.Price
  • To check if it's in stock: Laptop.InStock
Records vs. Arrays: The Big Difference
Feature Array (1D or 2D) Record
Data Types Must be Homogeneous (all the same type). Can be Heterogeneous (different types mixed).
Access Method Access elements using a numbered Index (e.g., Array[5]). Access elements using a Field Name (e.g., Record.Name).
Did you know?

In programming, records are often used to define objects or user-defined types, making code much clearer. If you were storing 100 students, you would create an Array of Records! This combines the structure of a Record (for complex student data) with the list functionality of an Array (for holding 100 students).

Key Takeaway: Records group different types of data (fields) together to describe one complex entity.


Chapter Summary: Organizing Your Code

Congratulations! You now understand the basic building blocks of organizing data in programming.

🧠 Quick Data Structure Review

  • 1D Array: A simple list. Needs one index. All items must be the same type.
  • 2D Array: A grid/table. Needs two indices (Row, Column). All items must be the same type.
  • Record: Defines a complex entity (like a Student or Item). Stores different data types (fields) together and is accessed by the field name.

By mastering these structures, you are ready to write powerful programs that can store and manage data professionally. Keep practicing those indexing methods, and you’ll be an expert in no time!