FORTRAN 90

advertisement
FORTRAN 90
Lecturer: Rafel Hekmat Hameed
University of Babylon
Subject: Fortran 90
College of Engineering
Year: Second B.Sc.
Mechanical Engineering Dep.
ARRAYS
Terminology
Rank = Number of dimensions
Extent = Number of elements in a dimension
Shape = Vector of extents
Size = Product of extents
Conformance = same shape
Consider the following array:
– ”ƒ͖
– shape ȋȀ͖ǡ͘ /)
- ‡š–‡–•͖ƒ†͘
- size ͜
Declaring an Array
For example, suppose we have the following array declarations:
REAL, DIMENSION(-1:1) :: a, Sum
INTEGER, DIMENSION(100) :: InputData
INTEGER, DIMENSION(8) :: x,y
CHARACTER(len=10) :: names(25)



The elements of arrays a and Sum are REALs and the indices are in the
range of -1 and 1.
The elements of array InputData are INTEGERs and the indices are 100.
Both x, and y have 8 elements.
ϭ

declares an array which has 25 elements with each element having a character string of
size 10.
The integers in an extent can be PARAMETERs:
INTEGER, PARAMETER ::x = 5
INTEGER, PARAMETER :: y = 10
REAL, DIMENSION(x,y) :: Score, Mark
Array Input/Output
The easiest way of reading data into an array could be the following:
INTEGER, DIMENSION(1:10) :: x
INTEGER
:: n, i
READ(*,*) n
DO i = 1, n
READ(*,*) x(i)
END DO
The implied DO can simplify this greatly. Consider the following example:
INTEGER, DIMENSION(1:10) :: x
INTEGER
:: n, i
READ(*,*) n
READ(*,*) (x(i), i=1, n)
INTEGER, DIMENSION(3,5) :: a
INTEGER
:: I,j
READ(*,*) ((a(I,j), j=1, 5),i=1,3)
INTEGER, DIMENSION(2:4,0:1) :: a
WRITE(*,*) ((a(i,j),j=0,1),i=2,4)
INTEGER, DIMENSION(2:4,0:1) :: a
DO i = 2, 4
READ(*,*) (a(i,j),j=0,1)
END DO
Initializing arrays
This is to be used for 1-dimensional arrays that need to be assigned with
various values
INTEGER :: a(6)=(/1,2,3,6,7,8/)
Ϯ
DATA statement
Use the DATA when other methods are tedious and/or impossible. For
example for more than one array initialization or for array section initialization.
INTEGER :: a(4), b(2,2), c(10)
DATA a/4,3,2,1/
DATA a/4*0/
DATA b(1,:)/0,0/ DATA b(2,:)/1,1/
DATA (c(i),i=1,10,2)/5*1/ DATA (c(i),i=2,10,2)/5*2/
Arrays Operations
For example, if a and b are both 2x3 arrays
the result of addition is
and of multiplication is
PROGRAM XW
IMPLICIT NONE
INTEGER, DIMENSION(2,3) :: A, B, C,D,E
INTEGER::I,J
DATA A/3, 5,4,6,8,6
DATA B/5,3,2,3,1,1
C=A+B
D=A-B
E=A*B
WRITE(*,3) ((c(i,J),J=1,3),I=1,2)
3 FORMAT(3(2x,I5))
WRITEˬ
WRITE(*,3) ((D(i,J),J=1,3),I=1,2
WRITEˬ
WRITE(*,3) ((E(i,J),J=1,3),I=1,2
END
If one of the operands is a scalar, then the scalar is broadcast into an array
which is conformable with the other operand. Thus, the result of adding 5 to b is
ϯ
Example
Find the larger element of the corresponding elements of two arrays and
store it into a third one
PROGRAM XX
IMPLICIT NONE
integer, PARAMETER:: LENGTH = 5
integer, DIMENSION(1:LENGTH) :: A, B, C
INTEGER :: I
DATA A/3, 8,29,100,9
DATA B/54,87,24,108,5
DO I = 1, LENGTH
IF (A(I) > B(I)) THEN
C(I) = A(I)
ELSE
C(I) = B(I)
END IF
END DO
WRITE(*,3)(C(i),i=1,LENGTH)
3FORMAT(5(2x,I5))
END
Arrays Multiplication
In the multiplication matrices must be the number of columns in the first
matrix equals the number of rows in the second matrix.
A(N,L)  B(L,M) = C(N,M)
A(3,2)  B(2,4) = C(3,4)
ϰ
ͳ ͷ
ʹ
‫ ܣ‬ൌ ൥͵ ͳ൩ ‫ ܤ‬ൌ ቂ
ͳ
Ͷ ʹ
Program multiplication
Implicit none
Integer parameter:: n=3, m=4 , L=2
Integer, dimension (n,L)::a
Integer, dimension (L,m)::b
Integer, dimension (n,m)::c
Data a/1,3,4,5,1,2/
Read(*,*) ((b(i,j), j=1,m), i=1, L)
Do i=1,n
Do j=1,m
C(I,j)=0
Do k=1,L
C(i,j)=c(i,j)+a(i,k)*b(k,j)
Enddo;enddo;enddo
Write(*,50)((c(I,j),j=1,m),i=1,n)
50 format (4(2x,i5))
End
7
23
35
28
7
13
21
14
10 20 32
22
ϱ
͵ͷ ͵
ቃ
Ͷ͸ ʹ
Download