Documente Academic
Documente Profesional
Documente Cultură
N
1 2
s
2
N 1 i 1
Xi X
6
This program computes these two quantities from data
read from file DATA. The first item
IMPLICIT NONE
INTEGER :: I, N
REAL :: Std = 0
REAL, DIMENSION(5) :: X
REAL :: XBar = 0
OPEN (1, FILE = 'DATA.txt')
READ (1, *) N
DO I = 1, N
READ (1, *) X(I)
XBar = Xbar + X(I)
END DO
XBar = XBar / N
DO I = 1, N
Std = Std + (X(I) - XBar) ** 2
END DO
Std = SQRT( Std / (N - 1) )
WRITE(*,*) 'Mean: ', XBar
WRITE(*,*) 'Std deviation: ', Std 7
END
Arrays Declarations
For a simple variable of types (Real and Integer) we have:
1- One dimensional array of real called (Vector)
REAL,Dimension(10)::vector
REAL, DIMENSION(10) :: X
or
Real :: Vector(10)
Real :: X(10)
This is declarations creates 10 Real variables:
vector(1),vector(2),vector(3),.........
.., vector(10)
which are stored contiguously. 8
2- Two dimensional array of an integer called (Array)
Integer,Dimension(5,20)::array
or
Integer :: Array(5,20)
This is mean that the declaration creates 100 Integer
variables:
array(1,1),array(1,2),array(2,1)..........
,array(5,20)
9
Array(1,1) Array(1,2) Array(1,20)
Array(2,1)
Array(3,1)
Array(5,1) Array(5,20)
11
Rules for using dimension statement:
1- In one program we can use more than one
dimensional statement.
2- It,s not allowed to any control statements to
GOTO dimension statement .
3- If the subscripted variable is integer then all the
elements in it must be integer (also for real).
4- The subscripted must be integer X(i),X(J),....,also
it must be greater than zero.
12
5- The general form of diemsion statement of one
dimensional array is:
Real,Dimension(n)::X
Where: n: is the number of location stored.
x: is the name of variable
6- The general form of dimension for two dimension
array is:
Integer,Dimension(n,m)::X
Where: n: is the number of rows.
m: is the number of columns.
x: is the name of array.
13
Program(1):
Write a program in Fortran 90 to calculate the ages average of students in first
stage where the number of students is (10) when:-
10
age average = Ai
i=1
Solution:
Real,dimension(10)::A
Open(4,file='data.txt')
Sum=0.0
Do I=1,10
Read(4,*)A(I)
Sum=Sum+A(I)
End Do
Average = Sum/10
Print*, 'The ages average of student is=',
Average
End
14
Program(2):
Write a program in Fortran 90 to find the average mark of class (10
students) for computer examination using dimension
statement.
Solution:
Integer::I
Real,dimension(10)::X
Real::total
S=0.0
Do I=1,10
Print*, 'Supply mark of student No.',I
Read*,X(I)
S=S+X(I)
End do
Print*, 'Average score is',S/10
End
15
Declarations
Literals and constants can be used in array declarations,
REAL, DIMENSION(100) :: R
REAL, DIMENSION(1:10,1:10) :: S
REAL :: T(10,10)
REAL, DIMENSION(-10:-1) :: X
INTEGER, PARAMETER :: lda = 5
REAL, DIMENSION(0:lda-1) :: Y
REAL, DIMENSION(1+lda*lda,10) :: Z
17
Array Syntax
Can reference:
• whole arrays
A = 0.0
sets whole array A to zero.
B = C + D
adds C and D then assigns result to B.
• elements
A(1) = 0.0
sets one element to zero,
B(0,0) = A(3) + C(5,1)
sets an element of B to the sum of two other elements.
18
Array Syntax
• array sections
A(2:4) = 0.0
sets A(2), A(3) and A(4) to zero,
19
Array Sections
subscript-triplets specify sub-arrays. The general form is:
[< bound1 >]:[< bound2 >][:< stride >]
The section starts at < bound1 > and ends at or
before < bound2 >. < stride > is the
increment by which the locations are selected.
Example
A(:) ! the whole array
A(3:9) ! A(m) to A(n) in steps of 1
A(3:9:1) ! as above
A(m:n) ! A(m) to A(n)
A(m:n:k) ! A(m) to A(n) in steps of k
20
A(8:3:-1) ! A(8) to A(3) in steps of-1
A(8:3) ! A(8) to A(3) step 1=> Zero size
A(m:) ! from A(m) to default UPB
A(:n) ! from default LWB to A(n)
A(::2) ! from default LWB to UPB step 2
A(m:m) ! 1 element section
A(m) ! scalar element - not a
section
are all valid sections.
21
Array Input/out put statements
Implied Do-loops:
A compact way of reading in (or writting out) the
elements of an array is by use of an implied Do-
loop , which is structuer a variable only for dealing
with I/O
1- For one dimension array :
To read and write array A(I) using Do-loops
Do I=1,5
Read(*,*)A(I) Do I=1,5
End Do write(*,*)A(I)
End Do 22
Other types of array I/O
R= (10,20,30,40,50) DIRECT
Read(*,*)(R(I),I = 1,5) without do-loop
This is mean read the element in subscripted variable R(I).
Write(*,*)(P(I),I = 1,5)
This is mean that the program print output as follows:
P(1),P(2),P(3),P(4),P(5)
25
Rules for matrix multiplication:
1 4 7 10 40 70
A( I , J ) 2 5 8 , B( I , J ) 20 50 80
3 6 9
30 60 90
27
!This program to find the result of summation of
!Two matrix
Program Sum_arrays
Integer,dimension(3,3)::A,B,C
Do
Do J=1,3
Do I=1,3
Read(*,*)A(I,J), B(I,J)
End do
End do
Do J=1,3
Do I=1,3
C(I,J)=A(I,J)+B(I,J)
End do
End do
Do I=1,3
Write(*,*)(C(I,J),J=1,3)
End do
End do 28
Program(2):
Write a program in Fortran 90 to transpose the following matrix.
!Fortran 90
!This program to find the result of
!Transpose of matrix
Program transpose_array
Integer,dimension(3,4):: N
Integer,dimension(4,3):: M
Do
Do I=1,3
Read(*,*)(N(I,J),J=1,4)
End do
Do J=1,3
Do I=1,4
M(I,J) = N(J,I)
End do
End do
Do J=1,3
Write(*,*)(M(I,J),I=1,4)
End do
End do
End Program transpose_array
29
Program(2):
Write a program in F.90 to find the results of:
4 5
B K
6 3
30
!Fortran 90
!This program to find the result of two dimension array
Program B_array
Integer,dimension(2,2)::A,B
Do
Read(*,*)K
Do J=1,2
Do I=1,2
Read(*,*)A(I,J)
B(I,J)=K*A(I,J)
End do
End do
Print*, “ The result of matrix”
Do I=1,2
Write(*,*)(B(I,J),J=1,2)
End do
End do
End Program B_array 31