Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Algorithmics and Data

Structures

Records
Introduction

So far, to solve problems, we have only used primitive types


(characters, integers, reals, strings) and arrays.

In this course, we will explore another type.


Example:
Let there be a car park containing N vehicles, where each vehicle is
characterized by a registration number, brand, model, color,
number of seats, power and price which are information of different
sizes and types.
Display the information of vehicles of a brand M with a price lower than a value P.

To solve this problem, we can propose to use seven arrays. Each array contains one of this
information.

This is a solution, but it is not practical!

The best thing is to group all this information so that we can manipulate them at the same
time.

So far, we can only group variables of the same type (Arrays).

But since it is interesting, we propose a new structure:

Records
A record is a user-defined data type that allows grouping a finite number of elements
(called fields) of possibly different types.
The record allows the user to define their own types based on the basic types. We can
therefore have an infinity of types.

Record Declaration:

A record can be declared in the declaration part as a structured variable.

<RecName> : Record
<Field_Name1> : <TypeF1>;
<Field_Name2> : <TypeF2>;
<Field_Name3> : <TypeF3>;
---
<Field_NameN> : <TypeFN>;
End;
<RecName>: identifier (Group Name)
<Field_Namei>: field identifier (a group element)
<TypeFi>: type of field.
Example

Vehicle: Record

Registration: String[11];

Brand: String[15];

Model, Color: String[10];

NBSeats, Power: integer;

Price: real;

End;

Remark: A record is a user-defined data type.

The declaration of types is done in the declaration section of the algorithm, to which a Type
section is added, in addition to Const and Var sections.
Algorithme AlgoName;
Type
Vehicle = Record
Registration: String[11];
Brand: String[15];
Model, Color: String[10];
NBSeats, Power: integer;
Price: real; Becomes a
predefined
End; type.
Var
V1,V2,V3: Vehicle;
Operations on Records
A referenced field
A referenced field is preceded by the group name (last name) followed by the field name.

Syntax: <RecName>.<FieldName>

Example: V1.Registration, V2.Price

A referenced field is manipulated exactly like a basic type variable. All operations on the
variable type are allowed.

Example

Initialization: V2.Price ← 0;

Reading: Read(V2.Brand);

Writing: Write(V2.Seats);

Test: If V2.Price>2000000 Then ...


Remark All operations are performed on the record fields.

The only operation allowed on the record itself is the assignment between records.

Example: V1 ← V2; copies all V2 fields to V1.

Let's go back to the example given in the introduction

Display the information of vehicles of a brand M with a price lower than a value P.

We will use a single array whose elements are of type Vehicle.


Algorithme Park_car;
Type
Vehicle = Record
Registration: String[11];
Brand: String[15];
Model, Color: String[10];
NBSeats, Power: integer;
Price: real;
Var TV : Array[1..200] of Vehicle;
V: Vehicle;
I,N : Integer; P:real;
M: String[15];
Begin
Write(‘Give Vehicle's number’); Repeat Read(N); Until (N>0 And N ≤200);
For I←1 à N
Do
Write(‘Give the informations of vehicle number: ’,I);
Write(Registration : ‘); Read(V.Registration);
Write(Brand : ‘); Read(V.Brand);
Write(‘Model : ‘); Read(V.Model);
Write(‘Color : ‘); Read(V.Color);
Write(‘Number of Seats: ‘); Read(V.NBSeat);
Write(‘Puissance: ‘); Read(V.Puissance);
Write(‘Price: ‘); Read(V.Price);
TV[I]←V;
Done;
Write(‘Give a brand’); Read(M);
Ecrire(‘Give a price’); Read(P);
//Display information
For I←1 à N
Do
If(V[I].Brand=M AND V[I].Price <P)
Then
Write(V[I].Registration);
Write(V[I].Brand);
Write(V[I].Model);
Write(V[I].Color);
Write(V[I].NBSeat);
Write(V[I].Power);
Write(V[I].Price);
Eif;
Done;
End.

Attention: common recurring errors

Do not use Read(V) or Write(V); the only allowed operation on V is assignment.

Do not use the type name instead of the variable:


Another Example

Let a record describing a teacher by:

● A Last name and First name


● A birth date (day, month and year)
● A recruitment date (day, month and year)
● Personal address (Street, City, Wilaya)
● Employer
● Employer address (Street, City, Wilaya)

The first declaration can be:

Teacher= Record
Name,Pname : String[25];
BD_Day, BD_Month,BD_Year : Integer;
RD_Day, RD_Month,RD_Year : Integer;
PA_Street, PA_City, PA_Wilaya: String[20];
Employer : String[10];
EA_Street, EA_City, EA_Wilaya: String[20]; End;
This declaration can be simplified because the birth date and recruitment date are of the
same type, as well as the two addresses. To do this, we use:

Nesting Records

Date = Record
Day, Month, Year : Integer;
End;
Address = Record
Street, City, Wilaya: String[20];
End;
Teacher = Record
Name,PName: String[25];
BirD,RecD: Date;
PerAd,EmpAd: Address;
Employer : String[10];
End;
Reference fields of nested records

To reference a field in a nested record at any level, the same principle is followed. Before
reaching the field, at each level, we use the name of the parent record followed by a dot
(●).

Example:

If T is a variable of type Teacher, accessing T's fields is done as follows:

T.Name, T.BirD.Day, T.PerAd.City

Remark

If the record contains multiple levels of nesting, it is necessary to provide the entire path
from the root record to the deepest one, always separated by dots (●).

Root.Level1.Level2 ...... Levelk.Field


Another simplification
In the case of nested records, it is noticeable that accessing fields becomes cumbersome,
especially with multiple levels of nesting (having to provide the entire path for each field).

To avoid repeating this path each time, the use of an instruction is proposed to simplify the
writing is proposed:

With instruction
Syntax

With<Record_NAme>
Do
<Actions>;
Done;

<Record_Name>: refers to the record name or the whole path.

<Actions>: sequence of actions manipulating the fields without specifying the path.
Example
With T
Do
Read(Name,Pname); Instead of
Done; Read(T.Name, T.Pname);
Remark:
If the fields do not belong to the same nesting level, it is not possible to
manipulate the fields of lower levels directly. However, you can provide a
list of paths separated by commas or use nested 'With' statements.
With T With T.BirD
Do Do
Read(Name,Pname); Read(Name,Pname);
Read(Day, Month, Year); Read(Day, Month, Year);
Done; Done;

With T
Do
Read(Name,Pname);
With BirD
Read(Day, Month, Year);
Done;
Done;

You might also like