Transcript Document

FORTRAN 77
Programming.
Lecture 1 : January 2001
Dr. Andrew Paul Myers
18/07/2015
1
Course Methods.








18/07/2015
Lectures.
Handout notes.
Supervised practical work.
Daily submission of exercises.
Daily feedback on exercises.
Model Answers.
Final project.
Weekly clinic for final Project.
2
Introduction.



A course in FORTRAN 77.
Why FORTRAN?
Objectives :
•
•
•
•

General education in computing.
Introduction to programming.
First write a simple program, then…
Write complex program.
Course Benefits :
• I.T. is a part of all our lives and getting more
so!
• FORTRAN and computing skills for other
courses.
• Future employment.
18/07/2015
3
Tools For The Job.

Programming Environment :






18/07/2015
CFS system and Exceed 6.2.
Unix system (SG IRIX 6.5).
Silicon Graphics Desktop
Windows Text Editor (emacs).
FORTRAN 77 Compiler (Mips 7.2.1).
Workshop Debugger (cvd).
4
Programming
Languages.






You are users!
How computers work.
Machine code.
High level languages.
Fortran : FORMula TRANslation.
Other languages :
• Basic
• C/C++
• Pascal
18/07/2015
5
Programming Cycle.







18/07/2015
Analyse the task.
Plan the program, a structured
approach!
Flowcharts & Dry running.
Edit your source code.
Compile and link program.
Execute and debug program.
Edit and recompile as necessary.
6
IRIX f77 Compiler.

Edit source program (*.f) with
“emacs” editor. Save file.

Compile and run on Unix command
line in a shell window :
/disk/n/gps> f77 –o test test.f
/disk/n/gps> test
18/07/2015
7
Simple Structure of A
FORTRAN Program.






18/07/2015
Program name.
Declare variables and structures.
Assign values to variables.
Process data.
Print results.
End program.
8
Flow of a Program.







18/07/2015
Linear sequence.
One command per line.
Position on line : Very Important!
Comments Statements (ignored).
Repetition : Loops.
Selections : Conditional statements.
Always finish with an END statement.
9
Position on a line.

1 2-5
C
9 9999
18/07/2015
The layout of FORTRAN program
dates back to old 80 column punched
cards, which were used for program
input.
6
7-72
Total=x_value+y_value
& +z_value
73-80
Comment line.
FORMAT(‘Answer =‘,I4)
10
Variable Declarations.

Variable names :




18/07/2015
Must be at least one alphabetic
character long, up to a maximum of 31
alphanumeric characters.
Must start with an alphabetic
character. Case insensitive.
Alphanumeric characters are : a-z, 0-9
and the underscore ( _ ).
Implicit variables. I to N integers!
11
Examples.

Valid names :
•
•
•
•

Invalid names :
•
•
•
•
18/07/2015
X
THEDAY
Min_cur
Time28
X*Z
THE TIME
7YEARS
_no_way$
12
Basic Data Types.





REAL
INTEGER
COMPLEX
LOGICAL
CHARACTER
x=5.0
i=20
z=(1.4,3.2)
test=.TRUE.
char=‘Hello’
More advanced data types can be
made from these basic types.
18/07/2015
13
Declarations.
<Data Type> <variable> [,<variable(s)>]
e.g.
REAL x
REAL radius,volume
INTEGER loop,temp
CHARACTER string*10,name*30
18/07/2015
14
Parameters.

Parameters are constants, their value,
once defined, can not be changed.
REAL g,pi
INTEGER days
PARAMETER (days=365)
PARAMETER (g=9.81,pi=3.142)
18/07/2015
15
Assignments.
<variable> = <value> | <variable> | <expression>
radius=2.5
y=z
test=value+loop-temp
volume=(4.0*pi*radius**3.0)/3.0
Expressions follow the BODMAS
precedence rule. Operators +, -, *, / and **
18/07/2015
16
Control Structures.
Basic building blocks of programs.
They control the flow of the program.
There are 3 different types :



18/07/2015
Linear Sequence.
Selection.
Iteration or Loop.
17
Other Statements.

PROGRAM [ program name ]
END

C or *



A comment.
PRINT*,’Hello’
PRINT*,’Value of X = ‘,x
This is free format output.
18/07/2015
18
Data Input.



18/07/2015
Programs are useless without data!
Use the READ statement to allow
users to input data.
Prompt user for the data too!
e.g.
PRINT*,’Enter values for x & y :’
READ*,x,y
19
Character Input.

A normal read statement can not be
used to enter character variables. Use
the following:
PRINT*,’Continue (y/n) : ‘
READ ‘(A1)’,yes_or_no
‘(A<n>)’ – <n> is the number of characters.
18/07/2015
20
Good Programming
Style.






18/07/2015
Comment your program!
FORTRAN keywords in upper case.
Variables in lower case.
Use descriptive variable names.
Blanks may be used to improve
readability.
Indent code with “tabs”.
21
General Program
Layout.
PROGRAM [ program name ]
[ comments ]
[ declaration statements ]
[ executable statements ]
STOP
END
18/07/2015
22
References.
A Crash Course in FORTRAN 77.
Donald M. Monro.
(Edward Arnold).
Irix Insight On-line Help.
SGI Developer Section.
MIPSpro FORTRAN 77
Programmer’s guide.
18/07/2015
23