(c)opyright Brent M. Dingle 2001

Slides:



Advertisements
Similar presentations
Homework quiz Simplify Completely: Test Friday 3/2 Simplifying expressions –Exponent properties (4.1, 4.2) Know how to apply the specific properties.
Advertisements

Today’s quiz on 8.2 A Graphing Worksheet 1 will be given at the end of class. You will have 12 minutes to complete this quiz, which will consist of one.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Inequalities and their Graphs Objective: To write and graph simple inequalities with one variable.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Structs Structured Data Objects (Structs) Chapter 7.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Further Modularization, Cohesion, and Coupling. Simple Program Design, Fourth Edition Chapter 9 2 Objectives In this chapter you will be able to: Further.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Precedence Operators Error Types
Chapter 2 – part b Brent M. Dingle Texas A&M University
Unit 2 Technology Systems
Definition of the Programming Language CPRL
Parallel Arrays? ANSI-C.
Pascal Programming Arrays and String Type.
SurveyDIG 2.1 Tutorial.
RECORDS Introduction Declaring a record Using records
CSIS 115 Database Design and Applications for Business
The Data Types and Data Structures
Web Design II Flat Rock Community Schools
Computer Programming BCT 1113
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Complex data types Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings,
Loop Structures.
Exam 3 Review.
CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002
Intro to PHP & Variables
A solution to a list of records
Programming – Touch Sensors
8.1 – 8.3 Solving Proportions, Geometric Mean, Scale Factor of Perimeter and Area Since these sections are all very short, we are combining them into one.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Structures Lesson xx In this module, we’ll introduce you to structures.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Learning to Program in Python
Subroutines Web Programming.
CS212 Data Structures 2018 Second Semester.
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Programming Logic and Design Fourth Edition, Comprehensive
How to manage Lodge dues billing and tracking of Lodge dues payments
Beginning C for Engineers
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Computer Science 1 1/19/2011 On the record (paper) write the following
Chapter 4 Company Code Global Parameters
CS 2 Records 2/22/2018.
Developing a Program.
Simple Branches and Loops
Chapter 2 – part a Brent M. Dingle Texas A&M University
CS2013 Lecture 7 John Hurley Cal State LA.
Welcome Back CS 2 2/4/2013 On the record (paper) write the following
(c)opyright Brent M. Dingle 2001
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Complex Array Structures
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Reasoning with Types.
Presentation transcript:

(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

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

(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

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

(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

(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

(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

(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

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

(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

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

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

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

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

(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

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

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

(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

(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

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

(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

(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

(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

(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

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

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

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

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

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