Computer Science 1510
Computer Science 1510
Lecture 9
Lecture Outline
Repetition:
DO
DO WHILE
DO EXIT
CS1510 Lecture 9
Repetition
CS1510 Lecture 9 1
Counter-controlled DO loop
Syntax:
DO control-variable=initial,limit,step-size
statements-to-repeat
END DO
CS1510 Lecture 9 2
Example 1: DO
PROGRAM Hello
INTEGER::i
DO i=1,10
WRITE(*,*) Hello
END DO
END PROGRAM Hello
CS1510 Lecture 9 3
Example 2: DO
PROGRAM Odd
INTEGER::i,n
READ(*,*) n
DO i=1,n,2
WRITE(*,*) i
END DO
END PROGRAM Odd
CS1510 Lecture 9 4
Example 3: DO
PROGRAM Hello
INTEGER::i
DO i=1,10
WRITE(*,*) Hello
END DO
WRITE(*,*) i = ,i
END PROGRAM Hello
CS1510 Lecture 9 5
Example 4: DO
Example:
PROGRAM Hello
INTEGER::i
DO i=10,1,-1
WRITE(*,*) Hello
END DO
WRITE(*,*) i = ,i
END PROGRAM Hello
CS1510 Lecture 9 6
Counter-controlled DO loop
CS1510 Lecture 9 7
Example 5: DO
Example:
PROGRAM Sum_of_integers
IMPLICIT NONE
INTEGER :: num, i, sum=0
WRITE(*,*) This program prints the sum &
& 1 + 2 + 3 +...+ num
WRITE(*,*) Enter a value for num
READ(*,*) num
DO i=1,num
sum = sum + i
END DO
WRITE(*,*) 1 + 2 + 3 +...+ ,num, = ,sum
END PROGRAM Sum_of_integers
CS1510 Lecture 9 8
Example 6: DO
Example:
PROGRAM Mult_table
IMPLICIT NONE
INTEGER :: m, n, lastm, lastn, prod
WRITE(*,*) Calculating m*n up to some limit
WRITE(*,*) Enter the limit of m and n
READ(*,*) lastm, lastn
WRITE(*,*) M N M*N
WRITE(*,*) -------------
DO m=1,lastm
DO n=1,lastn
prod = m*n
WRITE(*,2) m,n,prod
END DO
END DO
2 FORMAT(I2,2X,I2,2X,I3)
END PROGRAM Mult_table
CS1510 Lecture 9 9
Fortran statements: DO-WHILE
Syntax:
DO WHILE (logical-expression)
statements-to-repeat
END DO
CS1510 Lecture 9 10
Example 1: DO WHILE
PROGRAM While
IMPLICIT NONE
INTEGER::n,m
n=0
m=5
DO WHILE((m-n)>0)
n=n+1
WRITE(*,90) m,n
END DO
90 FORMAT(m = ,I2, n = ,I2)
END PROGRAM While
Output:
m = 5 n = 1
m = 5 n = 2
m = 5 n = 3
m = 5 n = 4
m = 5 n = 5
CS1510 Lecture 9 11
Example 2: DO WHILE
PROGRAM While_odd
INTEGER::i,n
i=1
READ(*,*) n
DO WHILE (i<=n)
WRITE(*,*) i
i=i+2
END DO
END PROGRAM While_odd
CS1510 Lecture 9 12
Fortran statements: DO EXIT
Syntax:
DO
statement-sequence-1
IF (logical-expression) EXIT
statement-sequence-2
END DO
CS1510 Lecture 9 13
Example: DO EXIT
PROGRAM Summation
IMPLICIT NONE
INTEGER :: num, sum, limit
CS1510 Lecture 9 14
Fortran statements: CYCLE
Syntax:
DO i=1,N
statement-sequence-1
IF (logical-expression-1) CYCLE
statement-sequence-2
END DO
CS1510 Lecture 9 15
A few more points about loops
CS1510 Lecture 9 16
Program 1: Mean time to failure
PROGRAM Mean_Time_to_Failure
!-----------------------------------------------------------------------
! Program to read a list of failure times, count them, and find the
! mean time to failure. Values are read until an end-of-data flag
! is read. Identifiers used are:
! INPUT:
! A list of failure times (FailureTime = current failure time read)
! OUTPUT:
! NumTimes : the number of failure time readings
! MeanFailureTime : the mean time to failure
!-----------------------------------------------------------------------
IMPLICIT NONE
INTEGER :: NumTimes
REAL :: FailureTime, Sum, MeanFailureTime
REAL, PARAMETER :: EndDataFlag = -1.0
Sum=0.0
NumTimes=0
WRITE(*,*) "Enter failure time of", EndDataFlag, "to stop."
DO
WRITE(*,*) "Enter failure time:"
READ(*,*) FailureTime
! If end-of-data, terminate repetition
IF (FailureTime == EndDataFlag) EXIT
NumTimes = NumTimes + 1
Sum = Sum + FailureTime
END DO
IF (NumTimes /= 0) THEN
MeanFailureTime = Sum / NumTimes
WRITE(*,*)
WRITE(*,*) "Number of failure time readings:", NumTimes
WRITE(*,*) "Mean time to failure:", MeanFailureTime
ELSE
WRITE(*,*) "No failure times were entered."
END IF
END PROGRAM Mean_Time_to_Failure
CS1510 Lecture 9 17
Example 2: Approximating sin(x) and cos(x)
X
x2n+1 x3 x5 x7
n
sin(x) = (1) = x + +
n=0
(2n + 1)! 3! 5! 7!
X
x 2n
x 2
x 4
x 6
cos(x) = (1)n = 1 + +
n=0
(2n)! 2! 4! 6!
CS1510 Lecture 9 18
Program 2: Approximating sine
PROGRAM Approx_sine
IMPLICIT NONE
INTEGER::i,n
REAL::x,term,approx
WRITE(*,*) Enter a value for x
READ(*,*) x
WRITE(*,*) How many terms would you like to include?
READ(*,*) n
term=x
approx=x
DO i=1,n-1
term=(-1)*term*(x**2/(2*i*(2*i+1)))
approx=approx+term
END DO
WRITE(*,*) sin(,x,) is approximately,approx
END PROGRAM Approx_sine
CS1510 Lecture 9 19
Program 3: Approximating sine/cosine
PROGRAM Functions
IMPLICIT NONE
INTEGER::i,n
REAL::x,term,approx
CHARACTER::func
WRITE(*,*) Enter s for sin, and c for cos or q to quit
READ(*,*) func
DO WHILE(func/=q)
WRITE(*,*) Enter a value for x
READ(*,*) x
WRITE(*,*) How many terms would you like to include?
READ(*,*) n
SELECT CASE (func)
CASE(s)
term=x
approx=x
DO i=1,n-1
term=(-1)*term*(x**2/(2*i*(2*i+1)))
approx=approx+term
END DO
WRITE(*,*) sin(,x,) is approximately,approx
CASE(c)
term=1
approx=1
DO i=1,n-1
term=(-1)*term*(x**2/(2*i*(2*i-1)))
approx=approx+term
END DO
WRITE(*,*) cos(,x,) is approximately,approx
CASE DEFAULT
WRITE(*,*) Invalid entry, try again
END SELECT
WRITE(*,*) Enter s for sin, and c for cos or q to quit
READ(*,*) func
END DO
END PROGRAM Functions
CS1510 Lecture 9 20
Example 3: Euclids algorithm
CS1510 Lecture 9 21
Euclids algorithm - Why it works
= r = a qb = sd qtd = (s qt)d
CS1510 Lecture 9 22
Program 4: Euclids algorithm
PROGRAM Euclid
IMPLICIT NONE
INTEGER::m,n,q,r
WRITE(*,*) Enter two integers m and n, where m>n
READ(*,*) m,n
r=1
DO WHILE (r/=0)
q=m/n
r=m-q*n
WRITE(*,*) m, = ,n, * ,q, + ,r
m=n
n=r
END DO
WRITE(*,*) The greatest common divisor is ,m
END PROGRAM Euclid
CS1510 Lecture 9 23
Example 4: Linear Least-Squares
Suppose that you have data collected from an
experiment, and you want to compute a line of
best fit.
where x
and y are the mean values.
CS1510 Lecture 9 24
Program 5: Linear Least-Squares
PROGRAM Least_Squares_Line
IMPLICIT NONE
INTEGER :: NumPoints
REAL :: X, Y, Sum_of_X, Sum_of_X_squared, Sum_of_Y, Sum_of_XY,&
X_Mean, Y_Mean, Slope, Y_Intercept
REAL, PARAMETER :: EndDataFlag = -99.0
NumPoints = 0
Sum_of_X = 0.0
Sum_of_X_squared = 0.0
Sum_of_Y = 0.0
Sum_of_XY = 0.0
WRITE(*,*) "To stop, enter", EndDataFlag, " for coordinates of point."
DO
WRITE (*, (1X, A), ADVANCE = "NO") "Enter point: "
READ(*,*) X, Y
NumPoints = NumPoints + 1
Sum_of_X = Sum_of_X + X
Sum_of_X_squared = Sum_of_X_squared + X ** 2
Sum_of_Y = Sum_of_Y + Y
Sum_of_XY = Sum_of_XY + X * Y
END DO
CS1510 Lecture 9 25