Day 10
This is the Day 10 page for computer programming course 101, an introduction to computer programming. Day 10 teaches the concept of complex user defined types.Below are the topics and course materials we are learning today.
Complex Types
In our previous lessons we touched on intrinsic types such a String, Integer, Char, and Single. In out last session we discussed user defined such as type aliases, sub ranges, enumerations, and sets. All of these types reference a single piece of information generally of a fixed size. Complex types are types that are user defined with various sizes dependent upon how you define them.Records
The first complex type we'll examine is the record. A record (called to a structure in some languages) represents a collection of various types. Each item in the collection is called a is called a field. The declaration of a record type specifies a name and type for each field. The syntax of a record type declaration is as follows:type
TRecordName =
record
Field1: TypeName1;
Field2: TypeName2;
Field3: TypeName3;
...
end
;
Record Example
For example, the following declaration creates a record type called TDateRec.type
TDateRec =
record
Year: Integer;
Month: (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
Day:
1..31
;
end
;
Using a Record
The TDateRec type declaration, however, does not allocate any memory for the Year, Month, and Day fields. Memory is allocated when you declare a variable of type, like so:var
StartDate, FinishDate: TDateRec;
StartDate
.
Year :=
1776
;
StartDate
.
Month := Jul;
StartDate
.
Day :=
4
;
with
StartDate
do
begin
Year :=
1776
;
Month := Jul;
Day :=
4
;
end
;
FinishDate := StartDate;
FinishDate
.
Year :=
2050
;
Other Records
Here are a few example of other record types. Examine them and see if you can understand their meanings.type
TPoint =
record
X, Y: Integer;
end
;
TRectangle =
record
X, Y, Width, Height: Integer;
end
;
TSuit = (Diamonds, Clubs, Hearts, Spades);
TRank = (Ace, Two, Three, Four, Five, Six,
Seven, Eight, Nine, Ten, Jack, Queen, King);
TCard =
record
Suit: TSuit;
Rank: TRank;
FaceDown: Boolean;
end
;
TCard =
record
Suit: TSuit;
Rank: TRank;
FaceDown: Boolean;
end
;
TAddress =
record
Street:
string
;
City, State, ZipCode:
string
;
end
;
TPerson =
record
Name:
string
;
Birthday: TDateRec;
Address: TAddress;
end
;
TBusiness =
record
Name:
string
;
StockSymbol:
string
;
Headquarters: TAddress;
ChiefExecutive: TPerson;
end
;
Arrays
Another complex type is the array type. An array represents an indexed collection of elements of the same type. The syntax of an array type declaration is as follows:type
TArrayName =
array
[LowBound
..
HighBound]
of
SomeType;
Array Example
For example, the following declaration creates a array type called TDeck.type
TDeck =
array
[
1..52
]
of
TCard;
procedure
BuildDeck(
var
Deck: TDeck);
const
Range = Ord(High(TRank)) +
1
;
var
Deck: TDeck;
I: Integer;
begin
for
I := Low(Deck)
to
High(Deck)
do
begin
Deck[I].Suit := Low(TSuit);
Deck[I].Rank := Low(TRank);
Inc(Deck[I].Suit, (I -
1
)
div
Range);
Inc(Deck[I].Rank, (I -
1
)
mod
Range);
Deck[I].FaceDown := True;
end
;
end
;
Array Boundaries
Arrays can have any upper and lower bounds. You set these bounds when you declare an array. For example:type
TDeck =
array
[
1..52
]
of
TCard;
type
TDeck =
array
[
0..51
]
of
TCard;
Array Dimensions
The array examples we've look at so far are one dimensional arrays. That is, they are arrays with a single pair of upper and lower bounds. In most programming languages, and Pascal as well, you can define multi dimensional arrays.Consider the following example:
type
TDailyReminders =
array
[
0..11
,
0..31
]
of
string
;
Reminder := DailyReminders[Month, Day];
Dynamic Arrays
Situations may arise where not know exactly how large to make an array. You may want to have the capability of changing the size of the array at runtime. A dynamic array declares an array with a type type, but no boundaries.var
Students:
array
of
string
;
SetLength(Students,
14
);
To create a two-dimensional dynamic array, use the following code:
type
TMatrix =
array
of
array
of
Double;
var
Matrix: TMatrix;
begin
SetLength(Matrix,
10
,
20
)
// ...
end
;
Matrix :=
nil
;