fortran course

68
ALEXANDRIA UNIVERSITY FACULTY OF ENGINEERING STRUCTURAL ENGINEERING DEPARTMENT COMPUTER APPLICATIONS FOR CIVIL ENGINEERING FORTRAN 77 PROGRAMMING (CE146) Dr.Ahmed Shamel Fahmy 2009-2010

Upload: john-ramirez

Post on 08-Nov-2014

62 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Fortran Course

ALEXANDRIA UNIVERSITYFACULTY OF ENGINEERING

STRUCTURAL ENGINEERING DEPARTMENTCOMPUTER APPLICATIONS FOR CIVIL ENGINEERING

FORTRAN 77 PROGRAMMING(CE146)

Dr.Ahmed Shamel Fahmy2009-2010

Page 2: Fortran Course

A Brief History of FORTRAN/Fortran

Fortran is a general programming language which is suitable and oriented toEngineering and Science. It names comes from two words FORmulaTRANslation. It was the first ever high level programming language witheasy to use while powerful commands.

Started at 1950, till now, Fortran is still well known and extensively used bymillions all over the world. The phenomenal success of the FORTRAN Iteam, can be attributed in part to the friendly non-authoritative groupclimate. Another factor may be that IBM management had the sense toshelter and protect the group, even though the project took much moretime than was first anticipated.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 3: Fortran Course

A Brief History of FORTRAN/Fortran

FORTRAN II (1958) was a significant improvement, it addedthe capability for separate compilation of programmodules, assembly language modules could also be'linked loaded' with FORTRAN modules.

FORTRAN III (1958) was never released to the public. Itmade possible using assembly language code right inthe middle of the FORTRAN code.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 4: Fortran Course

FORTRAN IV (1961) was a 'clean up' of FORTRAN II, improving things like theimplementation of the COMMON and EQUIVALENCE statements, andeliminating some machine-dependant language irregularities.

Formally outdated many years ago, compilers for FORTRAN 77 are still used today, mainly to re-compile legacy code.

A new standard has been designed and widely implemented in recent years. It is unofficially called Fortran 90, and adds many powerful extensions to FORTRAN 77. The language in its present form is competitive with computer languages created later

FORTRAN 95 added some minor improvements to the Fortran 90 standard.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 5: Fortran Course

SUMMARY OF FORTRAN ADVANTAGES

a) Scientifically and Engineering orientedb) Better optimized codec) A lot of existing coded) Easier to learne) More efficient mathematicsf) Easier to use and more robustg) Better diagnostics

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 6: Fortran Course

Problem Formulation

In order to construct a computer program, many stepsmust be considered; all of them are logical and easy tohandle and follow. The following may be introduced asthe general steps for constructing a computer program:

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

1- Program aim:State the problem clearly in an easy and understandable sentences.

Page 7: Fortran Course

Problem Formulation

2- Program formulation:The problem is divided into smaller stages; each stage is

intended to reach a specific result. Then each step is broken into smaller items to help the step by step approach required by computer in order to reach stage result.

3- Change the logical thinking into a computer language:The program formulation explained at step 2 is expressed

using a language that the computer understands and follows in order to perform the required steps. This means constructing a computer program suitable for solving a specific problem.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 8: Fortran Course

4- Debugging the computer program :The program is examined and checked; many

errors are discovered and corrected in order to get the program running as expected. Some errors are serious and may cause the program to stop running as dividing a number by zero, using the a name which is not defined at earlier stage, or using some codes which the computer language does not support.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 9: Fortran Course

PROGRAMMING

There are four kinds of actions which the program can order the computer to do, these actions are:

1- Step by Step Actions:Where the computer is asked to do specific jobs each after the other;

like adding numbers, subtracting numbers, calculating values or any other straight forward action to perform.

2- Decision Actions:Where the computer is asked to check a condition and choose a way

based on the output of the condition checked. For example, the number of reactions of a simple beam is checked, then if the beam is unstable, the program stops and print an error note or the beam is stable the program continues solving the problem.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 10: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

PROGRAMMING

3- Loop Actions:Where the computer is asked to repeat one or ore steps for many

times as requires by the program. For example, solving a beam problem for many load cases. The computer is asked to solve the first load condition then the second an so on.

4- Input and Output Actions:Where the computer is asked to get input (data) or to write out the

answers in a specific form.5- Subroutines and Functions:Where the computer is asked to store a ready-to-use sub programs

inside the main program to be used for a repeated actions needed while running the program.

Page 11: Fortran Course

PROGRAMMING

Example:For two given numbers x1 and x2, it is required to

evaluate and print the bigger of the.

Logical thinking:1- look at the two numbers.2- compare the two numbers and choose the bigger

of them.3- write the bigger number.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 12: Fortran Course

PROGRAMMING

Automated thinking:1- Start the program.2- Read the first number x1.3- Read the second number x2.4- Compare if x1 » x2 Then x1 is bigger.5- if x1 is bigger then print x1.6- if x1 is not bigger then print x2.7- End of the program.

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 13: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Typing the program code according to FORTRAN requirments

The page is divided into 80 columns, these columns are divided into a very important way of dividing or classification according to the following:

Page 14: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Typing the program code according to FORTRAN requirments

1- Column № 1: Indicator columnThis column indicates a comment, if a value of * or

c is typed in this column, it indicates that this line will be used as a comment and will not be considered by the computer to perform any action.

If any other value is given, this means to the computer that this is a regular line.

Page 15: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Typing the program code according to FORTRAN requirments

2- Columns № 2,3,4 and 5: Statement labels columnsThese columns are used to type Statement labels which may

be used by the program as a reference number (same as address of a house, it is used in order to reach it).

3- Column № 6: Continuation columnThis column may be used in order to tell the computer that this line is a continuation line for the above (preceding) line. If any numeric or alphabetic value is given in column 6 rather than zero or blank this means that this line is a continuation for the above line.

Page 16: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Typing the program code according to FORTRAN requirments

4- Columns № 7 to 72: Statement columnsThese lines are used in order to type the required codes and actions required to be performed by the computer.

5- Columns № 73 to 80: Dum columnsThese lines are ignored by the computer and any codes listed inside this range are not considered.

Page 17: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

PROGRAM, FUNCTION, SUBROUTINE

FORMAT PARAMETER IMPLICIT

type declarations and other specification statements:

COMMON, DIMENSION

DATA statement functions

executable statements

END

Page 18: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLEPROGRAM MAIN

C This program computes the circumference andC area of a circle for a given radius.

REAL PI REAL RADIUS, CIRCUM, AREA PI = 3.1416 RADIUS = 5. CIRCUM = 2. * PI * RADIUS AREA = PI * RADIUS * RADIUS

C Calculations are done. Print the results.10 PRINT *, 'THE RADIUS OF THE CIRCLE IS', RADIUS

PRINT *, 'THE CIRCUMFERENCE OF THE CIRCLE IS', CIRCUM PRINT *, 'THE AREA OF THE CIRCLE IS', AREASTOPEND

Page 19: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

FORTRAN LANGUAGEThe FORTRAN language consists of many orders, commands, and rules. The

first step is to know the basic components of this language and the rules used in it.

CONSTANTS: Constants are the values, which are not changed, for example:

7 +22.5 -12.5 63.4 0.147E+2

INTEGER CONSTANTS: - These are the integer whole numbers.- They may be preceded by (+) or (-) sign.- Their maximum value is (215-1) which is (32767) this value depends on the

compiler used, but the value mentioned will be used in our course.- Their minimum value is (-215-1) which is (-32767) this value depends on the

compiler used, but the value mentioned will be used in our course.

Page 20: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Examples:

Valid examples Invalid examples Comments

-103 (-) sign must be used-103.0 No decimal points allowed1/7 No fractions allowed

+105 (+) sign is optional123,67 No commas allowed

32765 Within range of use99765 Exceeds maximum value

-32766 Within range of use

Page 21: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

REAL CONSTANTS: (FLOATING POINT CONSTANTS)

These are the numbers which always have a decimal point They may be preceded by (+) or (-) sign Their maximum value is 10+38

Their minimum value is 10-39

They are 8 digits maximum They may be in an engineering exponent form such as:

14.3 = 0.143 x 102 = 0.143 E+2

Page 22: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Examples:

Valid examples Invalid examples Comments-10.3 (-) sign must be used

-103 It is integer not real12.3$ Only numbers permitted

+105.7 (+) sign is optional123,67.4 No commas allowed

-0.127E12 Negative value is allowed 0.127E-2.5 Exponent must be integer

0.0E0 Zero value1E2 May be used

0.135E825 Value is too large0.135E-124 Value is too small

Page 23: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Decimal Scientific Exponential

0.000135 1.35 × 10-4 0.135E-03

-246.8 -2.468 × 102 -0.2468E+03

235700000000000000000.0 2.357 × 1020 0.2357E+21

Page 24: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

VARIABLES:

Variables are names used to represent a quantity, which may have different values, for example:

HEIGHT WIDTH TEMPI AGE

Rules of variables:

Variable name are 1 to 6 characters long Only letters (A-Z) and numbers (0-9) are allowed First character must be a letter Upper and lower case are equal Blank spaces are ignored

Page 25: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

INTEGER VARIABLES:

They must be valid variables and following all the above mentioned rules. The first character is ( I, J, K, L, M, or N )

Examples:

Valid examples Invalid examples CommentsLENGTH Begins with (L)

3stars Must begin with a letterelevation More than six characters

My2cat Starts with (M)GOOD4U Starts with (G)TOYS-R-US More than six characters

Starts with (T)Not allowed characters

LO TFY Spaces are ignored

Page 26: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

REAL VARIABLES: (FLOATING POINT VARIABLES)They must be valid variables and following all the above mentioned rules The first character is not one of ( I, J, K, L, M, or N )

Examples:

Valid examples Invalid examples CommentsALOOO Begins with (A)

ALLSTARS More than six characters GO2 Begins with (G)

NEED$? Starts with (N)Not allowed characters

2G0 Starts with a numberBO N A Spaces are ignored

Page 27: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Priority Operation Symbol FORTRAN Expression

Arithmetic Expression

inside to outside

Parentheses( )

right to left Exponentiation ** A**B ab

left to right Multiplication and Division

*/

A*BA/B

a×ba÷b

left to right Addition, Subtraction, and Unary Minus

+-

A+BA-B-A

a+ba-b-a

Page 28: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

INPUT AND OUTPUT STATMENTS:

Input and output are done using READ and WRITE commands, first input and output are defined as:

When data is presented to the computer, it is called input.When data is presented from the computer, it is called output.

FORM:READ (K1, K2) X, Y, TEMP, SAS READ (5,10) X, Y, TEMP, SAS

(K1) is a logic unit number specified by computer manager for input. This number specifies for the program where to get the data from; Screen? Keyboard? File? ....(k2) is called FORMAT label, it is like an address for a specific form to be used by the program to arrange data places and how they are arranged and stored.X, Y, TEMP, SAS are list of variables to be given as input

Page 29: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

INPUT AND OUTPUT STATMENTS:

WRITE (K3, K4) A, B, SANTA, YEA WRITE (6,25) A, B, SANTA, YEA

(K3) is a logic unit number specified by computer manager for output. This number specifies for the program where to send the output; Screen? Printer? File? ....(k4) is called FORMAT label, it is like an address for a specific form to be used by the program to arrange output places and how they are arranged and presented.A, B, SANTA, YEA are list of variables to be listed as output

Page 30: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

INPUT AND OUTPUT STATMENTS:

Logic unit numbers: It is a number specified by the computer manager to specify where to read or write the data (Screen? Disk? Printer...etc), each unit at the computer center must have a logic unit number, for example:

Keyboard has a logic number (5)Printer has a logic number (6) Screen has a logic number (*)

These numbers may be specified, controlled or changed by the FORTRAN program itself or by the computer manager according to the language codes and their limits and regulations.

Page 31: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

FORMAT label (statement number):

It is a number used one time only at the program. It is used to identify a specific type, space and appearance of the data (type, size...)

Example:

READ (5, 10) ALI, HANI, IAM, BAD, GOOD 10 FORMAT (2F10.3, I3, 2F7.2)

Page 32: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

READ (5, 10) ALI, HANI, IAM, BAD, GOOD 10 FORMAT (2F10.3, I3, 2F7.2)

The above command means the following:

There is data to be given, and the program is asked to store it. (READ) The data consists of five variables: ALI, HANI, IAM, BAD AND GOOD. The data will be given through a file. (5)The data will be arranged as specified by a FORMAT has a label (or a statement number) of 10.The FORMAT with the label (label number) 10 tells the program to arrange the following places:

•2 places for real values (ALI and HANI), each place consists of 10 spaces. Three of these spaces are after the decimal point. (2F10.3)•1 place for an integer value (IAM), this place consists of 3 spaces. (13)•2 places for real values (BAD and GOOD), each place consists of 7 spaces. Two of these spaces are after the decimal point. (2F7.2)

Page 33: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Example:K = 17A = 11.27SAS = -12.71 J = -220WRITE (6, 10) K,J

10 FORMAT (I3,I5)The output will have the following form:

WRITE (6,20) K,J20 FORMAT (2I5)

1 7 - 2 2 0

1 7 - 2 2 0

1 2 3 1 2 3 4 5

1 2 3 4 5 1 2 3 4 5

Page 34: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Example:

I = 5IK = 22A = 11.27B = 1.2C =-11.02SAS = - 11.27WRITE (6,30) I,IK,SAS

30 FORMAT (I2,I4,2X,F6.2)

The output will have the following form:

5 2 2 Ø Ø - 1 1 . 2 7

1 2 1 2 3 4 1 2 1 2 3 4 5 6

Page 35: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Example:

Write a program to read the diameter of a circle, then to print its area.

READ (*,*) DAREA = (22.0/7.0)*D*D/4.0 WRITE (*,50) AREA

50 FORMAT ('AREA',F8.3)

Page 36: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Function ArgumentType

ReturnType Description

ABS DIR DIR absolute value of a numberACOS DR DR arccosine of a numberASIN DR DR arcsine of a numberATAN DR DR arctangent of a numberCOS DR DR cosine of an angle in radians

COSH DR DR hyperbolic cosineINT DIR I converts to the INTEGER data type by truncationLOG DR DR natural logarithm

LOG10 DR DR common logarithmMAX DIR,DIR,... DIR maximum value of argumentsMIN DIR,DIR,... DIR minimum value of argumentsMOD DIR,DIR DIR,DIR arg1 modulo arg2

REAL DIR R converts to the REAL data typeSIN DR DR sine of an angle in radians

SINH DR DR hyperbolic sineSQRT DR DR square rootTAN DR DR tangent of an angle in radians

TANH DR DR hyperbolic tangent

Page 37: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Function Meaning

Y = EXP (X) Exponential of the argument X; that is, the variable e to the power X.

Y = ALOG (X)Natural logarithm of the argument X. Attempting to take the logarithm of a negative number will cause the system to report an error.

Y = SIN (X) Sine of the argument X. Important: X is expressed in radians.

Y = COS (X) Cosine of the argument X. Important: X is expressed in radians.

Y = TAN (X) Tangent of the argument X. Important: X is expressed in radians.

Y = ASIN (X) Arcsine of the argument X. The result will be in radians, with a value between -pi and +pi.

Y = ACOS (X)Arccosine of the argument X. The result will be in radians, with a value between -pi and +pi.

Y = ATAN (X)Single-argument arctangent. Here Y is the arctangent of the argument X. The result will be in radians, with a value between -pi/2 and +pi/2.

Y = ATAN2 (X, Z)

Two-argument arctangent; the result is the arctangent of X/Z, in radians. The use of two arguments allows the determination of the quadrant of the angle, so that the result is between -pi and +pi rather than -pi/2 and +pi/2.

Page 38: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

ARRAYS:

When some variables represent a set of numbers, we can represent them as an array. The arrays are the same as ordinary variables in name and length and type either integer or real. There is single and double arrays.

Mathematical Notation FORTRAN Notation

a1, a2, ..., an A(1), A(2), ..., A(N)

b1,1, b1,2, ..., bm,n B(1,1), B(1,2), ..., B(M,N)

Page 39: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

I is subscript, which must be integerK is variable nameK(3) is the third element 20K(5) is the fifth element 29

K(I) 10 15 20 23 29 51

SINGLE ARRAY FORM:

Page 40: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

DOUBLE ARRAY FORM:

I is first subscriptJ is second subscript K is variable nameK(2,4) 2nd row 4th column 8K(3,2) 3rd row 2nd column 7

K(I,J) J1 J2 J3 J4 J5

I1 5 5 7 9 10

I2 12 7 8 8 2

I3 9 7 3 3 1

I4 1 12 25 7 9

Page 41: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

When we use arrays, we must introduce them in the beginning of the program and state their maximum size.

This statement is called DIMENSION statement.

DIMENSION T(20) , BMD(23) , Q(23) , STEEL(54) , QA(10,10)

Page 42: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

READING ARRAYS:

READ (5,10) (X(J), J = 1 , 7)IT MEANS READ X(I)

THEN READ X(2)THEN READ X(3)THEN READ X(4)THEN READ X(5)

READ(5,15) ((X(I,J), J = 1 , 2), I = 1,3) IT MEANS READ X(1, 1)

THEN READ X(1,2)THEN READ X(2,1)THEN READ X(2,2)THEN READ X(3,1)THEN READ X(3,2)

Page 43: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE:

Write a computer program to read and write up to 5 values of loads (Q).

DIMENSION Q(5) READ (5,5) MAX READ (5,10) (Q(I), I = 1 , MAX)

5 FORMAT (I2)10 FORMAT (5F5.2)

WRITE (6,10) (Q(I), I = 1 , MAX)END

Page 44: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Repetitive Instructions and DO Loops

When doing numerical calculations it is often necessary to do the same calculation numerous times. An example might be computing the solar elevation angle for each hour of the day. Fortran provides a standardized set of instructions - called a "DO loop" - for performing these repetitive calculations. An example of a repeated calculation using a DO loop could be a simple program to compute the number of elapsed seconds at each hour of the day, beginning at midnight:

PROGRAM MAININTEGER I, SECONDDO 10 I = 0, 24

SECOND = 3600 * IPRINT *, 'HOUR = ', I, ' SECONDS =', SECOND

10 CONTINUESTOPEND

Page 45: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

The statement beginning with DO controls the instructions that are to be repeated and the number of times they are repeated. We need to look closely at each part of the DO statement, from left to right:

DO 10 I = 0, 24•The word DO means that this line is the first of the statements that will be repeated. •The number 10 is the statement number of the last instruction to be repeated. Essentially, this tells the system "we will repeat all statements from here up to and including the statement that is numbered 10." This final statement is sometimes referred to as the terminal statement of the loop. •The letter I is called the index of the DO loop. By default, the index increases by 1 each time the loop is repeated. •The number following the equals sign is the initial value of the DO loop index. This is the value that is assigned to the index the first time the loop is executed. •The number following the comma is the limit of the DO loop. Once the index exceeds the limit, the loop is no longer repeated.

Page 46: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

SUM = 0 DO 10, I = 1,100

SUM = SUM + I10 CONTINUE

DO 20, I = 0,100,3 WRITE(*,*)I

20 CONTINUE

Useful examples:

Page 47: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

DO 200 I = 1, 100DO 100 J = 1, 100

WRITE(*,*) I, J100 CONTINUE200 CONTINUE

DO 10 I = 0, 24 IF (I .GE. 10) THEN

PRINT *, 'THE TIME IS', I ELSE

END IF10 CONTINUE

Page 48: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

GO TO STATEMENT: When we want to go into a line inside the program and pass many lines without execution, we use the GO TO statement.

GO TO 90 It means go directly to 90

EXAMPLE:DIMENSION Q(5)READ (5,6) MAXREAD (5,10) (Q(l), I = 1 , MAX) GO TO 20

6 FORMAT (I5)10 FORMAT (5F10.2)

WRITE (6,10) (Q(l), I = 1 , MAX)20 CONTINUE

END

This program will read the values of Q(l), BUT will not print them.

Page 49: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

COMPUTED GO TO STATEMENT:

When we want to go into a specific position inside the program based on a specific condition, we may use the computed go to statement.

GO TO (10,15,40,100),K

It means that, when:

K = 1 GOTO 10K = 2 GOTO 15 K = 3 GOTO 40 K = 4 GOTO 100

Page 50: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

ARITHMATIC IF STATEMENT:

When we want to go into a line inside the program based on a specific condition, we use the IF STATEMENT.

IF (Expression) K, L , M

It means that:

When the expression < 0 GO TO (K) When the expression = 0 GO TO (L) When the expression > 0 GO TO (M)

Page 51: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE:

BM1 = 7BM2 = 12IF (BM1 - BM2) 10 , 10 , 20

It means IF (BM1 BM2) is negative value or zero, then go to statement 10, while IF (BM1 BM2) is positive value, then go to statement 20.

In our case, BM1 — BM2 = 7 —12 = - 5, this means GO TO 10

Page 52: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Nesting

Nesting DO loops is common, particularly when manipulating multi-dimensional arrays. Consider this program fragment which performs matrix-vector multiplication:

... DO 20, I = 1,M

SUM = 0.0 DO 10, J = 1,N

SUM = SUM + MATRIX(I,J)*VECTOR(J)10 CONTINUE

PRDCT(I) = SUM20 CONTINUE

...Note that the inner 'DO 10' loop is completely contained within the body of the outer 'DO 20' loop. Indentation helps distinguish the nesting occurring in the code.

Page 53: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

LOGICAL IF STATEMENT:

When we want to perform a specific statement based on a condition if TRUE or FALSE, we use the logical if statement.

We use the following while evaluating the IF statement:.EQ. MEANS EQUALS TO (=).NE. MEANS NOT EQUAL (≠).GT. MEANS GREATER THAN (>).LT. MEANS LESS THAN (<).LE. MEANS LESS OR EQUAL (≤).GE. MEANS GREATER OR EQUAL (≥).OR. MEANS OR.AND. MEANS AND

Page 54: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE:

X = 5.0Y = 22.0 Z = 12.0 IF ((X.EQ.Y) .OR. (Y.LT.Z) GO TO 90

In the above lines, IF (X = Y) or IF (Y< Z), then GO TO 90

So, for the above values of X, Y and Z; The program will not go direct into 90

Page 55: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

BLOCK IF STATEMENT:

IF (TEST CONDITION) THEN

Perform statements if test is TRUE

ELSE

Perform other statements if test is FALSE

END IF

Page 56: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE:

C PROGRAM TO CHECK TENSION SIDE OF A CONTINUES BEAMC BMV MEANS BENDING MOMENT VALUEC TENSION IS AT TOP, FOR NEGATIVE MOMENT VALUESC TENSION IS AT BOTTOM, FOR POSITIVE MOMENT VALUES10 PRINT *, 'ENTER THE BENDING MOMENT VALUE'

READ * , BMVIF (BMV.LT.0.0) THENPRINT * , 'TENSION IS AT THE TOP OF THE BEAM'ELSEPRINT * , 'TENSION IS AT THE BOTTOM OF THE BEAM'END IF

Page 57: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE:

WRITE A PROGRAM TO READ THE GRADES OF MID TERM EXAM AND FIND THE LOWEST GRADE AND THE ID NUMBER OF THE STUDENT WHO GOT IT.

DIMENSION GRADE(50)C READ THE GRADES OF THE CLASS BASED ON ID NUMBERC N IS THE NUMBER OF STUDENTS AT THE CLASS

READ (*,5) N 5 FORMAT (I2)C READ THE GRADE FOR EACH STUDENT

READ (*,10) (GRADE(I), I = 1, N)10 FORMAT (F5.1)

ID = 1SMALL = GRADE(1) K=0

Page 58: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

EXAMPLE (cont.):

6 K=K+1IF (GRADE(K) - SMALL) 9 , 9 , 100

9 SMALL = GRADE(K)ID = K

100 IF (K- N) 6 , 200 , 200200 WRITE (*,90) SMALL , ID 90 FORMAT (F5.1, I2)

END

Page 59: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

A subroutine defines a complete process and is self contained. It has an initial SUBROUTINE statement, a specification part, an execution part that comprises the algorithm, any internal procedures that perform ancillary processes, and an END statement. When a subroutine is invoked, its execution begins with the first executable construct in the subroutine. Data objects and other entities may be communicated to and from the subroutine through argument association, host association, use association, or common storage association.

Rules and restrictions:1. If the END statement contains a subroutine name, it must be the same name as that in the SUBROUTINE statement.2. An internal subroutine must not contain an internal subprogram part.3. An internal subroutine must not contain ENTRY statements.4. The END statement of an internal or module subroutine must be END

Subroutines

Page 60: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

PROGRAM MAININTEGER AREAL F,R,X,YCOMMON R,A,FA = -14B = 99.9F = 0.2CALL SUB(X,Y)...END

SUBROUTINE SUB(P,Q)INTEGER IREAL A,B,P,QCOMMON A,I,B...END

Page 61: Fortran Course

EXAMPLEPROGRAM BEAM C SPAN = L C UNIFORM LOAD W C STEP ==> 0.01 L C BENDING MOMENT=BM

REAL LPRINT*,' SPAN = ' READ (*,*) L PRINT*,' UNIFORM LOAD = 'READ (*,*) W X = 0.0 CALL BMD(X,BM)X = X + 0.01*L IF (X .GT. L) GO TO 20

20 WRITE(*,200) 200 FORMAT (‘===============================‘)

END

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 62: Fortran Course

SUBROUTINE BMD(X,BM)BM1 = W*X*X/2.0 BM2 = W*L*X/2.0BM = BM1 + BM2 WRITE(*,100) X,BM1,BM2,BM

100 FORMAT(‘ X = ',F5.2,' BM1 = ',F7.3,' BM1 = ‘,$ F7.3,' BM1 = 'F7.3,' TM.')

RETURNEND

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 63: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

FunctionsA function is similar to a subroutine, except that its principal use is as a primary in an

expression. Analogous to a subroutine, a function has an initial FUNCTION statement, a specification part, an execution part, possibly internal procedures, and an END statement. An argument list provides data communication with the function, but in this case arguments typically serve as input data for the function. The principal output is delivered as the functionresult to the expression invoking the function. Data objects also may be available to the unction via host association, use association, and common storage association.

Rules and restrictions:1. The type of the function may be specified in the function statement or in a type declaration statement, but not both. If the type is not explicitly specified in this way, the default typing rules apply.2. If the function is array valued, or a pointer, the declarations must state these attributes for the function result name. The function result name may be declared to be an explicit shape or a deferred shape array. 3. Dummy argument attributes may be specified explicitly in the body of the function or may be declared implicitly. Each dummy argument is a local variable of the function; therefore, its name must be different from that of any other local variable in the function.

Page 64: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

4. If the END statement contains the function name, it must be the same name used in the FUNCTION statement.5. An internal function must not contain an internal subprogram part.

Example

c ==================== ==============================PROGRAM Inx INTEGER n PARAMETER ( n=3 ) REAL p(0:n),x,w,w2,ln1 ,ln2 p(3)=0.48674609 p(2)=0.35370773 p(1 )=0.6704 p(0)=2

10 READ (*,*) x IF ( x .NE. 0 ) THEN w=(x-1 )/(x+1) In1=log(x) w2=w*w In2=w*poly(p,n,w2) WRITE(*,*) 'x : ',x WRITE(*,*) ‘Approximate : ',ln2 WRITE(*,*) ‘Call of In(x) : ',ln1

Page 65: Fortran Course

IF ( x .NE. 1 ) WRITE(*,*) ‘Error’

& abs(ln2-1n1 )/abs(ln1 )

WRITE(*,*)

GOTO 10

ENDIF

END

REAL FUNCTION poly(p, n, x)

INTEGER n, i

REAL p(0:n), x

poly=p(n)

DO 10 i=n-1,0,-1

10 poly=poly*x + p(i)

END

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

Page 66: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

c =====================================PROGRAM curve REAL a,b,c,d,e,gCHARACTER art DO 10 i=1,2

READ ( *,100) art 100 FORMAT(a1)

READ(*,*) a,b,c,d,e,gCALL graph(art,a,b,c,d,e,g)

10 WRITE(*, '(////)’)END

ADVANCED EXAMPLE

Page 67: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

SUBROUTINE graph(art,a,b,c,d,e,f,g)INTEGER teil,anzREAL inc,xCHARACTER art IF ( art .EQ. 'b') THEN

teil=50 inc=6.5/19

ELSE teil=128 inc=6.5/64

ENDIFi=l WRITE(*,100) ('-',i=1 ,teil)

100 FORMAT(128a) x=0

10 x=x + inc IF ( x .LE. 6.5 ) THEN

fx=f(a,b,c,d,e,g,x)anz=int(teil*fx/22)

Page 68: Fortran Course

Alexandria UniversityFaculty of Engineering

Structural Engineering Department

IF ( x .LE. 6.5 ) THEN fx=f(a,b,c,d,e,g,x)anz=int(teil*fx/22) IF ( anz .EQ. 0 ) THEN

WRITE(*,*)ELSE

WRITE(*,*) ‘I’,(‘ ‘,i=1,anz),’*’ENDIFGOTO 10

ENDIFEND

REAL FUNCTION f(a,b,c,d,e,g,x) REAL a,b,c,d,e,g,xf=a+x*(b+x*(c+x*d))+e/(x+g)END