Pascal allows the use of enumerated types as index types. For example, if we def
ID: 3838569 • Letter: P
Question
Pascal allows the use of enumerated types as index types. For example, if we define the enumerated type GreatLakes to have the values Erie, Ontario, Huron, Michigan, and Superior (ordered here by increasing volume of water), then we can define:
type GreatLakes = (Erie, Ontario, Huron, Michigan, Superior);
area: array [ GreatLakes ] of integer,
elevation: array [ GreatLakes ] of real;
(and give them values)
area := ( 9910, 7340, 23000, 22300, 31700 );
elevation := ( 570.38, 244.77, 578.68, 578.68, 600.38 );
Now we can access individual elements to find, for example, that
area[Erie] = 9910
area[Superior] = 31700
elevation[Huron] = elevation[Michigan] = 576.68
In the above definitions we stored the areas and elevations of the Great Lakes in vectors (1- dimensional arrays).
Restate these definitions in order to store the same information in a structure that is:
(a) A vector of records
For defining records, use the syntax:
Person: record
age: integer;
sex: (Male, Female);
height, weight: real;
married: Boolean;
end;
(b) A record of vectors
(c) A 2-dimensional array indexed by type GreatLakes and by another enumerated type.
Explanation / Answer
Sometimes it's necessary to express values as names and that is what Pascal tried to do. Enumerated data types restrics our scope to the limited values available. A record is defined as
Person: record
age: integer;
sex: (Male, Female);
height, weight: real;
married: Boolean;
end;
And we have data as Great Lakes, their area and their elevation.
Hence, we define a record
GreatLakes: record
area: integer;
name: (Erie, Ontario, Huron, Michigan, Superior);
elevation: real;
end;
Now, we can go on to define a vector of this record i.e. creating instances of GreatLakes.
Second method is creating record of vectors i.e. we have only one record and inside it we have many vectors.
type
GreatLakes:
Area:
Elevation:
Third method is the simplest creating 2-D array
Great Lakes ---> all the lakes
Area --> respective area
Elevation --> respective elevation
The above figure can be arranged in rows and columns to get the required result. I hope this answers your question.