Professional Documents
Culture Documents
Algo - Les Enrengistrements 1
Algo - Les Enrengistrements 1
Structures
Records
Introduction
To solve this problem, we can propose to use seven arrays. Each array contains one of this
information.
The best thing is to group all this information so that we can manipulate them at the same
time.
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:
<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];
Price: real;
End;
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>
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);
The only operation allowed on the record itself is the assignment between records.
Display the information of vehicles of a brand M with a price lower than a value P.
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:
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 (●).
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;
<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;