280 likes | 523 Views
CSC 201 Computer Programming- FORTRAN. Supplementary Lecture Note by Aborisade D.O. Course Contents. Repetitive Structures Do Loop While Loop Do While Array Sample FORTRAN Programs on Two dimensional array
E N D
CSC 201 Computer Programming- FORTRAN Supplementary Lecture Note by Aborisade D.O.
Course Contents • Repetitive Structures Do Loop While Loop Do While • Array Sample FORTRAN Programs on Two dimensional array Sample FORTRAN programs on Multidimensional array. References
Repetitive Structures • FORTRAN programming offers us three basic Control Structures: • (i)Sequence (ii) Selection and Repetition. • Repetition structure or loop makes possible the repeated execution of one or more statements, called the body loop. There are two basics types of repetition structures: • Repetition controlled by a counter: in which the body of the loop is executed once for each value of some control variable in a specified range of values . e.g Do Loop
Repetition Structure Contd. - Repetition controlled by a logical expression in which the decision to continue or to terminate repetition is determined by the value of some logical expression. e.g While Loop. . While loop is a repetition in which logical expression controls the loop, in that as long as the logical expression remain true, the loop continues, but when it is false, the loop terminates.
While Repetition structure WHILE logical expression true false Statement sequence
Do Repetition Structure • DO WHILE forms: • DO WHILE (logical expression) statement sequence END DO OR • WHILE (logical expression) DO statement sequence END WHILE
DO WHILE PROGRAM INTEGER NUMBER, SUM, LIMIT *Read LIMIT AND initialize NUMBER and SUM PRINT *, ‘ENTER VALUE 1+2+ ………+ ? IS TO EXCEEED’ READ *, LIMIT NUMBER=0 SUM=0 DO WHILE (SUM .LE. LIMIT) NUMBER=NUMBER+1 SUM=SUM+NUMBER END DO *Print the results PRINT *, ‘1+…………..+’, NUMBER, ‘ =‘, SUM, ‘>’, LIMIT END
Arrays • An array is a data structure in which a fixed number of data values of the same type are organized in a sequence, and direct access to each value is possible by specifying its position in sequence. • In a program, an array element can be referenced by using the array subscripted variable formed by appending a subscript (or index) enclosed in parentheses to the array variable e.g. NTIMES(2),SUM(20). The subscript specifies the position of an element in the array.
Arrays Contd. • Types of Arrays: • One-dimensional arrays • Two multidimensional arrays • Multidimensional arrays • Arrays are declared using DIMENSION statement. Array declaration format: DIMENSION list (where list is a list of array declarations of the form array name(1:u) separated by commas, the pair 1:u must be a pair of integer constants or parameters).
One Dimensional Array • e.gDIMENSION COUNT(20) INTEGER COUNT OR DIMENSION COUNT(1:20) INTEGER COUNT • Dimension in array declaration can be specified by parameters example the array FAILTM can be declared by INTEGER LLIM, ULIM PARAMETER (LLIM=1, ULIM=50) REAL FAILTM(LLIM:ULIM)
Input/Output in One Dimensional Array • There are three ways by which a one dimensional array can be read or displayed • use a Do loop containing an input/output statement. • use only the array name in an input/output statement. • use an implied Do loop in an input/output statement. Example 1: Input/Output using a Do loop
FORTRAN Program to Read and Display Velocities INTEGER LIMIT, NUMVEL, I PARAMETER (LIMIT=50) REAL VELOC(LIMIT) *Read the list of velocities PRINT *, ‘ENTER THE NUMBER OF VELOCITES:’ READ *, NUMVEL PRINT *, ‘ENTER THE VELOCITY VALUES, OE PER LINE:’ DO 10 I=1, NUMVEL READ *, VELOC(I) 10 CONTINUE *Print the list of velocities PRINT 20 20 FORMAT(/1X, ‘LST OF VELOCITIES: ‘/1X,18(‘=‘))
FORTRAN Program using the Array Name DO 40 I=1, NUMVEL PRINT 30, I, VELOC(I) 30 FORMAT(1X, I3, ‘:’,F10.1) 40 CONTINUE END Example 2: Input/Output using the Array Name INTEGER LIMIT PARAMETER(LIMIT=10) REAL VELOC(LIMIT) * Read the list of velocities
FORTRAN Program using Implied Do Loops PRINT *, ‘ENTER THE VELOCITY VALUES AS MANY AS DESIRED:’ READ *, VELOC * Print the list of velocities PRINT 20 20 FORMAT(/1X, ‘LIST OF VELOCITIES:’ / 1X, 18(‘=‘)/) PRINT 30, VELOC 30 FORMAT(1X, 5F10) END Example 3:Input/Output using Implied Do Loops. An implied Do loop in an input/output statement provides the most flexible method for reading the elements of an array. It enables a programmer to specify that only a portion of the array be transmitted and to specify the arrangement of the values to be read or displayed.
FORTRAN Program using Implied Do Loops INTEGER LIMIT, NUMVEL, I PARAMETER(LIMIT=50) REAL VELOC(LIMIT) * Read the list of velocities PRINT *, ‘ENTER THE NUMBER OF VELOCITIES:’ READ *, NUMVEL PRINT *, ‘ENTER THE VELOCITY VALUES AS MANY AS MANY PER LINE:’ READ *, (VELOC(I), I=1, NUMVEL) * Print the list of velocities PRINT 20, NUMVEL 20 FORMAT(/1X, ‘LIST OF’, 13, ‘VELOCITIES:’ / 1X, 21(‘=‘)) PRINT 30, VELOC(I), I=1, NUMVEL) 30 FORMAT(1X, 5F10.1) END
2-Dimensional Array • Many problems exist in which data to be processed can be naturally organized as a table . To easily solve problems like these, 2 dimensional arrays are defined to stored to data in rows and columns for processing. • A 2-dimensional array can be declared using statement such as: DIMENSION TEMTAB(4,3) REAL TEMTAB • In a 2-dimensional array, the two subscripts refers to the positions of data in the array. 4 for row position and 3 for column position. Example of a two dimensional array.
Two dimensional Array contd. Example: A Program to process data in a 2-dimensional array. INTEGER MAXTIM, MAXLOC, NTIMES, NLOCS, TIME, LOC PARAMETER (MAXTIM=24, MAXLOC=10) REAL TEMTAB(MAXTIM,MAXLOC) PRINT *, ‘ENTER # OF TIMES TEMPERATURES ARE RECORDED’ PRINT *, ‘AND # OF LOCATIONS WHERE RECORDED:’ READ *, NTIMES, NLOCS PRINT *, ‘ENTER THE TEMPERATURES AT THE FIRST LOCATION.’ PRINT *, ‘THEN THOSE AT THE SECOND LOCATION. AND SO ON:’ READ *, ((TEMTAB(TIME,LOC), LOC=1, NLOCS), TIME=1, NTIMES) PRINT * PRINT 100, (LOC, LOC=1, NLOCS)
Two dimensional Array contd. • 100 FORMAT(1X, T13, ‘LOCATION’ / 1X, ‘TIME’, 1016) DO 130 TIME=1, NTIMES PRINT 110, TIME, (TEMTAB(TIME,LOC), LOC=1, NLOCS) 110 FORMAT(/1X, I3, 2X, 10F6.1/) 130 CONTINUE
Multidimensional Array • This refers to array with more than two dimensions. For example, suppose that temperature readings are made for one week so that seven temperature tables such as one depicted below could be referred as a multidimensional array: Day 7 ….... Day 2 Day 1
Multidimensional Array contd. • The general form of an array declaration is: array-name(11:u1, 12:u2,………………………..1k:uk) where the number k of dimensions is at most seven; and each pair 1:u must be a pair of integer constants or parameters specifying the range of values for the ith subscript to be from 11 through u1. or in DIMENSION statements as: REAL ALPHA(5,10), BETA(5,5,2), GAMMA(1:2,-1:3), BETA(5:12) INTEGER COUNT(0:2,0:3,1:2) or DIMENSION list-of-array-declarations e.g DIMENSION ALPHA(5,10), BETA(5,5,2) DIMENSION GAMMA(1:2, -1:3), DELTA(5:12), COUNT(0:2,0:3, 1:2) REAL ALPHA, BETA, GAMMA, DELTA INTEGER KAPPA
Multidimensional Array-Program Example * Program to solve a linear system using Gaussian elimination INTEGER LIMROW, LIMCOL PARAMETER (LIMROW=10, LIMCOL=LIMROW+1) REAL LIN(LIMROW, LIMCOL), X(LIMROW) INTEGER N, I, J LOGICAL SINGUL * Read coefficients and constants PRINT *, ‘ENTER NUMBER OF EQUATIONS’ READ *, N DO 10 I=1, N
Multidimensional Array PRINT *, ‘ENTER COEFFICIENT AND CONSTANT OF EQUATION’ , + I, ‘: ‘ READ *, (LIN(I,J), J=1, N+1) 10 CONTINUE SUBROUTINE GAUSS(LIN, LIMROW, LIMCOL, N, X, SINGUL) REAL LIN(LIMROW, LIMCOL), X(LIMROW), TEMP, MULT, EPSIL PARAMETER (EPSIL=1E-7) INTEGER N, PIVROW LOGICAL SINGUL SINGUL=.FALSE. DO 50 I=1, N
Multidimensional Array * Locate pivot element ABSPIV=ABS(LIN(I,J)) PIVROW=I DO 10 K=I+1, N IF (ABS(LIN(K,I)) .GT. ABSPIV) THEN ABSPIV=ABS(LIN(K,I)) PIVROW=K END IF 10 CONTINUE * Check if matrix is nearly singular IF (ABSPIV .LT. EPSIL) THEN SINGUL=.TRUE. RETURN
Multidimensional Array END IF * Interchange row PIVROW and I if necessary IF (PIVROW .NE. I) THEN DO 20 J=1, N+1 TEMP= LIN(I,J) LIN(I,J)= LIN(PIVROW, J) LIN(PIVROW,J) = TEMP 20 CONTINUE END IF * Eliminate Ith unknown from equations I+1, ………N DO 40 J=I+1, N MULT= -LIN(J, I)/ LIN(I, I)
Multidimensional Array DO 30 K=I, N+1 LIN(J,K)=LIN(J,K)+ MULT* LIN(I,K) 30 CONTINUE 40 CONTINUE 50 CONTINUE * Use subroutine GAUSS to find the solution and then display the solution CALL GAUSS(LIN, LIMROW, LIMCOL, N, X, SINGUL) IF (.NOT. SINGUL) THEN PRINT *, ‘SOLUTION IS’ DO 20 I=1, N PRINT 100, I, X(I)
Multidimensional Array 100 FORMAT(1X, ‘X(‘, I2, ‘) =‘, F8.3) 20 CONTINUE ELSE PRINT *, ‘MATRIX IS (NEARLY) SINGULAR’ END IF END
References • Larry N. and Sandford L.:FORTRAN 77 for Engineers and Scientists with An Introduction to FORTRAN 90 fourth Edition.