Derived Data Types and Structures Introduction Derived types

  • Slides: 47
Download presentation
Derived Data Types and Structures

Derived Data Types and Structures

Introduction • Derived types are ones that the programmer manufactures. • They are alternatives

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

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

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 : :

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) :

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)

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

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

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

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

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 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

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

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

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,

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)

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

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

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

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

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

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

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

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.

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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