Download presentation
Presentation is loading. Please wait.
1
(c)opyright Brent M. Dingle 2001
Pascal Records Brent M. Dingle Texas A&M University Chapter 11 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan) Fall 2001 (c)opyright Brent M. Dingle 2001
2
Record Types - Motivation
So far our only structured data type has been arrays. A difficulty of arrays is they only allow the SAME data type to be stored within them. What if we wanted to store a bunch of DIFFERENT data types together? Do we really want to use a bunch of parallel arrays? Fall 2001 (c)opyright Brent M. Dingle 2001
3
(c)opyright Brent M. Dingle 2001
Consider the problem: We want to read in a bunch of information about many students and we want it all organized by name (or id number). So the information on each student is: Student Name Student ID Number 5 Quiz Grades 3 Test Grades 7 Lab Grades Fall 2001 (c)opyright Brent M. Dingle 2001
4
How would we store all that?
Currently we would need to make 5 different (parallel) arrays: VAR Name : array [1..NUM_STUDS] of string; ID : array [1..NUM_STUDS] of string; Quizzes : [1..NUM_STUDS][1..5] of real; Tests : [1..NUM_STUDS][1..3] of real; Labs : [1..NUM_STUDS][1..7] of real; Fall 2001 (c)opyright Brent M. Dingle 2001
5
(c)opyright Brent M. Dingle 2001
Organizing it Then once we had all those arrays initialized properly, We would have to sort the arrays by name (or id) and maintain the order of all of them. This is extremely prone to error. There must be an easier way. Fall 2001 (c)opyright Brent M. Dingle 2001
6
(c)opyright Brent M. Dingle 2001
Easier with Records Fortunately Pascal allows us to do this in a much easier fashion. We get to use Records. But let’s look at a really simple case to illustrate all this. Fall 2001 (c)opyright Brent M. Dingle 2001
7
(c)opyright Brent M. Dingle 2001
Simple student info Let’s reduce the student information to be just: Student Name Student ID Student Grade Fall 2001 (c)opyright Brent M. Dingle 2001
8
(c)opyright Brent M. Dingle 2001
To store ONE student We would need the following declarations: TYPE NAME_TYPE = string[40]; ID_TYPE = string[9]; GRADE_TYPE = real; VAR name : NAME_TYPE; id : ID_TYPE; grade : GRADE_TYPE; Notice we use THREE variables. Fall 2001 (c)opyright Brent M. Dingle 2001
9
Using records to store ONE student
We would need the following declarations: TYPE NAME_TYPE = string[40]; ID_TYPE = string[9]; GRADE_TYPE = real; STUD_TYPE = RECORD name : NAME_TYPE; id : ID_TYPE; grade : GRADE_TYPE; END; VAR stud : STUD_TYPE; Notice we use ONE variable. Fall 2001 (c)opyright Brent M. Dingle 2001
10
(c)opyright Brent M. Dingle 2001
And if we had 100 students So if we had more than one student we would need to make parallel arrays based on the above. to make an array of the above record type Compare the following Fall 2001 (c)opyright Brent M. Dingle 2001
11
Using parallel arrays we would have the following
CONST NUM_STUDS = 100; TYPE NAME_TYPE = string[40]; ID_TYPE = string[9]; GRADE_TYPE = real; NAME_ARRAY = array[1..NUM_STUDS] of NAME_TYPE; ID_ARRAY = array[1..NUM_STUDS] of ID_TYPE; GRADE_ARRAY = array[1..NUM_STUDS] of GRADE_TYPE; VAR name : NAME_ARRAY; id : ID_ARRAY; grade : GRADE_ARRAY; Fall 2001 (c)opyright Brent M. Dingle 2001
12
Initializing using parallel arrays
BEGIN { main } for i:=1 to NUM_STUDS do Begin Input name[i] Input id[i] Input grade[i] End END. Fall 2001 (c)opyright Brent M. Dingle 2001
13
Passing student data using parallel arrays
To pass the information on a student to a function or procedure we would need to send at least 3 parameters e.g. ProcMisc(name, id, grade); And hope the procedure would operate correctly on all 3 parameters using the correct indices. It would be nice if we could just send ONE parameter. Fall 2001 (c)opyright Brent M. Dingle 2001
14
Using records we would have the following
CONST NUM_STUDS = 100; TYPE NAME_TYPE = string[40]; ID_TYPE = string[9]; GRADE_TYPE = real; STUD_TYPE = RECORD name : NAME_TYPE; id : ID_TYPE; grade : GRADE_TYPE; END; STUD_ARRAY = array [1..NUM_STUDS] of STUD_TYPE; VAR stud : STUD_ARRAY; Fall 2001 (c)opyright Brent M. Dingle 2001
15
(c)opyright Brent M. Dingle 2001
Record Appearance The above declarations look similar to those used for parallel arrays, But look closely we added a bizarre line: STUD_TYPE = RECORD… And we have only ONE array (of STUD_TYPE) declared. Fall 2001 (c)opyright Brent M. Dingle 2001
16
Initializing - Using records
We could say: BEGIN { main } for i:=1 to NUM_STUDS do Begin Input stud[i].name Input stud[i].id Input stud[i].grade End; END. Or we could simplify and say: Input stud[i] Fall 2001 (c)opyright Brent M. Dingle 2001
17
Passing student data using records
To pass the information on a student to a function or procedure we would need to only send ONE parameter e.g. ProcMisc(stud); Which is much easier (and ‘safer’) than sending three. Fall 2001 (c)opyright Brent M. Dingle 2001
18
(c)opyright Brent M. Dingle 2001
Too see more examples Check out the code posted on the web to see a full working example using parallel arrays. Contrast that with the example using a record type. These example programs (or similar) will be presented in class. Fall 2001 (c)opyright Brent M. Dingle 2001
19
(c)opyright Brent M. Dingle 2001
Important Side Note Arrays are STILL said to only be of ONE data type, They are of ONE record type. The records may contain multiple types, but the array is still only of ONE record type. So if asked how many types an array can contain the correct answer is ONE. Fall 2001 (c)opyright Brent M. Dingle 2001
20
Definitions (from the book)
Know the definitions presented in the book. Some, but not necessarily all, are listed below. Fall 2001 (c)opyright Brent M. Dingle 2001
21
(c)opyright Brent M. Dingle 2001
Fields Fields, components, or component fields all refer to the same thing when talking about records. Fields are the individual components of a record. Example: In the STUD_TYPE above there are 3 fields. Fall 2001 (c)opyright Brent M. Dingle 2001
22
(c)opyright Brent M. Dingle 2001
Field Identifiers Each field has a name, often called the field identifier. Example: The 3 fields in STUD_TYPE above are: name id grade Fall 2001 (c)opyright Brent M. Dingle 2001
23
(c)opyright Brent M. Dingle 2001
Record Variable A variable declared as a record type is referred to as a record variable. Example: VAR student : STUD_TYPE; Where STUD_TYPE was declared as above. student is declared as a record type, thus student is a record variable. Fall 2001 (c)opyright Brent M. Dingle 2001
24
(c)opyright Brent M. Dingle 2001
Component Variable A variable of a component of a record is called a component variable. Example: Let the variable student be a variable of record type STUD_TYPE i.e. VAR student : STUD_TYPE; Notice, STUD_TYPE has field identifiers name, id, and grade then the following are component variables: student.name student.id student.grade Fall 2001 (c)opyright Brent M. Dingle 2001
25
Record Syntax/Structure
Record types are declared in the TYPE section as follows: TYPE [record name] = RECORD [field 1] : [type 1]; [field 2] : [type 2]; [field 3] : [type 3]; : : [field n] : [type n]; END; Fall 2001 (c)opyright Brent M. Dingle 2001
26
Records within Records
You may have a record type which contains a field that is of another record type. Example: TYPE GRADE_REC = RECORD grade : real; letter: char; END; STUD_REC = RECORD name : string[30]; grade : GRADE_REC; Fall 2001 (c)opyright Brent M. Dingle 2001
27
With statement and Variant Records
For now don’t worry about using the WITH statement (if we get to it, then we get to it, if not then not =) Regardless you will NOT need to know about variant records. Fall 2001 (c)opyright Brent M. Dingle 2001
28
(c)opyright Brent M. Dingle 2001
Suggested Problems pages 412, 413 1, 2, 4 Fall 2001 (c)opyright Brent M. Dingle 2001
29
(c)opyright Brent M. Dingle 2001
End Pascal Record Fall 2001 (c)opyright Brent M. Dingle 2001
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.