Derived Data Types and Structures Introduction Derived types















































- Slides: 47

Derived Data Types and Structures

Introduction • Derived types are ones that the programmer manufactures. • They are alternatives to the standard (intrinsic) data types – integer – real – character – logical – complex

Derived types • A derived type is invented by the programmer. • They make use of intrinsic types, or be based on other derived types. • Derived types are created by grouping data into ‘structures’. • Each data item in the group is called a component

Why do we need them? • Derived types allow us to group related items together, regardless of what data type they are. • Example: write a derived type to store the numerator and denominator of a fraction. – Both are INTEGER – Both are related

Fraction example INTEGER : : num, denom num denom TYPE fraction INTEGER : : num, denom END TYPE fraction num denom Num and denom are called the ‘components’ of TYPE fraction.

Structure declarations TYPE fraction INTEGER : : num, denom END TYPE fraction TYPE(fraction) : : proportion num denom

Array declarations TYPE fraction INTEGER : : num, denom END TYPE fraction TYPE(fraction), DIMENSION(50) : : proportion or TYPE(fraction) : : proportion(50)

Array declarations TYPE fraction INTEGER : : num, denom END TYPE fraction TYPE(fraction), PARAMETER : : proportion(9, 5) establishes a constant called proportion like this: proportion num 9 denom 5

Array declarations A common use of TYPEs is to establish the definition of records of data contained in a file. Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Array declarations TYPE Student. Record CHARACTER(10) : : lname CHARACTER(10) : : fname INTEGER : : id INTEGER : : age REAL : : GPA END TYPE Student. Record Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Array declarations TYPE Student. Record CHARACTER(10) : : lname CHARACTER(10) : : fname INTEGER : : id INTEGER : : age REAL : : GPA END TYPE Student. Record

Student. Record REMEMBER: a TYPE definition and declaration give us the framework for a Student. Record. Only constructors actually create them. lname fname id age GPA

Array declarations TYPE Student. Record CHARACTER(10) : : lname CHARACTER(10) : : fname INTEGER : : id INTEGER : : age REAL : : GPA END TYPE Student. Record declaration constructor TYPE(Student. Record) : : student = Student. Record(“Duck”, “Don”, 100, 50, 2. 00)

Student. Record Student has now been constructed. lname Duck fname Don id 100 age 50 GPA 2. 00

Input and output • To get data into structures we must be able to refer to a structure AND a component. • We also must refer to the structure and component in order to print it. • The ‘component selector’ operator in FORTRAN is %

Direct Assignment Instead of the following constructor student = Student. Record(“Duck”, “Don”, 100, 50, 2. 00) Individual components can be assigned individually student%lname = “Duck” student%fname = “Don” student%id = 100 student%age = 50 student%GPA = 2. 00

Direct Assignment Individual components can also be read from a file. READ (10, 20) student%lname, student%fname, & student%id, student%age, student%GPA 20 FORMAT(a 10, i 5, 1 x, i 3, 1 x, f 4. 2)

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy Smith 1012 1013 1014 1010 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 Smith Suzanne 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 Smith Suzanne 1012 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 Smith Suzanne 1012 21 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 Smith Suzanne 1012 21 3. 32 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Data file example Smith Jones Anderson Johnson etc. Suzanne William James Nancy 1012 1013 1014 1010 Smith Suzanne 1012 21 3. 32 21 20 19 21 3. 32 3. 04 2. 74 2. 50

Arrays of structures • One of the biggest limitations of arrays was that they only hold one type of data. • Thus, for reading data from files (as in the last example) we would need 5 parallel arrays. • This meant that sorting necessitated swapping corresponding elements of parallel arrays.

Parallel arrays lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Array of records Smith Jones Anderson Johnson etc Suzanne William James Nancy etc 1012 etc 21 20 19 21 etc 3. 32 3. 04 2. 74 2. 50 etc

Swap first elements of parallel arrays temp Smith lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Swap first elements of parallel arrays temp Smith Suzanne lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Swap first elements of parallel arrays temp Smith Suzanne 1012 lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Swap first elements of parallel arrays temp Smith Suzanne 1012 21 lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Swap first elements of parallel arrays temp Smith Suzanne 1012 21 3. 32 lname Smith Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Copy values up temp Smith Suzanne 1012 21 3. 32 lname Jones Anderson Johnson etc. fname Suzanne William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Copy values up temp Smith Suzanne 1012 21 3. 32 lname Jones Anderson Johnson etc. fname William James Nancy etc. id 1012 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Copy values up temp Smith Suzanne 1012 21 3. 32 lname Jones Anderson Johnson etc. fname William James Nancy etc. id 1013 1014 1010 etc. age GPA 21 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Copy values up temp Smith Suzanne 1012 21 3. 32 lname Jones Anderson Johnson etc. fname William James Nancy etc. id 1013 1014 1010 etc. age GPA 20 20 19 21 etc. 3. 32 3. 04 2. 74 2. 50 etc.

Copy values up temp Smith Suzanne 1012 21 3. 32 lname Jones Anderson Johnson etc. fname William James Nancy etc. id 1013 1014 1010 etc. age GPA 20 20 19 21 etc. 3. 04 2. 74 2. 50 etc.

Copy data back temp Smith Suzanne 1012 21 3. 32 lname Jones Smith Anderson Johnson etc. fname William James Nancy etc. id 1013 1014 1010 etc. age GPA 20 20 19 21 etc. 3. 04 2. 74 2. 50 etc.

Copy data back temp Smith Suzanne 1012 21 3. 32 lname Jones Smith Anderson Johnson etc. fname William Suzanne James Nancy etc. id 1013 1014 1010 etc. age GPA 20 20 19 21 etc. 3. 04 2. 74 2. 50 etc.

Copy data back temp Smith Suzanne 1012 21 3. 32 lname Jones Smith Anderson Johnson etc. fname William Suzanne James Nancy etc. id 1013 1014 1010 etc. age GPA 20 20 19 21 etc. 3. 04 2. 74 2. 50 etc.

Copy data back temp Smith Suzanne 1012 21 3. 32 lname Jones Smith Anderson Johnson etc. fname William Suzanne James Nancy etc. id 1013 1012 1014 1010 etc. age GPA 20 21 19 21 etc. 3. 04 2. 74 2. 50 etc.

Copy data back temp Smith Suzanne 1012 21 3. 32 lname Jones Smith Anderson Johnson etc. fname William Suzanne James Nancy etc. id 1013 1012 1014 1010 etc. age GPA 20 21 19 21 etc. 3. 04 3. 32 2. 74 2. 50 etc.

That was very laborious! lname Jones Smith Anderson Johnson etc. fname William Suzanne James Nancy etc. id 1013 1012 1014 1010 etc. age GPA 20 21 19 21 etc. 3. 04 3. 32 2. 74 2. 50 etc.

The same process with array of structures temp Smith Jones Anderson Johnson etc Suzanne William James Nancy etc 1012 etc 21 20 19 21 etc 3. 32 3. 04 2. 74 2. 50 etc

The same process with array of structures temp Smith Suzanne Smith Jones Anderson Johnson etc 1012 21 3. 32 Suzanne William James Nancy etc 1012 etc 21 20 19 21 etc 3. 32 3. 04 2. 74 2. 50 etc

The same process with array of structures temp Smith Suzanne Jones Anderson Johnson etc 1012 21 3. 32 William James Nancy etc 1012 etc 20 20 19 21 etc 3. 04 2. 74 2. 50 etc

The same process with array of structures temp Smith Suzanne Jones Smith Anderson Johnson etc 1012 21 3. 32 William Suzanne James Nancy etc 1012 etc 20 21 19 21 etc 3. 04 3. 32 2. 74 2. 50 etc

Done! All of the data in each record is swapped in only three steps. Jones Smith Anderson Johnson etc William Suzanne James Nancy etc 1012 etc 20 21 19 21 etc 3. 04 3. 32 2. 74 2. 50 etc
Fundamental Derived Positions Fundamental derived Positions Fundamental derived
C Data Types Primary data types Derived data
Data Structures Systems Programming Data Structures Data Structures
Data Types and Data Structures Data Types Keywords
Introduction to Data Structures Data Structures A data
Derived Types C allows a number of derived
Data Structures and Algorithms Introduction to Data Structures
Data Structures and Algorithms Introduction Data Structures n
INTRODUCTION Data Structures Why study data structures and
Data Structures Fundamental Data Storage Data Structures For
Linked Data Structures Linked data structures versatile data
Introduction The Need for Data Structures Data structures
Introduction to Data Structures Data Structures n A
Data Structures and Algorithms Data Structures and Algorithms
Structures Structured Data types Data abstraction structs Data
Data Structures Introduction GC 211 1 Data Types
Data Structures and Algorithm Design Review Data Structures
Data Structures and Algorithm Design Review Data Structures
Data with Data Structures with Structures Java and
Derived Rules Definition of a Derived Rule for
Derived Variables A derived variable v is simply
Derived position Prepared by Sajida Mazhar Position derived
class Derived Base Derived Base Discount Sale Sale
2 2 Notes Derived Units Density Derived units
MPI derived datatypes Edgar Gabriel Derived Datatypes Basic
derived types The type definition Enumerated types Accessing
Control Structures Control Structures Control structures are used
Control Structures Control structures z Control structures are
Lewis Structures Chapter 8 Lewis Structures Lewis structures
17 Structures Simple Structures Structure Arrays Structures with
Control Structures Control Structures Control structures are used
Lewis Structures Lewis Structures Lewis structures are representations
Structures Systems Programming Structures Typedef Declarations Using Structures
Complex Structures Nested Structures Self referential structures A
Structures Structures 1 u Structures are Cs way
Structures Structures 1 u Structures are Cs way
Structures Structures 1 u Structures are Cs way
Data Structures Algorithms In this course Data Structures
Data Structures in Java 120301 Elementary Data Structures
Data structures collections Week 9 Data structures informally
Data Structures LECTURE 6 Dynamic data structures Motivation
Data Structures Algorithms Lecture 2 Data Structures Algorithms
Data Structures Algorithms Lecture 6 Elementary Data Structures
Linked List Categories of data structures Data structures
Data Structures Get your data structures correct first