
Example
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 *,experiment2PRINT * PRINT *," The reaction times for experiment 3 are: " PRINT *, experiment3 PRINT *," The average reaction time for experiment 3 is ",ave3 STOP END PROGRAM chemistry
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
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
A constant value of a derived type takes the form
The statement
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:
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
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.
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
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.
