lecture1, introduction to fortran

12
1 Dr. Assakkaf Slide No. 1 • A. J. Clark School of Engineering • Department of Civil and Environmental Engineering ENCE 202 FORTRAN Handout 1  Introduction to FORTRAN by Dr. Ibrahim A. Assa kkaf ENCE 202 Spring 2000 Department of Civil and Environmental Engineering University of Maryland Dr. Assakkaf Slide No. 2 • A. J. Clark School of Engineering • Department of Civil and Environmental Engineering ENCE 202 FORTRAN Handout 1  Introduction n FORTRAN  FORTRAN = FORmula TRANslation  Develope d for the I BM 704 Co mputer  – Develo ped by John Backus and a team of 13 other programmers  – Developed between 1954 an d 1957

Upload: wtstreetglow

Post on 07-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 1/12

Dr. Assakkaf

Slide No. 1

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Introduction to FORTRAN 

by

Dr. Ibrahim A. Assakkaf

ENCE 202

Spring 2000

Department of Civil and Environmental Engineering

University of Maryland

Dr. Assakkaf

Slide No. 2

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Introduction

n FORTRAN

 – FORTRAN = FORmula TRANslation

 – Developed for the IBM 704 Computer

 – Developed by John Backus and a team of13 other programmers

 – Developed between 1954 and 1957

Page 2: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 2/12

Dr. Assakkaf

Slide No. 3

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Introduction

n FORTRAN is a high-level languagesuch as BASIC, C, and C++

n A compiler translates each statement inthe program into a sequence of basicmachine language instruction

C  B A X  += *

Dr. Assakkaf

Slide No. 4

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Introduction

Compiler

Source Program(high level language)

Object Program(machine language)

Page 3: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 3/12

Dr. Assakkaf

Slide No. 5

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Program Development

n A program consists of

 – Input

 – An execution logic (computation)

 – Output

n A flow chart is used to develop thestructure

Dr. Assakkaf

Slide No. 6

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Program Development

n EXAMPLE 1: Volume of a Cylinder – Input:

• Diameter, D 

• Height, h 

 – Computation•

 – Output:• Volume, V 

h

 D

V  ×  

 

 

 

 

= 4

Page 4: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 4/12

Dr. Assakkaf

Slide No. 7

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Program Development

n Flow Chart for Example 1

 – A flow chart is a block diagram that summarizesthe program structure and logic flow

BEGINENTER:D and h 

CALCULATE:V 

h D

V   

 

 

 

 =

4

OUTPUT:Display

Volume(V )END

Dr. Assakkaf

Slide No. 8

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Program Development

n EXAMPLE 2: Height of a Cylinder – Input:

• Diameter, D 

• Volume, V 

 – Computation•

 – Output:• Height, h 

2

4

 D

=

Page 5: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 5/12

Dr. Assakkaf

Slide No. 9

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Program Development

n Flow Chart for Example 2

 – A flow chart is a block diagram that summarizesthe program structure and logic flow

BEGINENTER:

D and V 

CALCULATE:V 

OUTPUT:DisplayHeight(h )

END

2

4

 D

V h

π

=

Dr. Assakkaf

Slide No. 10

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN 

n The following is a format of a simpleprogram:

• PROGRAM NAME

• Opening documentation use

• Variables declaration

• Program statements• END

Page 6: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 6/12

Dr. Assakkaf

Slide No. 11

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n FORTRAN Statements• Positions 7 to 72 of a line can be used. To

continue on the next line, use any character onposition 6 of the next line

n Constants – Examples

• -12

• 1.2345• 12.45-e10

Dr. Assakkaf

Slide No. 12

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1

 

1 2 3 4 5 6 7 81234567890123456789012345678901234567890123456789012345678901234567890 1234567890C

Statement in columns 7 to 72

Statement labels in columns 1 to 5

Character different from 0 or blank in column 6

Indicates a continuation of the preceding line

C or * in column 1 indicates a comment

 Fundamentals of FORTRAN

Column 73

And beyond

Are ignored

Page 7: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 7/12

Dr. Assakkaf

Slide No. 13

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n Data Types

 – Integer

 – Real

 – Double Precision

 – Complex

 – Character

 – Logical

Dr. Assakkaf

Slide No. 14

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN

n Variables – Variables starts with a letter

 – The first letter defines the variable type• Integer or real

 – The default types are• I, J, K, L, M, and N

• These are integers variables if they are used asthe first letter in names

• Otherwise, they are treated as real variables

Page 8: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 8/12

Dr. Assakkaf

Slide No. 15

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n Type Statements

 – REAL list of variables 

 – INTEGER list of variables 

 – CHARACTER*n list of variables with size 

n (default n = 1)

 – PARAMETER (param 1 = value 1, param 2 = 

value 2 , ….etc ) – DATA list 1 /data 1 /, list 2  /data 2  /, , list n  /data n ? 

Dr. Assakkaf

Slide No. 16

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN

n Examples: Type statementsREAL X, Speed, Height

INTEGER I, M, Q

CHARACTER*2 Var1, Var2

PARAMETER (PI = 3.14159, E = 2.71)

DATA w/1.3/, x,y/2.3, 5.6/ 

DATA w,x,y/1.3, 2.3, 5.6

Page 9: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 9/12

Dr. Assakkaf

Slide No. 17

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n Arithmetic Operations

 – Addition +

 – Subtraction -

 – Multiplication *

 – Division /  

 – Exponentiation **

Example:Area = (3.14)*(D**2)/4

Dr. Assakkaf

Slide No. 18

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN

n Priority Rules

** from right to left

* and / from left to right

+ and - from left to right

NOTE:

To avoid confusion, always use parentheses

Page 10: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 10/12

Dr. Assakkaf

Slide No. 19

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n Standard Library Functions

 – Examples

• SQRT (argument), Square Root

• ABS (x), Absolute value

• COS (y) Cosine of argument

• LN(x) Natural Logarithm

• Etc.

Dr. Assakkaf

Slide No. 20

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN

n Assignment Statements

Variable = expression

 – Examples

x = 0.234

y = sqrt(x) / 2 + height**3

Page 11: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 11/12

Dr. Assakkaf

Slide No. 21

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Fundamentals of FORTRAN

n List-directed Input and Output

PRINT *, output-list (separators are commas )

n Examples:PRINT * è (print a blank line)

PRINT *, ‘Test, Test’ è Test, Test

PRINT *, ‘Interest’ =, Int è Interest = 5

Dr. Assakkaf

Slide No. 22

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Fundamentals of FORTRAN

n List-directed Input and Output

READ *, input-list (separators are commas )

n Examples:PRINT *, ‘Enter Velocity and Time?’

READ *, ‘VELOC0, TIME

READ *, Int, ANote: It is a good practice to prompt the user that

input is needed, using PRINT statement beforeREAD with prompt information

Page 12: Lecture1, Introduction to Fortran

8/3/2019 Lecture1, Introduction to Fortran

http://slidepdf.com/reader/full/lecture1-introduction-to-fortran 12/12

Dr. Assakkaf

Slide No. 23

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202

FORTRAN

Handout 1 Example: Simple Program

n This program calculates the velocity andheight of a projectile given its initialheight, velocity,and constantacceleration

0

002

 :Velocity

2

1 :Height

vat v

ht vat h

+=

++=

Dr. Assakkaf

Slide No. 24

• A. J. Clark School of Engineering • Department of Civil and Environmental Engineering

ENCE 202FORTRAN

Handout 1  Example: Simple Program

PROGRAM PROJECT

C Th is program calculates the velocity and height of a projectile given itsC initial height, initial velocity, and constant acceleration. Variables used are:

C HGHT : initial height

C HGHT : height at any timeC VELOC : initial vertical velocityC VELOC : vertical velocity at any time

C ACCEL : vertical acceleration (Gravity)C TIME : time elapsed since projectile was launched

REAL HGHT , HGHT, VELOC , VELO, ACCEL, TIME

ACCEL = - .

PRINT *, `Enter initial height, initial velocity, and time?`READ *, HGHT , VELOC , TIME

HGHT = . * ACCEL * TIME ** +VELOC * TIME + HGHTVELOC = ACCEL * TIME + VELOC  

PRINT *, `AT TIME `, TIME, ` THE VERTICAL VELOCITY IS `, VELOCPRINT *, `AND THE HEIGHT IS `, HGHTEND