programming fortran 77 slide01

37
CIVIL HTI Fortran 77 Series Lectures Dr. A.M.K. Shaltout 2016

Upload: ahmed-gamal

Post on 22-Jan-2018

536 views

Category:

Engineering


10 download

TRANSCRIPT

Page 1: programming fortran 77 Slide01

CIVIL HTI

Fortran 77

Series Lectures

Dr. A.M.K. Shaltout

2016

Page 2: programming fortran 77 Slide01
Page 3: programming fortran 77 Slide01

Variables,Types and Declarations

Variable NamesVariable names in Fortran consist of any letters from a-z or digits 0-9

Fortran 77 does not distinguish between upper and lower case

Types and declarationsThe most common declarations are:

Integer characters: I, J, K, L, M, N

integer list of variables

real list of variables

Examples:

VOL TEMP A2 COLUMN IBM370

Page 4: programming fortran 77 Slide01
Page 5: programming fortran 77 Slide01
Page 6: programming fortran 77 Slide01

Column position rules :

Fortran 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules:

Col. 1 : Blank, or a "c" or "*" for comments

Col. 2-5 : Statement label (optional)

Col. 6 : Continuation of previous line (optional)

Col. 7-72 : Statements

Page 7: programming fortran 77 Slide01

Statement Format

FORTRAN 77 requires a fixed format for programsPROGRAM MAIN

C COMMENTS ARE ALLOWED IF A “C” IS PLACED IN COLUMN #1

DIMENSION X(10)

READ(5,*) (X(I),I=1,10)

WRITE(6,1000) X

1000 FORMAT(1X,’THIS IS A VERY LONG LINE OF TEXT TO SHOW HOW TO CONTINUE ’

* ‘THE STATEMENT TO A SECOND LINE’,/,10F12.4)

1-5

Label

6 7-72 Statements 73-80

Optional

Line #sAny character: continuation line

Page 8: programming fortran 77 Slide01
Page 9: programming fortran 77 Slide01

Flowchart Symbols Basic

Oval

Parallelogram

Rectangle

Diamond

Hybrid

Name Symbol Use in Flowchart

Denotes the beginning or end of the program

Denotes an input operation

Denotes an output operation

Denotes a decision (or branch) to be made.

The program should continue along one of

two routes. (e.g. IF/THEN/ELSE)

Denotes a process to be carried out

e.g. addition, subtraction, division etc.

Flow line Denotes the direction of logic flow in the program

Page 10: programming fortran 77 Slide01

Pseudocode & Algorithm

Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Page 11: programming fortran 77 Slide01

Pseudocode & Algorithm

Pseudocode:

Input a set of 4 marks

Calculate their average by summing and dividing by 4

if average is below 50

Print “FAIL”

else

Print “PASS”

Page 12: programming fortran 77 Slide01

Pseudocode & Algorithm

Detailed Algorithm

Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then

Print “FAIL”

else

Print “PASS”

endif

Page 13: programming fortran 77 Slide01

Example

PRINT

“PASS”

Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4

Step 3: if (GRADE <50) then

Print “FAIL”

else

Print “PASS”

endif

START

Input

M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

IS

GRADE<5

0

PRINT

“FAIL”

STOP

YN

Page 14: programming fortran 77 Slide01

Example 2

Write an algorithm and draw a flowchart to convert the length in feet to centimeter.

Pseudocode:

Input the length in feet (Lft)

Calculate the length in cm (Lcm) by multiplying LFT with 30

Print length in cm (LCM)

Page 15: programming fortran 77 Slide01

Example 2

Algorithm

Step 1: Input Lft

Step 2: Lcm Lft x 30

Step 3: Print Lcm

START

Input

Lft

Lcm Lft x 30

Print

Lcm

STOP

Flowchart

Page 16: programming fortran 77 Slide01

Example 3

Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.

Pseudocode

• Input the width (W) and Length (L) of a rectangle

• Calculate the area (A) by multiplying L with W

• Print A

Page 17: programming fortran 77 Slide01

Example 3

Algorithm

• Step 1: Input W,L

• Step 2: A L x W

• Step 3: Print A

START

Input

W, L

A L x W

Print

A

STOP

Page 18: programming fortran 77 Slide01

Example 4

• Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation

• Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

2 0ax bx c 2 4b ac

Page 19: programming fortran 77 Slide01

Example 4

Pseudocode:

• Input the coefficients (a, b, c) of the quadratic equation

• Calculate d

• Calculate x1

• Calculate x2

• Print x1 and x2

Page 20: programming fortran 77 Slide01

Example 4

• Algorithm: • Step 1: Input a, b, c

• Step 2: d sqrt ( )

• Step 3: x1 (–b + d) / (2 x a)

• Step 4: x2 (–b – d) / (2 x a)

• Step 5: Print x1, x2

START

Input

a, b, c

d sqrt(b x b – 4 x a x c)

Print

x1 ,x2

STOP

x1 (–b + d) / (2 x a)

X2 (–b – d) / (2 x a)

4b b a c

Page 21: programming fortran 77 Slide01

Program Organisation

A Fortran program generally consists of a main program (or driver) and possibly several subprograms (or procedures or subroutines). For now we will assume all the statements are in the main program; subprograms will be treated later. The structure of a main program is:

program name

declarations

statements

stop

end

Page 22: programming fortran 77 Slide01
Page 23: programming fortran 77 Slide01
Page 24: programming fortran 77 Slide01

Create a new text file

Page 25: programming fortran 77 Slide01
Page 26: programming fortran 77 Slide01
Page 27: programming fortran 77 Slide01
Page 28: programming fortran 77 Slide01
Page 29: programming fortran 77 Slide01
Page 30: programming fortran 77 Slide01
Page 31: programming fortran 77 Slide01
Page 32: programming fortran 77 Slide01
Page 33: programming fortran 77 Slide01
Page 34: programming fortran 77 Slide01
Page 35: programming fortran 77 Slide01
Page 36: programming fortran 77 Slide01

Numeric Expressions

Very similar to other languages Arithmetic operators:

Precedence: **

Examples:

x = 3 + 4

y = a*b

z = radius ** 2

z=x-y

z=x/y

Page 37: programming fortran 77 Slide01