Arrays and Records in Pascal

July 24, 2024

Arrays and Records in Pascal

In programming, we often need to store and manipulate multiple values efficiently. In Pascal, two powerful data structures that help us achieve this are arrays and records. In this post, we will explore how to declare, initialize, and manipulate arrays and records in Pascal programs.

Understanding Arrays

An array is a collection of elements of the same type, stored in contiguous memory locations. Arrays allow us to handle multiple values under a single variable name, making it easier to manage data.

Declaring Arrays

To declare an array in Pascal, you specify the type of its elements and the number of elements it will hold. Here’s the syntax:

type
  ArrayName = array[lowerBound..upperBound] of ElementType;

For example, to declare an array of integers with 5 elements:

var
  numbers: array[1..5] of Integer;

Initializing Arrays

Arrays can be initialized at the time of declaration or later in the program. Here’s how to initialize the array we declared earlier:

begin
  numbers[1] := 10;
  numbers[2] := 20;
  numbers[3] := 30;
  numbers[4] := 40;
  numbers[5] := 50;
end;

Accessing and Manipulating Arrays

You can access array elements using their index. Here’s an example of how to print all elements of the array:

var
  i: Integer;
begin
  for i := 1 to 5 do
    WriteLn('Element ', i, ': ', numbers[i]);
end;

Understanding Records

Records in Pascal are similar to structures in other programming languages. They allow you to group different types of data under a single name. This is particularly useful when you want to represent a complex data structure.

Declaring Records

To declare a record, you define a new data type that contains various fields. Here’s the syntax:

type
  RecordName = record
    field1: FieldType1;
    field2: FieldType2;
    ...
  end;

For example, to declare a record to represent a student:

type
  Student = record
    name: string;
    age: Integer;
    grade: Real;
  end;

Using Records

Once you have declared a record type, you can create variables of that type and access their fields:

var
  student1: Student;
begin
  student1.name := 'Alice';
  student1.age := 20;
  student1.grade := 3.5;
  WriteLn('Name: ', student1.name);
  WriteLn('Age: ', student1.age);
  WriteLn('Grade: ', student1.grade);
end;

Combining Arrays and Records

One of the most powerful features of Pascal is the ability to combine arrays and records. For instance, you can create an array of records to store multiple entries of a data structure:

var
  students: array[1..100] of Student;
  i: Integer;
begin
  for i := 1 to 100 do
  begin
    students[i].name := 'Student ' + IntToStr(i);
    students[i].age := 18 + (i mod 5);
    students[i].grade := 3.0 + (i mod 4) * 0.5;
  end;
end;

Conclusion

Arrays and records are essential data structures in Pascal that enable you to store and organize data efficiently. By mastering these concepts, you can create more complex and functional Pascal programs. In the next lesson, we will delve deeper into file handling in Pascal, which will allow us to work with data persistence.