Download presentation
Presentation is loading. Please wait.
Published byPenelope Farmer Modified over 6 years ago
1
RECORDS Introduction Declaring a record Using records
The with...do statements Array of records
2
0. Introduction to store and process related data items of different types E.g., an employee record contains: employee’s name (string), sex (character), age (integer), monthly salary (real)
3
1. Declaring a record record a record is
a collection of related records. structured data type record record <field identifier-1> : <data type-1>; <field identifier-2> : <data type-2>; . <field identifier-i> : <data type-i>; end.
4
1. Declaring a record programmer’s name Example
Suppose we want to keep a record for a programmer of a software house, which contains a field for each of the following: programmer’s name (John Wong) sex (M) age (22) monthly salary ( )
5
1. Declaring a record preferable with arrays var Programmer : record
Name : string; Sex : char; Age : integer; MonthlySalary : real end; type EmployeeInfo = record var Programmer : EmployeeInfo; preferable with arrays
6
2. Using Records Accesssing record fields Assigning values to a record
Copying records
7
2.1 Accessing record fields
John Wong M Programmer Name Sex Age MonthlySalary <record name>.<field name> e.g., accessing record Programmer Programmer.Name Programmer.Sex Programmer.Age Programmer.MonthlySalary using writeln statements to output
8
2.2 Assigning values to a record
by assignment statements Programmer.Name := ‘Mary Chan’; Programmer.Sex := ‘F’; Programmer.Age := 20; Programmer.MonthlySalary := 12000; by input statements readln( Programmer.Name ); readln( Programmer.Sex ); readln( Programmer.Age); readln( Programmer.MonthlySalary );
9
2.3 Copying records to transfer information in one record to another record Example type EmployeeInfoA = record ... end; EmployeeInfoB = record ... end; var Employee1, Employee2 : EmployInfoA; Employee3 : EmployInfoB; .. Employee1 := Employee2; Which one is correct? Employee1 := Employee3;
10
3. The with...do statements with <record name> do
for input statements for the record Programmer in 2.2 readln( Programmer.Name ); readln( Programmer.Sex ); readln( Programmer.Age); readln( Programmer.MonthlySalary );
11
3. The with...do statements with <record name> do
can be written as: with Programmer do begin readln(Name); readln(Sex); readln(Age); readln(MonthlySalary); end;
12
4. Array of Records Problem: a telephone directory which contains the names and the phone numbers of a number of friends. What kind of array is the following words represent? Size = 100; type NameArray = array[1..Size] of string[20]; PhoneNoArray = array[1..Size] of string[8]; var Name : NameArray; PhoneNo : PhoneNoArray; PARALLEL ARRAY
13
4. Array of Records record + array = an array of records
const Size = 100; type Str20 = string[20]; Str8 = string[8]; Info = record Name : Str20; PhoneNo : Str8; end; InfoArray = array[1..Size] of Info; var Friend : InfoArray;
14
4. Array of Records Name[1] PhoneNo[1] Name[2] PhoneNo[2] Name[3] PhoneNo[3] . . Name[100] PhoneNo[100] Fig.1 Parallel arrays Name PhoneNo Friend[1] Friend[2] Friend[3] . Friend[100] Fig.2 An array of records
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.