Welcome to Data Types and Structures!

In this chapter, we are going to learn how computers organize information. Think of data types as different shaped containers in a kitchen—you wouldn’t put soup in a sieve or flour in a shallow tray. Similarly, computers use specific "containers" to store different kinds of data efficiently. Whether you are building a simple calculator or a complex game, understanding these structures is the first step toward being a great programmer. Don’t worry if this seems a bit abstract at first—we’ll use plenty of everyday analogies to help it click!

10.1 Data Types and Records

Before a computer can process data, it needs to know what kind of data it is. This is called a Data Type.

Basic Data Types

In the 9618 syllabus, you need to be familiar with these specific types:

INTEGER: Whole numbers (e.g., 10, -5, 1000).
REAL: Numbers with a decimal point (e.g., 3.14, -0.5, 2.0).
CHAR: A single character (e.g., 'A', '!', '5').
STRING: A sequence of characters/text (e.g., "Hello World").
BOOLEAN: Can only be TRUE or FALSE.
DATE: Represents a specific day (e.g., 25/12/2024).

What is a Record?

Sometimes, we want to group different types of data together because they belong to the same "thing." This is called a Record.

Real-world Analogy: Imagine a library card. It contains a STRING (the book title), an INTEGER (the book ID), and a DATE (the due date). All these different types are kept together under one "Record."

Defining a Record in Pseudocode

To create a record, we use a special structure. Here is how you might define a record for a Student:
TYPE StudentRecord
    DECLARE Name : STRING
    DECLARE Age : INTEGER
    DECLARE IsEnrolled : BOOLEAN
ENDTYPE

Quick Review: Records allow us to treat several related items as a single unit, making our programs much more organized!

10.2 Arrays

An Array is like a row of lockers in a school. Each locker has the same size and is used for the same purpose, and every locker has a number so you can find it.

Key Terms for Arrays

Index: The "address" or position of an item in the array.
Lower Bound: The starting index (usually 0 or 1).
Upper Bound: The last index in the array.
1D Array: A single row of data.
2D Array: A grid of data (like a spreadsheet with rows and columns).

Important Rule: All items in an array must be of the same data type. You can't have an array that stores both an Integer and a String!

Finding and Sorting Data

Once data is in an array, we often need to search for things or put them in order.

1. Linear Search (The "One-by-One" Method)

Imagine you are looking for a specific shirt in a pile of laundry. You pick up the first one, check it, then the second, and so on, until you find it. That is a Linear Search. It is simple but can be slow if the array is very large.

2. Bubble Sort (The "Swap" Method)

This is a way to sort an array into order. It works by comparing two neighboring items. If they are in the wrong order, they "swap." This repeats until the largest items "bubble" up to the end of the list.

Common Mistake to Avoid: When using a 2D array, always remember which index is for the Row and which is for the Column. Usually, it is written as MyArray[Row, Column].

Key Takeaway: Arrays are for storing multiple items of the same type. Use 1D for lists and 2D for tables.

10.3 Files

Why do we need Files? Everything we store in variables or arrays is kept in the computer's RAM. When you turn off the computer, RAM is cleared. To keep data permanently, we must save it to a file on a hard drive or SSD.

In your exams, you will see pseudocode for:
OPENFILE: Telling the computer which file you want to use.
READFILE: Taking data out of the file to use in your program.
WRITEFILE: Putting data into the file to save it.
CLOSEFILE: Always "lock the door" when you're finished!

Did you know? A text file is essentially a long string of characters stored on your disk that your program reads line by line.

10.4 Introduction to Abstract Data Types (ADT)

An Abstract Data Type (ADT) is a collection of data that has a specific set of rules for how you can add or remove items. We usually implement ADTs using Arrays.

1. The Stack (LIFO)

LIFO stands for Last In, First Out.
Analogy: Think of a stack of cafeteria trays. The last tray put on top of the pile is the first one someone picks up.
Push: Adding an item to the top.
Pop: Removing the item from the top.

2. The Queue (FIFO)

FIFO stands for First In, First Out.
Analogy: A line (queue) at a movie theater. The first person to join the line is the first one to get their ticket.
Enqueue: Adding an item to the back.
Dequeue: Removing an item from the front.

3. The Linked List

A Linked List is a collection of "nodes." Each node contains the data AND a Pointer to the next item in the list.
Analogy: A scavenger hunt. You find a clue (the data), and that clue tells you where the next clue is (the pointer). If a pointer is Null, it means you’ve reached the end of the list.

Key Differences Summary Table

Stack: Uses a single pointer (Top of Stack). Order: LIFO.
Queue: Uses two pointers (Front and Rear). Order: FIFO.
Linked List: Uses pointers to connect nodes; items don't have to be stored next to each other in memory.

Memory Aid: Just remember "S-L-Q-F"Stacks are LIFO, Queues are FIFO!

Final Encouragement: You've just covered the core ways computers handle data! While the logic of pointers and bubble sorts can be tricky, just keep picturing those real-world analogies like lockers, laundry piles, and cafeteria trays. You've got this!