1 / 27

CSC 201 Computer Programming- FORTRAN

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

parry
Download Presentation

CSC 201 Computer Programming- FORTRAN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 201 Computer Programming- FORTRAN Supplementary Lecture Note by Aborisade D.O.

  2. 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

  3. 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

  4. 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.

  5. While Repetition structure WHILE logical expression true false Statement sequence

  6. Do Repetition Structure • DO WHILE forms: • DO WHILE (logical expression) statement sequence END DO OR • WHILE (logical expression) DO statement sequence END WHILE

  7. 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

  8. 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.

  9. 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).

  10. 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)

  11. 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

  12. 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(‘=‘))

  13. 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

  14. 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.

  15. 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

  16. 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.

  17. 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)

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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)

  25. 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)

  26. Multidimensional Array 100 FORMAT(1X, ‘X(‘, I2, ‘) =‘, F8.3) 20 CONTINUE ELSE PRINT *, ‘MATRIX IS (NEARLY) SINGULAR’ END IF END

  27. References • Larry N. and Sandford L.:FORTRAN 77 for Engineers and Scientists with An Introduction to FORTRAN 90 fourth Edition.

More Related