1 / 29

Pascal Records

Pascal Records. Brent M. Dingle Texas A&M University Chapter 11 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan). Record Types - Motivation. So far our only structured data type has been arrays.

ellamae
Download Presentation

Pascal Records

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pascal Records Brent M. DingleTexas A&M University Chapter 11 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan) (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? (c)opyright Brent M. Dingle 2001

  3. 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 (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; (c)opyright Brent M. Dingle 2001

  5. 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. (c)opyright Brent M. Dingle 2001

  6. 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. (c)opyright Brent M. Dingle 2001

  7. Simple student info • Let’s reduce the student information to be just: • Student Name • Student ID • Student Grade (c)opyright Brent M. Dingle 2001

  8. 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. (c)opyright Brent M. Dingle 2001

  9. Using recordsto 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. (c)opyright Brent M. Dingle 2001

  10. 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 (c)opyright Brent M. Dingle 2001

  11. Using parallel arrayswe 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; (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. (c)opyright Brent M. Dingle 2001

  13. Passing student datausing 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. (c)opyright Brent M. Dingle 2001

  14. Using recordswe 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; (c)opyright Brent M. Dingle 2001

  15. 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. (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: BEGIN { main } for i:=1 to NUM_STUDS do Input stud[i] END. (c)opyright Brent M. Dingle 2001

  17. Passing student datausing 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. (c)opyright Brent M. Dingle 2001

  18. 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. (c)opyright Brent M. Dingle 2001

  19. 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. (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. (c)opyright Brent M. Dingle 2001

  21. 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. (c)opyright Brent M. Dingle 2001

  22. Field Identifiers • Each field has a name, often called the field identifier. • Example:The 3 fields in STUD_TYPE above are: • name • id • grade (c)opyright Brent M. Dingle 2001

  23. 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. (c)opyright Brent M. Dingle 2001

  24. 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 identifiersname, id, and grade then the following are component variables: • student.name • student.id • student.grade (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; (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; END; (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. (c)opyright Brent M. Dingle 2001

  28. Suggested Problems • pages 412, 413 • 1, 2, 4 (c)opyright Brent M. Dingle 2001

  29. End Pascal Record (c)opyright Brent M. Dingle 2001

More Related