7.1.3 Derived Types

Example

Program Description
A chemist makes four measurements of the rates of three different reaction times in an experiment. This program defines a derived type named reaction_times that stores the reaction time data. It then calculates the average reaction time of each experiment.

Mathematical Equation
The average of each of the four reaction times for each of the three experiments is calculated by the equation

average = (rt1 + rt2 + rt3 + rt4 )/4

where rt1, rt2, rt3, rt4 are the reaction times.

Input/Output Description
This program uses a derived type to define and store 4 reaction times for three experiments. The output is the average of the reaction times for each of three experiments.

Algorithm
1. Define derived type for experiment data.
2. Assign reaction time values to each of three experiments.
3. Calculate the average times
4. Display the reaction times for the three experiments.
Code

PROGRAM  chemistry
IMPLICIT none

! This program uses a derived type to store 3 sets of 4 reaction times.

! Define types
	TYPE reaction_times
	   real ::t1, t2, t3, t4
	END TYPE reaction_times

! Define variables
	TYPE(reaction_times) ::experiment1, experiment2, experiment3
	REAL :: ave1, ave2, ave3

! Structure constructors for experiment1, experiment2, experiment3
	experiment1 = reaction_times(25.78,23.56,23.4,31.67)
	experiment2 = reaction_times(78.66,72.12,75.33,67.33)
	experiment3 = reaction_times(12.11,13.33,12.99,14.01)

! Calculate the average times
	ave1 = (experiment1%t1+experiment1%t2+experiment1%t3+experiment1%t4)/4
	ave2 = (experiment2%t1+experiment2%t2+experiment2%t3+experiment2%t4)/4
	ave3 = (experiment3%t1+experiment3%t3+experiment3%t3+experiment3%t4)/4

! Print the results
	PRINT *, "The reaction times for experiment 1 are:"
	PRINT *,experiment1
	PRINT *," The average reaction time for experiment 1 is "
	PRINT *, ave1
	PRINT *
	PRINT *," The reaction times for experiment 2 are: "
	PRINT *,experiment2 

PRINT * PRINT *," The reaction times for experiment 3 are: " PRINT *, experiment3 PRINT *," The average reaction time for experiment 3 is ",ave3 STOP END PROGRAM chemistry

Output
 The reaction times for experiment 1 are:
  25.7800007  23.5599995  23.3999996  31.6700001
  The average reaction time for experiment 1 is
  26.1024990

  The reaction times for experiment 2 are:
  78.6600037  72.1200027  75.3300018  67.3300018
  The average reaction time for experiment 2 is   73.3600006

  The reaction times for experiment 3 are:
  12.1099997  13.3299999  12.9899998  14.0100002
  The average reaction time for experiment 3 is   13.0249996

Fortran 90 allows the programmer to create data types that are composed of one or more of the intrinsic types or previously defined derived types. Such derived types take the general form

TYPE new_type
component _definition _1
component _definition _2
.
.
.
END TYPE new_type

The keywords TYPE and END TYPE mark the beginning and end of the type definition. There may be as many component definition as needed each taking the same form as a variable declaration. After a derived type has been defined, it can be used to type variables by enclosing the type name in parenthesis and preceding it with the type name.

The derived type reaction_type is composed of three integer values - t1, t2, t3, t4 - that contain the time values of the reaction times of the experiment. The variable reaction_type references the values representing each of the four reaction times.

Example

In a program that processes student records, each student is identified by their name (first name, middle initial, and last name), student identification number, and current grade point average. A derived data type called student could be defined to contain all the needed information. The following type definition defines the type student as having a CHARACER component named first_name, middle_initial, and last_name, an INTEGER component named id_num, and a REAL named gpa. The variable declaration declares tony and jennifer to be student types.

TYPE DEFINITION
TYPE student
CHARACTER(LEN =20) :: first_name, middle_initial * 1, last_name
INTEGER :: id_num
REAL :: gpa
END TYPE student

VARIABLE DEFINITION
TYPE(student) :: tony, jennifer

A constant value of a derived type takes the form

tony = student("Tony","M","Barela", 456345, 3.88)
in which the sequence of constants corresponds to the components of the derived type in type and order, is enclosed in parenthesis, and preceded by the type name. (This form of defining a constant value for a derived type is called a structure constructor.) Similarly, a READ statement will expect a sequence of data values and a PRINT statement will output a sequence of data values that correspond to the values of the derived type.

The statement

jennifer%last_name = meglio

references the last_name component of TYPE(student) :: jennifer directly. The % points to the component of the derived type. In this statement, the value meglio is assigned to the last_name component of the variable jennifer which is declared to be a student type. This type of reference allows individual components of the derived type to be processed as needed.

A previously defined type may be used as a component of another derived type:

TYPE class_member
TYPE(student) :: member
CHARACTER(LEN = 1) :: gender
INTEGER :: rank
END TYPE class_member

In the derived type class_member, memberis a variable that is the TYPE student which contains the components first_name, middle_initial, last_name, id_num, and gpa. The TYPE class_member also contains a CHARACTER type (gender) and an INTEGER type (rank).

Example - Coefficients of an Equation Representing a Line

Program Description
This program uses two derived types, line and point, to calculate the coefficients of an equation representing a line. The equation representing the line is:

ax + by + c = 0 

Therefore, the line is defined as having three coefficients:

  a = y2 - y1

  b = x1 - x2

  c = x2 y1 - x1 y2

where (x1 , y1 ) and (x2 , y2 ) are the cartesian coordinates for two points through which the line passes.

Input/Output Description
The user inputs the cartesian coordinates for the two points.

The output is the value for a, b, and c where ax + by + c =0.

Algorithm
1. Define derived type for the points.
2. Define derived type for the line.
3. Prompt for and get the coordinates of the two points
4. Calculate the coefficients of equation representing the line
5. Display the values of a, b, and c where ax + by + c =0.

Code
PROGRAM line_pt
IMPLICIT NONE

! This program uses two derived types: one that defines a point and one 
! that defines the coefficients of the equation ax + by + c = 0 that defines 
! a line.
 
! Type definitions
        TYPE point
                REAL :: x,y             ! Cartesian coordinates of the point.
        END TYPE point

        TYPE line
                REAL :: a, b, c         !Coefficients that define the line.
        END TYPE line

! Variable declarations
        TYPE(point) :: point1, point2
        TYPE(line) ::  point1_to_point2

! Read data
        PRINT *, "Enter the co-ordinates of the first point"
        READ *, point1
        PRINT *,"Enter the co-ordinates of the second point"
        READ *, point2

! Calculate the coefficients of equation representing the line
        point1_to_point2%a = point2%y - point1%y
        point1_to_point2%b = point1%x - point2%x
        point1_to_point2%c = point1%y * point2%x - point2%y * point1%x

! Print results
        PRINT *,"The equation of the line joining the two points is"
        PRINT *,"ax + by + c = 0"
        PRINT *,"Where a = ", point1_to_point2%a
        PRINT *,"           b = ", point1_to_point2%b
        PRINT *,"           c = ", point1_to_point2%c
 
        STOP
        END PROGRAM line_pt

Output
Enter the co-ordinates of the first point
4
5
 Enter the co-ordinates of the second point
7
9
 The equation of the line joining the two points is
 ax + by + c = 0
 Where a =    4.0000000
            b =   -3.0000000
            c =   -1.0000000

EXERCISES

1. Following an earthquake, seismic measurements that have been recorded from centers around the world are collected and printed. Define a derived type named quake_data that consists of the longitude and latitude of the recording instrument and the strength of the quake measured on the Richter scale. Define three variables to be quake_data types. Write a Fortran statement that calculates the average strength of the quake.

2. Write a program that calculates the sum and difference of two complex numbers.

Problem Statement
Complex numbers are used by engineers and mathematicians. They consists of two parts - a real part and an imaginary part. A complex number is defined as x + jy, where x is the real part, y is the imaginary part, and j represents the square root of -1. Complex numbers are typically written in the form (x,y). Write a program that defines a derived type named complex_number that consists of a real part and an imaginary part and then declare four variables of type complex_number. Calculate the sum and difference of the two imaginary numbers.

Input/output Description
The user is prompted for and enters two complex numbers.
The output is the sum and the difference of the two complex numbers.

Algorithm
1. Define a data type for complex numbers.
2. Declare four variables to be complex number types.
3. Calculate the sum and difference of the two complex numbers.
4. Print the sum and difference of the two complex numbers.