FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Repetitive Execution
 DO-CYCLE
In parallel with the DO-EXIT construct, Fortran has a DO-CYCLE
construct as follows:
DO control-info
statements-1
CYCLE
statements-2
END DO
When the execution of a DO-loop encounters the CYCLE statement, the
DO-loop starts next iteration immediately.
INTEGER :: i
DO i = 1, 5
IF (i = = 3) THEN
CYCLE
ELSE
WRITE(*,*) i
END IF
END DO
The upper loop only displays 1, 2, 4 and 5.
INTEGER :: Range
DO
WRITE(*,*) 'An integer >= 2 please --> '
READ(*,*) Range
IF (Range < 2) THEN
ϭ
WRITE(*,*) 'Input not in the required range'
CYCLE
END IF
... process Range ...
END DO
 Nested DO-Loops
A DO-loop can contain other DO-loops in its body
DO
statements-1
DO
statement-2
END DO
statement-3
END DO
INTEGER :: i, j
DO i = 1, 9
DO j = 1, 9
WRITE(*,*) i*j
END DO
END DO
INTEGER :: u, v
INTEGER :: a, b, c
DO u = 2, 5
DO v = 1, u-1
a = 2*u*v
b = u*u - v*v
c = u*u + v*v
WRITE(*,*) a, b, c
END DO
END DO
Ϯ
EEX
XA
AM
MPPLLEE
Write a fortran 90 program to reads a number of integer input until a
negative one, and determines the minimum and maximum of the input data
values.
PROGRAM MinMax
IMPLICIT NONE
INTEGER :: Minimum, Maximum
! max and min
INTEGER :: Count
! # of data items
INTEGER :: Input
! the input value
Count = 0
! initialize counter
DO
! for each iteration
READ(*,*) Input
! read in a new input
IF (Input < 0) EXIT
! if it is < 0, done.
Count = Count + 1
! if >= 0, increase counter
WRITE(*,*)) 'Data item #', Count, ' = ', Input
IF (Count == 1) THEN
! is this the 1st data?
Maximum = Input
! yes, assume it is the
Minimum = Input
! min and the max
ELSE
! no, not the 1st data
IF (Input > Maximum) Maximum = Input
IF (Input < Minimum) Minimum = Input
END IF
END DO
WRITE(*,*)
IF (Count > 0) THEN
! if at one data item found
WRITE(*,*) 'Found ', Count, ' data items'
WRITE(*,8) Maximum,, Minimum
8 FORMAT(2X,'Maximum=',
',7i, / ,2x,'Minimum=',7i)
ELSE
WRITE(*,*) 'No data item found.'
! no data item found
END IF
END
EEX
XA
AM
MPPLLEE
The exponential function, EXP(x),, is defined to be the sum of the
following infinite series:
ϯ
Write a program that reads in a REAL value and computes EXP() of that value
using the series until the absolute value of a term is less than a tolerance value,
say 0.00001.
Program exponential
Implicit none
REAL, PARAMETER :: Tolerance = 0.00001
Integer ::i,j
Real (kind=4) :: x , term , sum, fact
Read(*,*) x
Sum=1
I=1
Do
Fact=1
Do j=1,i
Fact=fact * j
Enddo
Term =x**i/fact
If (abs(term) < tolerance) exit
Sum=sum + term
I=i+1
Enddo
WRITE(*,*) 'After ',i , ' iterations:'
WRITE(*,*) ' Exp(', X, ') = ', Sum
WRITE(*,*) ' From EXP() = ', EXP(X)
WRITE(*,*) ' Abs(Error) = ', ABS(Sum - EXP(X))
End
EEX
XA
AM
MPPLLEE
Write a fortran 90 program to compute the ln(x+1), using the series until
the absolute value of a term is less than error value, say 0.00001.
Ln(x+1)=š െ
šʹ
ʹ
൅
‫͵ݔ‬
͵
െ‫ڮ‬
ϰ
Program ln
Implicit none
REAL, PARAMETER :: error = 0.00001
Integer ::i
Real (kind=4) :: x , term , sum
Read(*,*) x
i=0;sum=0
Do
Term=((-1)**(i)*x**(i+1))/(i+1)
if(abs(term)< error)exit
Sum=sum+term
i=i+1
enddo
write(*,50) sum,i
50 format (2x,'ln(1+x)=',2x,f8.6,2x,' after ',i4,2x,'Iteration')
end
ϱ
Download