Chapter 5: Data Structures
Data structures organize and manage the data in a program. Pascal’s built-in data structures let you store and work with data efficiently, and this chapter covers three of them: arrays, records, and sets.
Arrays
An array is a collection of elements of the same type, stored in contiguous memory locations. It holds several values in a single variable, and you reach each value through its index.
Declaring arrays
var
arrayName: array[indexRange] of dataType;
Example
program ArrayExample;
var
numbers: array[1..5] of integer;
i: integer;
begin
{ Initializing array elements }
for i := 1 to 5 do
numbers[i] := i * 2;
{ Accessing array elements }
for i := 1 to 5 do
writeln('numbers[', i, '] = ', numbers[i]);
end.
Here numbers is an array of integers with indices from 1 to 5. The program sets each element in one loop and reads them back in another.
Multidimensional arrays
Pascal also supports multidimensional arrays. A common one is the two-dimensional array, which you can picture as a table of rows and columns.
Example
program MultiArrayExample;
var
matrix: array[1..3, 1..3] of integer;
i, j: integer;
begin
{ Initializing matrix elements }
for i := 1 to 3 do
for j := 1 to 3 do
matrix[i, j] := i * j;
{ Accessing matrix elements }
for i := 1 to 3 do
begin
for j := 1 to 3 do
write(matrix[i, j]:4);
writeln;
end;
end.
matrix is a two-dimensional array with 3 rows and 3 columns. Nested loops set its elements and then read them back.
Records
A record is a composite data type that groups data of different types together, which makes records a good fit for complex data structures.
Declaring records
type
RecordName = record
field1: dataType1;
field2: dataType2;
...
end;
Example
program RecordExample;
type
Person = record
name: string;
age: integer;
salary: real;
end;
var
employee: Person;
begin
{ Initializing record fields }
employee.name := 'Alice';
employee.age := 30;
employee.salary := 50000.0;
{ Accessing record fields }
writeln('Name: ', employee.name);
writeln('Age: ', employee.age);
writeln('Salary: ', employee.salary:0:2);
end.
Person is a record type with three fields: name, age, and salary. The employee variable is an instance of Person.
Sets
A set is a collection of elements of the same type, with no duplicates. Sets are handy whenever you need membership tests, union, intersection, or difference.
Declaring sets
var
setName: set of baseType;
Example
program SetExample;
type
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
var
weekdays, weekend: set of Day;
d: Day;
begin
weekdays := [Mon, Tue, Wed, Thu, Fri];
weekend := [Sat, Sun];
{ Set operations }
if Tue in weekdays then
writeln('Tuesday is a weekday');
writeln('Weekend days: ');
for d in weekend do
write(d, ' ');
writeln;
end.
Day is an enumerated type for the days of the week, and weekdays and weekend are sets of type Day. The program checks whether a day is in a set, then iterates over a set’s elements.
Set operations
Pascal’s set operations include:
- Union:
+ - Intersection:
* - Difference:
- - Symmetric difference:
><
To add or remove a single element, use the procedures Include(s, x) and Exclude(s, x).
Example
program SetOperationsExample;
type
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
DaySet = set of Day;
procedure PrintSet(const title: string; s: DaySet);
var
d: Day;
begin
write(title, ': ');
for d in s do
write(d, ' ');
writeln;
end;
var
setA, setB, resultSet: DaySet;
begin
setA := [Mon, Wed, Fri];
setB := [Wed, Thu, Fri, Sat];
resultSet := setA + setB; { Union }
PrintSet('Union', resultSet);
resultSet := setA * setB; { Intersection }
PrintSet('Intersection', resultSet);
resultSet := setA - setB; { Difference }
PrintSet('Difference', resultSet);
resultSet := setA >< setB; { Symmetric Difference }
PrintSet('Symmetric Difference', resultSet);
end.
writeln can’t print a whole set, so PrintSet loops over the elements and prints each one.