Document 12820954

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:
– rank 2
– shape (/ 2, 4 /)
- extents 2 and 4
- size 8
Declariing an A
Arrray
For example, suppose we have the following array declarations:
REAL, DIMENSION(-1:1)
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 REALss and the indices are in the
range of -1 and 1.
The elements of array InputData are INTEGERs and the indices
ndices 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/Outputt
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
DATA stateement
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
EXAMPLE
Write a program to add, subtract, and multiply to matrices A & B
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
ϯ
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
Integer::i,j,k
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
Run
7
23
35
28
7
13
21
14
10 20 32
22
ϰ
͵ͷ ͵
ቃ
Ͷ͸ ʹ
Download