Fortran programming
Fortran programming
A Brief Introduction to
Fortran-90
for Numerical Programming
by
Pedro B. Perez
June 1999
Introduction of Fortran-90 for Numerical Programming
Abstract
This brief review of the Fortran 90 language is intended to introduce some of the
Fortran-90 features to programmers familiar with other scientific languages. This
work is by no means a complete introduction to the Fortran 90 language and will
focus only features pertinent to numerical programming. Examples and references
are provided for the reader to commence a self paced training journey. This work
is based on lecture notes from CSC112 and CSC302 at NCSU and references to
exercises are provided.
Introduction of Fortran-90 for Numerical Programming
Table of Contents
1.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.0 Source Code Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3.0 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.0 Code Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.0 Mathc Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.0 List Directed Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.0 Intrinsic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
8.0 Structured Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9.0 Function Sub-Program Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
10.0 Subroutine Sub-Program Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
11.0 Formatted Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
12.0 Structured Data Types - Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Appendices
1.0 Introduction
Fortran 90 is a standardized programming language which includes the entire Fortran 77 dialect.
The Fortran-90 highlights which are included in this monogram will be presented in the order
the author believes will impact the beginning Fortran programmer. The following topics are in
included:
No attempt is made to be inclusive of all the Fortran-90 features under the above headings. Only
an introductory level explanation with an example are provide. The reader is encouraged to read
the reference literature for a complete explanation.
The Fortran 90 compiler used at NCSU as of the date of this document is the Numerical
Algorithm Group Fortran 90 compiler. One simply enters at eos prompt “add nagf90" and then
invoke the compiler by entering at the eos prompt “f90 myprogram.f90".
Source code lines in free-form may now be 132 characters in length. A programmer may use
any column in the Fortran record for coding. The reserved column 7 through 72 for Fortran
statement is lifted as is the column 6 reservation for continuation characters.
Most Fortran-90 compilers identify the source code form from the extension of the file name.
Typically a " f 90 " extension will always be for a free-form source code. File extensions such
as " for " or " f " are likely reserved for fixed-form source code. Programmers are always
encouraged to check with the Compiler User Manual for the proper extension. However, the
following extensions are recommended since the author have used them successfully.
Introduction of Fortran-90 for Numerical Programming
Page 2
Comments
Comments may appear anywhere after an exclamation point (!) in free-form source codes. The
exclamation point terminates a Fortran record and the compiler will proceed to the next line.
Fortran-90 allows a variable or program unit name length to be up to 31 characters in length and
recognizes the underscore ( _ ) in symbolic names.
area_of_a_circle = pi * circle_radius**2
Fortran-90 allows for multiple statements to appear on the same Fortran line. A semicolon (;)
separates these statements. This feature should be used with care since it may lead to unreadable
coding.
Fortran-90 adds eight new characters to the Fortran 77 standard character set. These are listed
in Table I.
Introduction of Fortran-90 for Numerical Programming
Page 3
Table I
New Characters in Fortran-90
The Fortran language is not case sensitive. It is recommended to use small caps for source code
and comments.
Fortran-90 limits the number of continuation lines to 39. However, compiler options may extend
this limit. An ampersand ( & ) is used at the end of initial line to indicate the following line is
a continuation.
Example:
Character strings which must be continued on the next line require two "&". One at the end of
the first line and the other at the start of the continuation line:
Example:
FORTRAN-77 included five intrinsic data types. Fortran-90 supports these five and allows the
programmer to derive a new data type from these intrinsic types:
Fortran-90
Data Types
Intrinsic Derived
1 User Specified
Numeric Non-numeric
1 Integer 1 Boolean
1 Real 1 Character
1 Complex
Introduction of Fortran-90 for Numerical Programming
Page 5
Fortran requires identifiers to be declared a type. This is performed in the declaration part of the
program with type declarations REAL, INTEGER, COMPLEX, CHARACTER, and LOGICAL
followed by the list of identifiers:
The "LEN=" attribute to the type CHARACTER specifies the character string length. The double
colon (::) is a Fortran 90 feature which allows for declaring multiple attributes
to identifiers. The attributes are on the left of the double colon (::) and the identifiers are on the
right.
It is imperative to declare all variables and avoid default type (implicit) declarations. Use
IMPLICIT NONE in the declaration part of the program to enforce type declaration.
A Fortran code consists of program units of which one and only is called the PROGRAM unit.
Modular components include the Program, Subroutines, Functions, and Modules. Program units
may consist of data and algorithms. Each program unit is structured as follows:
HEADING
1 Program unit name
SPECIFICATION
1 Variable Declarations
EXECUTION
1 Algorithm
END
1 End Statement
Introduction of Fortran-90 for Numerical Programming
Page 6
Program Heading
PROGRAM name
Specification
Execution Statements
Executable statements are placed in the Execution Part of the program following the
declaration part.
Introduction of Fortran-90 for Numerical Programming
Page 7
1 END Statement
The END statement terminates execution and is an executable statement. Only one END
statement per program unit is allowed.
where program_unit is the Fortran program or subprogram and name is the name of
the program unit:
end program example
1 STOP Statement
Allows for the program execution to stop prior to terminating execution with the END
statement.
Syntax: STOP
PAUSE Routine
Example
Fortran 90
program hello_world
implicit none
write(*,*) “ Hello World of Mine “
end program hello_world
C++
#include <stdio.h>
#include <iostream.h>
main ()
{
cout << "Hello World of Mine" << endl ;
}
Assignment Operator
variable = expression
PI = 3.1416
CAREA = PI * R**2
The assignment associates a memory location with the variable. The assignment statement first
evaluates the expression (right-hand side) and then associates results with the variable.
Introduction of Fortran-90 for Numerical Programming
Page 9
Mixed-mode Assignments
A reminder that when variables of the same type are arithmetically combined, the result preserves
the type:
Avoid raising a base to a floating point value unless that is exactly what is needed and the base
will always be greater than 0.
Priority Rules
Expressions containing the arithmetic operations ( + - / * and ** ) are evaluated following the
FORTRAN priority rules which are identical to c++ with the addition of exponentiation:
Introduction of Fortran-90 for Numerical Programming
Page 10
2. All multiplications and divisions are performed next in the order in which
they appear left to right.
3. Addition and subtraction are performed last in the order in which they
Examples:
2* 3**2 = 2**9 = 18
2 + 4 * *2 / 2 = 2 + 16 / 2 = 2 + 8 = 1 0
The standard order of evaluation can be overridden using parentheses. Nested parentheses are
evaluated from the inner-most set. Parentheses must balance; that is, they must occur in pairs.
The assignment statement may be very useful in accumulating results (summation and product)
or counting. For example,
Product
1 Example
Introduction of Fortran-90 for Numerical Programming
Page 12
p ← an p = a(n) p = a(n) :
for i = n-1 to 0 step -1 do do i = n-1,0,-1 for (int = n-1; i>0; i--)
p ← ai + xp p = a(i) + x * p p = a[i]+x*p ;
end do end do
A program's ability to communicate with a user is critical. Fortran supports I/O functions in two
forms: formatted and unformatted. The simplest to use is unformatted I/O and is called list
directed.
• Input FORTRAN source code allows for input data to be received interactively or by an
input file. Simplest for is the list directed output (no format):
• Output FORTRAN source code allows for output data to be transmitted interactively or
by an input file. Simplest for is the list directed output (no format)
• (*,*)
First * is the unit number and the second * is the format specifier or label number
for the format specification
Introduction of Fortran-90 for Numerical Programming
Page 13
Intrinsic functions are available to a program by referencing the function's name. The function
returns its results for the given argument and this result may be assigned to a variable:
program get_pi
implicit none
real :: pi
pi = 4.0 * atan(1.0) ! Intrinsic Function atan (arc-tangent)
write(*,*) ' PI = ',pi
end program get_pi
It is important to note that all trigonometric intrinsic functions such as sine and
cosine use radians for angles not degrees.
Fortran programs may be structured in sequential, selective or repetition forms. Each structure
offer powerful tools for solving numerical problems. Logical expressions and variables are
essential to advanced methods of control and will be introduced in this section.
Sequential Programming
Each FORTRAN statement is executed exactly one time in the order of appearance. Simple to
code and follow-up, however, limited to relative simple programming needs.
Logical Operations
There are only two logical constants in FORTRAN and a variable typed LOGICAL may only have
one of these two logical constants:
.TRUE.
.FALSE.
Logical variables are declared in the specification part of the program unit (i.e. main program or
sub-program) just as other types:
LOGICAL :: name-list
Logical Expressions
1 Relational Operators
Symbol Meaning
Logical expressions formed by comparing real variables with .EQ. are evaluated as
.FALSE. even though the quantities are algebraically equal.
Example: Real :: X
Logical :: EQUALS
Y = X * (1.0/X)
EQUALS = (Y.EQ.1.0) results is usually .FALSE.
Compound logical expressions are formed by combining logical expressions using logical
operators.
Logical Operators
.NOT.
.AND.
.OR.
.EQV.
.NEQV.
Introduction of Fortran-90 for Numerical Programming
Page 17
P .NOT. P
.TRUE. .FALSE.
.FALSE. .TRUE.
Example:
N=4
N**2 + 1 > 10 .AND. .NOT. N< 3 .TRUE.
Example:
program decay
!
! Calculate the decay of radioactive material and provide error checks
! for input.
!
implicit none
logical :: error
real :: n,n0,lambda,t
write(*,*) “ Enter n_0,lambda, and time”
read(*,*) n0,lambda,t
error = (lambda.lt.0.0).OR.(t.lt.0.0)
if(error.eqv..true.)then
write(*,*)" Error detected in input"
stop
end if
n = n0 * exp(-lambda * t)
write(*,*)" Radioactive nuclide remaining ",n
end program decay
This structured programming approach provides two or more paths of program execution. The
control scheme is by logical expressions.
Introduction of Fortran-90 for Numerical Programming
Page 19
Examples:
The Logical expression is evaluated first. If it returns .TRUE., then the next two statements are
executed. If the logical expression returns .FALSE., then the "nested" expressions are bypassed.
The logical expression in an IF construct must be enclosed in parenthesis. The structure on the
right is given the name CHECK_X to easily identify the purpose.
Logical IF Statement
Simplified form of the IF construct (and an old popular one!) for single statement sequences.
IF ( logical-expression ) action-statement
Execution of the IF statement causes evaluation of the logical expression. If the logical expression
is .TRUE., the action statement is executed. If .FALSE. , the action statement is not executed.
IF-THEN-ELSE Constructs
IF constructs also allow for an alternative statement sequence for execution when the logical
expression is .FALSE.
Introduction of Fortran-90 for Numerical Programming
Page 20
IF-ELSE-IF Constructs
IF ( logical-expression-1 ) THEN
statement-sequence-1
ELSE IF ( logical-expression-2 ) THEN
statement-sequence-2
...
ELSE
statement-sequence-n
END IF
Example
The third Fortran control structure is the Repetition Structure which is also called Loops
Introduction of Fortran-90 for Numerical Programming
Page 21
Loop Control
DO Loops
1 Loop is executed once for each value of a pre-determined control variable range
1 The initial value, the limit, and the step size of the control variable are determined before
repetition begins
DO Loop Structure
{ statement sequence}
END DO
1 The Loop structure automatically increments or decrements the loop index variable
sum_do: do i=10,1,-1 !
sum = sum + i**2 ! Named Fortran 90 Do
end do sum_do ! construct
do while( i < 10 )
sum = sum + i ! Note: DO WHILE requires
i = i+ 1 ! incrementing control index
end do !
Introduction of Fortran-90 for Numerical Programming
Page 22
Below are Fortran-90 DO structures with an EXIT. The EXIT statement causes the process to exit
the loop and can be very useful.
do i=1,100 !
if(.not.(sum < = 100.0)) exit ! Highly desirable EXIT
sum = sum + i ! of DO construct prevents
end do ! infinite loops. Safer
! than DO WHILE
THINGS TO REMEMBER
1 Repetition structures with logic controls may easily become infinite. Make sure the
structure has a finite number of cycles
do while( i > 0) !
sum = sum + I ! Note: > will lead to
i = i+ 1 ! an infinite loop
end do !
1 INTRINSIC Functions
Compiler provided, relatively "safe" to use, careful with type of function and type of
argument.
Use of a FUNCTION in a fortran statement returns the result of the FUNCTION to the statement.
Example
Pythagoras Theorem
(B**2 + A**2)
SQRT
Assignment
1 External Functions
type
Name
It is called the "return variable" and the function must assign the results of the calculation
to this variable. This is the only time a program unit name is used in an assignment
statement.
These are called dummy or formal arguments which represent the actual arguments
(variables) when the function is referenced.
1 RETURN
Functions may have at least one RETURN statement which instructs the sub-program to
pass the calculated result to the program unit.
1 END Statement
The END statement is the last statement in every FORTRAN program unit. The END
statement terminates execution and is an executable statement. Only one END statement
per function unit is allowed
Introduction of Fortran-90 for Numerical Programming
Page 25
The external FUNCTION is referenced by the functions's NAME from the referencing program
unit
function_name ( actual-argument-list )
1 The actual-argument-list contains the actual arguments in the calling program unit.
1 The number of actual arguments must be equal to the number of dummy (or formal) of the
FUNCTION
1 Each actual argument must agree with the type of the dummy argument.
1 Corresponding pairs of actual and dummy arguments are associated with the same memory
location
CAUTION
1 Changing the values of dummy arguments within a FUNCTION program unit changes the
values of the corresponding actual arguments that are variables.
The FUNCTION dummy arguments reference the memory storage address of the
actual argument
If the FUNCTION changes the dummy argument, it will inadvertently change the
actual argument in the referencing program unit which usually leads to errors.
How does the programmer protect the actual argument? This is done by specifying
the “intent” of each dummy argument.
A dummy argument may be protected by specifying an INTENT(IN) for the dummy argument in
the procedure.
Introduction of Fortran-90 for Numerical Programming
Page 26
Example
In this example:
1 Variable name CUBE_ROOT is same as function name. Special variable called the result
variable
1 The type of the function program unit is declared BOTH in the referencing program unit
and in the function.
Introduction of Fortran-90 for Numerical Programming
Page 27
Example
program example
implicit none
!
! uses function cube_root
!
real, external :: cube_root
real :: x , a
!
1
!
a = cube_root(x)
!
1
end program example
!
real function cube_root(x)
implicit none
!
! calculates the cube root of a positive real number using logs
!
real intent(in) :: x ! protect from changing dummy
! argument
real :: log_x ! local variable to function
!
log_x = log(x)
!
cube_root = exp(log_x / 3.0) ! the result variable cube_root
!
end function cube_root
Introduction of Fortran-90 for Numerical Programming
Page 28
Example
A SUBROUTINE differs from a FUNCTION in how it is referenced and how the results (if any)
are returned. A FUNCTION is referenced simply by using its name followed by the arguments
(if any) in parenthesis. A SUBROUTINE is accessed by means of a CALL statement followed
by the name of the subroutine and the arguments (if any) in parenthesis.
Unlike a FUNCTION a SUBROUTINE does not need to return anything to the referencing
program unit. If the SUBROUTINE does return results it does so by means of one or more
arguments.
name is a legal FORTRAN identifier. The name should be distinct from all other names in the
program and should be chosen to indicate the purpose of the subroutine
Dummy-argument-list or Dummy arguments are used to "associate" values to and from the
subroutine.
Specification
This section includes non-executable statements. These provide information to be used during
compilation of the program.
• EXTERNAL, INTENT(IN)
Introduction of Fortran-90 for Numerical Programming
Page 30
Execution Statements
• Executable Statements
1 RETURN
Subroutines have at least one RETURN statement which instructs the sub-program to pass
the calculated values to the main program unit
Syntax: RETURN
1 END Statement
The END statement terminates execution and is an executable statement. Only one END
statement per subroutine unit is allowed
Syntax: END
1 The actual-argument-list contains the actual arguments in the referencing program unit.
1 The number of actual arguments must be equal to the number of dummy arguments.
1 Each actual argument must agree with in type with the dummy argument.
Introduction of Fortran-90 for Numerical Programming
Page 31
1 Corresponding pairs of actual and dummy arguments are associated with the same memory
location
1 Changing the values of dummy arguments within a sub-program changes the values of the
corresponding actual arguments that are variables.
INTENT(option)
INTENT(IN)
As with a FUNCTION variables with attribute INTENT(IN) means the variables are used to
transfer information to the FUNCTION or SUBROUTINE
INTENT(OUT)
A FUNCTION does not have any variables with this attribute since a FUNCTION does not return
"arguments"
A SUBROUTINE variable with attribute INTENT(OUT) indicates that these variables are used to
transfer information from the SUBROUTINE back to the referencing program
INTENT(INOUT)
Implies dummy argument may be used for information transmission in both directions.
Introduction of Fortran-90 for Numerical Programming
Page 32
PROGRAM SUBROUTINE_EXAMPLE
!
IMPLICIT NONE
INTEGER :: N=0
REAL :: FACTORIAL
!
WRITE(*,*)' Factorial Program, enter an integer ==> '
READ(*,*)N
!
CALL FACT(N,FACTORIAL) ! Reference SUBROUTINE using CALL
!
WRITE(*,*)' Factorial of ',N,' is ',FACTORIAL
!
STOP
END
!
!*********** subroutine fact ************
!
SUBROUTINE FACT(M,F)
!
IMPLICIT NONE
INTEGER :: J
INTEGER, INTENT(IN) :: M
REAL, INTENT(OUT) :: F
!
J=M
F = 1.0 ! Initialize here to reset value on each call
FACTORIALS: IF(J.LT.0)THEN
STOP
ELSE IF(J.EQ.0)THEN
F = 1.0
ELSE
DO WHILE(J.GT.0)
F=F*J
J=J-1
END DO
END IF FACTORIALS
!
END SUBROUTINE FACT
Introduction of Fortran-90 for Numerical Programming
Page 33
Fortran formatted I/O with the READ and WRITE statements allows for programmer specified
formats and file access.
1 (*,*)
• UNIT = * (first *)
The * implies default I/O unit number - Typically the CRT monitor
Formatted Output
The format specifier specifies format for displaying expressions in the output list:
1 An asterisk (*)
Examples:
PRINT*, NUMBER,TEMP
20 FORMAT(TR1,I5,F8.2)
WRITE(*,*) NUMBER,TEMP
WRITE(*,20) NUMBER,TEMP
20 FORMAT(TR1,I5,F8.2)
Format Descriptors
Format descriptors (TR1, I5, and F8.2) specify the format in which values of variables in the
output-list are displayed.
1 7 1 0 . 2 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Introduction of Fortran-90 for Numerical Programming
Page 35
rEw.d & rDw.d Real Data in Engineering Notion (single and double precision)
Tc Tab Descriptors
/ Vertical spacing
S Sign descriptors
BN Blanks
1 Formatted READ is often needed for reading the formatted output of another code
Introduction of Fortran-90 for Numerical Programming
Page 36
A collection of data values can be efficiently processed using structured data type such as arrays.
Arrays are very useful for processing a list of numerical, character, or logical variables and for
numerical linear algebra.
1-D Arrays
A list of values stored in memory allow for fast and efficient data retrieval. The list may be
organized in memory using an ARRAY data type which groups a fixed number of data items of
the same data type in a sequence. Each data item in an array can be directly accessed from
memory by specifying the position in the sequence.
Example:
REAL VALUE(5)
READ(*,*) VALUE(1), VALUE(2),..., VALUE(5)
VALUE(1)
VALUE(2)
VALUE(3)
VALUE(4)
VALUE(5)
The name and the subscript range of each one-dimensional array may be declared in two ways:
1 DIMENSION array-name(l:u)
Fortran-90 enhances the classic array declaration with help of the double colon ( :: ) operator.
REAL, DIMENSION(10) :: A
REAL :: A
DIMENSION A(100)
and
INTEGER :: LIMIT
PARAMETER (LIMIT=100)
REAL :: A(LIMIT)
These methods of declaring the array type and size require the number of elements to be know prior
to run time
Fortran 90 allows the size or dimension of the array to be specified at run-time as:
This is a Fortran-90 declaration for an array. The size is determined during run time. These are
called ALLOCATABLE arrays since the size is ALLOCATED at run time.
Once the array dimension is know during program execution the size is allocated as follows:
ALLOCATE (NUMBERS(N))
type :: array_name(extent)
real :: a(0:10) declares an array “a” with an extent of 11 units of type real storage.
Introduction of Fortran-90 for Numerical Programming
Page 38
Subscripted Variables
Each individual element of an array is uniquely identified and accessed by means of a subscripted
variable
array_variable-name(subscript-identifier)
1 VALUE(1) 1 VALUE(i)
VALUE(1)
VALUE(2)
VALUE(3)
VALUE(4)
VALUE(5)
IMPLIED DO LOOP
!
READ(*,*) (AXIS(I), I=1,LIMIT) ! <==== NICE
!
Introduction of Fortran-90 for Numerical Programming
Page 39
program allocatable_array !
!
! Introduce the concept of allocatable storage for a
! 1-D array for storing a set of observables and calculating
! the average value of the observables.
!
real, allocatable :: numbers(:) !
real sum, avg !
integer n !
!
sum = 0.0 ; avg = 0.0 !
!
write(*,"(a)", advance ="no")' How many observables in the set? '!
read(*,*) n !
!
allocate (numbers(n)) !
!
write(*,"(a)",advance="yes")' Enter the numerical values: '
do i=1,n !
read(*,*) numbers(i) !
sum = sum + numbers(i) !
end do !
!
avg = sum/n
!
write(*,*)' Avg = ', avg
!
end program allocatable_array
Introduction of Fortran-90 for Numerical Programming
Page 40
program array_demo
!
implicit none
!
real, allocatable :: elements(:)
real :: sum,average
integer :: i,j,ii
!
write(*,*)' Enter the array''s starting and ending points '
read(*,*) i,j
!
allocate(elements(i:j))
!
write(*,*)' Enter the values for elements '
read(*,*) (elements(ii), ii = i,j)
!
do ii=i,j,1
sum = sum + elements(ii)
end do
!
average = sum/(abs(i)+abs(j)+1.0)
!
write(*,'(//)')
write(*,*)' The following values have been entered for elements'
write(*,'(//)')
!
do ii=i,j,1
write(*,10) ii,elements(ii)
end do
write(*,'(//A,f8.3)')' The average is ',average
!
10 format(tr2,'Element ',i2,' is ',f8.4)
!
end
Introduction of Fortran-90 for Numerical Programming
Page 41
Multidimensional Arrays
Arrays are FORTRAN's version of "structured" data types. Fortran-90 allows up-to seven (7)
dimensions which refered to as a rank seven array. The shape of the array is determined from the
rank and extent.
The name and the subscript range of each two-dimensional array may be declared in two ways:
The second and prefered way to declare an array is to subscript the variable when declaring the type
REAL :: FLUX(1:2000,1:1000)
REAL :: FLUX(2000,1000)
1 The range or "dimension" of the array must be an integer value or integer parameter
1 Memory locations for the data type items are "reserved" according to the dimension of the
array
REAL,DIMENSION :: A(100,100)
1 FORTRAN-77 does not allow for dynamic memory allocation for arrays, but FORTRAN-90
does.
Subscripted Variables
Each individual element of a 2-D array is uniquely identified and accessed by means of a
subscripted variable:
VALUE( 1, 2 ) VALUE( i, j )
VALUE ( I , J) refers to array element (and contents) in the I th row and J th column
Each element of the 2-D array can be accessed directly by using a two-subscripted variable
consisting of the array name and the desired element subscript
INTEGER IMORN ( 2 , 3 )
11 12 13
21 22 23
IMORN(1,3) = 13 IMORN(2,3) = 23
Two-Dimensional arrays suggests two natural orders for processing the data entries:
1 Row wise
1 Column wise
The Fortran default processing order is column wise which is different from c/c++ which uses row
wise processing.
Introduction of Fortran-90 for Numerical Programming
Page 43
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
Column-wise processing:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
The programmer can select the processing order by controlling the use and
order of subscripts
The FORTRAN default convention is that 2-D arrays will be processed column-
wise
The first subscript is varied first and the second subscript varies
second
The second subscript is varied first and the first subscript varies
second
Introduction of Fortran-90 for Numerical Programming
Page 45
to obtain:
Use:
The second subscript is varied first and the first subscript varies
second
REAL AXIS(3,3)
!
DO I=1,3
DO 20 J=1,3
READ(*,*) AXIS(I,J)
END DO
END DO
The first subscript is varied first and the second subscript varies
second
REAL :: AXIS(3,3)
DO J=1,3
DO I=1,3
READ(*,*) AXIS(I,J)
END DO
END DO
When J = 3 (Column 3)
DO I = 1, 3
READ(*,*) AXIS(I,3)
END DO
1 It must be noticed that the READ statement is encountered nine (9) times in the nested do
loop structure above.
1 This means the data values must also be entered on nine separate lines.
1 If the data is in a file, the file structure for the column processing is, for example:
33.0
11.0
23.0
41.0
12.3
14.0
1.2
98.0
21.0
NOTE:
The READ statement is only encountered once per loop and all the data
for that loop can be on the same line
Introduction of Fortran-90 for Numerical Programming
Page 49
Important to Remember
The FORTRAN default convention is that 2-D arrays will be processed column-
wise
The second subscript is varied first and the first subscript varies
second
The first subscript is varied first and the second subscript varies
second
Introduction of Fortran-90 for Numerical Programming
Page 51
Appendix A
A third party Fortran-90 compiler was installed on the eos computing environment in early 1996.
Since this is not a native compiler to the workstation, the software most be added to the session:
Appendix B
Fortran-90 allows for user defined data types that are derived from intrinsic data types providing
considerable flexibility in defining data structures.
The programmer defines a derived data type using the new Fortran-90 type statement in the
declaration part of the program. The general form is as follows:
TYPE :: derived_name
intrinsic type declaration :: symbolic_name
{ ... :: ... }
END TYPE derived_name
A variable is declared a derived data type by using the type statement as follows in the declaration
part of the program unit.
TYPE(derived_name) :: variable_name
The variable has been declared type derived_name and is composed of a number of intrinsic data
types. It is important to notice the sequential order of the intrinsic data types and to use the %
symbol to separate these intrinsic components. The following example demonstrate initializing
variable:
Example
type :: periodic_table !
character(len=12) :: element_name !
real :: atomic_mass !
integer :: atomic_number !
logical :: fissile !
end type periodic_table !
Introduction of Fortran-90 for Numerical Programming
Page 53
Appendix C
SUBROUTINE F90_PAUSE !
!
! subroutine to pause program; requiring user to re-initiate !
execution
!
IMPLICIT NONE !
CHARACTER :: PAUSE !
!
WRITE(*,'(A)', ADVANCE = 'NO') 'Press < ENTER > to continue ==>' !
READ(*,'(A)') PAUSE !
!
END SUBROUTINE F90_PAUSE !