Download - 234136-Fortran 77 With Mts
-
7/26/2019 234136-Fortran 77 With Mts
1/307
https://books.google.co.in/books?id=m-MmAAAAMAAJ -
7/26/2019 234136-Fortran 77 With Mts
2/307
1.390.750
-
7/26/2019 234136-Fortran 77 With Mts
3/307
r . i:
; ta
'
-
7/26/2019 234136-Fortran 77 With Mts
4/307
-
7/26/2019 234136-Fortran 77 With Mts
5/307
-
7/26/2019 234136-Fortran 77 With Mts
6/307
FORTRAN 77
With
MTS and
the IBM PC
by
Brlce Carnahan
James O. Wilkes
College of Engineering
The University of Michigan
Ann
Arbor
1988
-
7/26/2019 234136-Fortran 77 With Mts
7/307
Copyright
1988
by Brice Caraahan and James O. Wilkes
Al l rights reserved. This
book
or any
part
thereof
must not be reproduced in
any
form
without the written permission of the authors.
-
7/26/2019 234136-Fortran 77 With Mts
8/307
pV\crk_
PREFACE
This book,
together with i t s companion volume,
The
IBM PC
and The
Michigan Terminal System,
has
been
written
primarily f o r the introductory
fresh
man engineering digital-computing course, Engr. 103, at the University of
Michi
gan.
I t
should
also
be
useful
to
others
who
wish
to learn
about
the
FORTRAN-
77
language
and apply
i t
to their own computing problems.
Chapters 1-3 and 6-11, being concerned with
FORTRAN
77 exclusively, are
of
general i n t e r e s t .
Chapters 4
and 5 are of more local
appeal,
since they discuss
compilation and execution i n specific
situations
otably
with
MS
FORTRAN on
the IBM
PC and with VS FORTRAN on the IBM
3090-600
mainframe computer
at the
University
of
Michigan. Two
features
that should prove
helpful are the
sub
stantial number
of
example programs, and
the
end-of-section exercises, solutions
for
a l l
of
which
appear i n Appendix
A.
We wish
to
thank a l l
those
who have shared their computing knowledge with
us, particularly including Jim
Rennell and
Pankaj
Shah, who have
done
excellent
work in
maintaining
the Engr. 103 3Com Ethernet local-area network
i n f i n e
shape.
The assistance of L i l i
Kokkinakos
and
Laurie
Breck
i n
helping to prepare the
f i n a l version of the
book i s much appreciated.
So,
too, i s
the highly s k i l l f u l and
untiring
e f f o r t of
Mich Rasulis, both i n
working with the typesetting program and
in drawing
the
f i g u r e s .
For the technically
minded,
t h i s book was composed on an IBM
PC-
AT
computer
using
the TgX
typesetting
program, and was printed on
an
Apple
LaserWriter
printer. Eleven-point type was used f o r the majority of the
t e x t .
The
figures
were
drawn
using the
MacDraw
and
CricketDraw
programs
on
a
Macintosh
computer.
Inquiries about this text should be
directed
to u s , at the
Chemical
Engineer
ing
Department, The
University
of Michigan, Ann
Arbor,
MI 48109-2136.
Finally,
students enrolled i n Engr . 103 at
the
University of
Michigan
are welcome to come
to our o f f i c e , 3146 Dow Building, i f they have questions about t h i s book or
any
other
aspects
of
the
course.
Brice
Carnahan
James
O.
Wilkes
September
1 , 1988
-
7/26/2019 234136-Fortran 77 With Mts
9/307
-
7/26/2019 234136-Fortran 77 With Mts
10/307
TABLE OF CONTENTS
Page
PREFACE i
TABLE
OF CONTENTS
i i i
CHAPTER 1INTRODUCTION TO COMPUTERS AND FORTRAN
1 . 1 Introduction
to Digital Computers
1- 1
1 . 2 Development
of
the FORTRAN
Language 1-6
1 . 3 Analysis
of
a FORTRAN
Program 1-9
Example Program
1 . 1
riangle Problem,
First
Version
1-11
Example Program
1 . 2
riangle
Problem,
Second
Version
1-14
1.4 Fundamental
Concepts
and Notation
1-19
1 . 5 Three Statements: GO TO, STOP, and END 1-22
CHAPTER 2ARITHMETIC STRUCTURES
2 . 1 Introduction
2-1
2.2 Types: Integer
and
Real 2-2
2.3 Constants 2-3
2.4 Variables .2-5
2.5 Function References 2-7
2.6 Operators
and
Expressions 2-10
2.7 Mixed-type Expressions 2-15
2.8 Assignment Statement 2-17
CHAPTER
3INPUT AND OUTPUT
3.1 Introduction
3-1
3.2 List-Directed READ and WRITE Statements 3-2
Example Program
3 . 1
ist-Directed
I/O
3-5
Example Program 3.2ist-Directed I/O 3-6
3.3 Formatted READ Statement 3-8
i i i
-
7/26/2019 234136-Fortran 77 With Mts
11/307
3.4
The FORMAT
Declaration 3-9
3.5
The Formatted
WRITE Statement
3-17
3.6
Literal Fields
3-19
3.7
Carriage
Control
3-20
Example Program
3.3Formatted I/O 3-22
CHAPTER 4RUNNING FORTRAN-77
PROGRAMS
ON THE IBM PC
4.1 Introduction 4-1
4.2
Compilation,
Linking,
and
Execution 4-1
4.3 The Microsoft
FORTRAN-77
Compiler/Linker
f o r
the
IBM
PC
4-2
4.4 FORTRAN-77 i n the FEC Laboratories 4-6
Example Compilation/Execution 4 . 1
4-7
4.5 Allocation
of
I/O Units 4-8
Example Execution 4.2/O Assignments 4-9
4.6 Terminating Execution 4-10
4.7 Error Messages 4-11
Example Program 4.3
iagnostics
4-11
CHAPTER 5RUNNING
FORTRAN-77
PROGRAMS UNDER MTS
5 . 1
Introduction
5-1
5.2
Compilation 5-1
5.3
Execution
5-3
5.4
Example
MTS VS-FORTRAN 77 Compilation
and
Execution
5-5
Example 5 . 1
TS Compilation/Execution
5-5
5.5
Error Messages
5-10
CHAPTER6LOGICAL AND CONDITIONAL STRUCTURES
6.1
Introduction 6-1
6.2 Relational Operators and Simple Logical
Expressions
6-3
6.3 Logical Operators
and Compound Logical Expressions 6-5
6.4
Logical
Assignment Statement 6-8
6.5
Complete Precedence Table f o r
FORTRAN
Operators
6-11
6.6
The
Computed GO TO Statement 6-12
6.7
The Block
I F
( IF/THEN/ELSE)
Structure
6-14
6.8 Other Conditional Statements
(Logical
and Arithmetic
IF Statements)
6-20
6.9
Example Program
6.1
oots
of
a
Quadratic
Equation
6-24
IV
-
7/26/2019 234136-Fortran 77 With Mts
12/307
CHAPTER 7SUBSCRIPTED VARIABLES AND ITERATION
7.1 Introduction
7-1
7.2
Storage
of
Arrays
and
the
DIMENSION
Declaration 7-2
7.3 Iteration: The
DO and CONTINUE
Statements 7-4
7.4 Several Examples Involving Iteration 7-10
Example
Program 7 . 1able
of
Functions 7-10
Example Program
7.2
rray
Manipulation 7-13
Example Program
7.3orting
the
Elements
of
a V ec tor 7-15
CHAPTER 8
ADDITIONAL
INPUT
AND OUTPUT
FEATURES
8 . 1 Introduction
8-1
8.2 The
OPEN and
CLOSE Statements
8-1
8.3 Reading
and
Punching
Cards
8-2
8.4
The G , L , and T Format Codes 8-3
8.5 Formatted Array Input and Outputntroduction 8-5
8.6 Examples of Array Input and Ouput 8-9
8.7 File-Control
Statements:
BACKSPACE,
REWIND
and ENDFILE
8-20
Example Program
8 . 1
ile-Control Statements
8-20
CHAPTER9ADDITIONAL DECLARATIONS
9.1
Introduction
9-1
9.2
The DATA
Declaration
9-1
9.3
Type
Declarations 9-3
9.4 The
EQUIVALENCE Declaration 9-10
9.5 Order
of
Statements i n a FORTRAN
Program
9-12
CHAPTER
10
FUNCTION AND
SUBROUTINE
SUBPROGRAMS
10.1 Introduction 10-1
10.2 FUNCTION Subprograms 10-5
Example Program 10.1unction f o r Array Averages 10-8
10.3 The Dimensioning of Arrays i n Subprograms 10-17
10.4 SUBROUTINE Subprograms 10-20
10.5 Multiple Entry PointsThe ENTRY Statement 10-26
10.6
The
INTRINSIC and EXTERNAL
Declarations
10-28
Example
Program 10.2EXTERNAL and INTRINSIC
Declarations 10-29
-
7/26/2019 234136-Fortran 77 With Mts
13/307
10.7 Statement
(Internal)
Functions 10-30
10.8 The
COMMON
Declaration
10-33
10.9
BLOCK
DATA
Subprograms 10-36
10.10
Arrangement
of
Subprograms
10-36
CHAPTER 11
CHARACTER
STRUCTURES
11.1 Introduction 11-1
11.2 I/O
of
Character Information 11-2
Example
Program
11.1
ormatted
Character I/O 11-3
Example
Program
11.2
ist-Directed Character I/O 11-5
11.3
Manipulation
of Character
Information 11-8
Example Program
11.3
lphabetizing a
List
11-9
11.4 Internal I/O Statements Involving Character
Variables
11-13
Example Program
11.4
lotting the
Sine Function 11-14
Example Program
11.5
mployee
Information
11-18
Example Program
11.6
Small Word-Processor 11-20
APPENDIX A: Solutions
to
Exercises A-l
APPENDIX
B: Summary
of
Statement
Types
B-l
INDEX 1-1
VI
-
7/26/2019 234136-Fortran 77 With Mts
14/307
CHAPTER
1
INTRODUCTION TO
COMPUTERS
AND FORTRAN
1.1 Introduction to Digital
Computers
Although
digital
computers
are
often
viewed
as
very
f a s t
numerical
calcula
t o r s , they
a r e ,
i n f a c t , rather general devices f o r
manipulating
symbolic informa
t i o n .
In most applications, the
symbols
being manipulated are numbers
or
digits
( he nc e the
name,
d i g i t a l
computer), and
the operations
being
performed
on these
symbols are the standard arithmetical operations such
as
subtraction and divi
s i o n . However, the s ym bol s mi gh t just
as
easily
have nonnumerical
values and
the operations
be nonnumerical
i n nature. For example,
the
symbols
might
be
characters
such
as l e t t e r s
and
punctuation marks,
and
the operations
might
result
in
the
parsing
of
sentences f o r their word
and
phrase
structure.
A
general-purpose
computer can be instructed to accept, store, manipulate, and display
virtually
any
kind
of information i n properly encoded symbolic
form.
General-purpose
computers are
designed to solve essentially
any problem
that i s computable. Computability has a rigorous mathematical or logical meaning,
but an intuitive understanding of the term i s adequate f o r most
programmers.
A
computable
problem i s
one
that
can
be stated unambiguously and for
which an
unambiguous terminating
solution procedure
or algorithm can
be
outlined,
step
by
step.
I f the
algorithm i s i n
graphical
form i t i s
usually
called
a f low
diagram; i f i t i s
a
l i s t
of commands
that can be interpreted directly by
a
computer, the algorithm
i s
called a
program.
Although
we
tend
to
view
a
digital
computer
as
a
unit,
that
i s ,
as
a
single
problem-solving machine,
every
computer
i s
i n fact a collection
of
a
large number
of
inter-connected electromechanical devices,
a l l
directed by a central control unit.
Fortunately, an
understanding
of
computer operation
and
the a b i l i t y
to
use a
computer does
not
require detailed knowledge either of electronics or of hardware
(the physical
equipment)
construction. An overall view of the organization of
the
computer
with
emphasis
on
function rather than
e l e c t r i c a l
or mechanical
details
i s
s u f f i c i e n t .
Viewed
functionally, a l l items
of equipment
associated with
a
digital com
puter
can
be
grouped
into
four general
categories:
1 . Memory.
2 .
Input/output units.
1- 1
-
7/26/2019 234136-Fortran 77 With Mts
15/307
Chapter 1
ntroduction
t o Computers
and FORTRAN
3 .
Arithmetic unit.
4 .
Control
unit.
The
machine
shown
in
Fig.
1 . 1
i s
hypothetical,
but
typifies most
currently
available digital computers. Specific operating details for each of the many com
puters now i n use w i l l vary from one machine to the next.
Input
Devices
MEMORY
(Store)
Arithmetic
Unit
Output
Devices
Control
Unit
Fig. 1.1
Overall d i g i t a l - computer
organization.
(Lines between
units show flow
of
information.)
The Memory.
The
principal
or
fast
memory
or
store
i s
the heart
of
the
digital computer, and consists of solid-state devices in which a large number of
binary digits or b i t s can be saved and retrieved rapidly. Since almost any kind of
information (Greek l e t t e r s , punctuation marks, e t c . ) can be e nc od ed as a unique
sequence of binary d i g i t s , the memory can be v ie we d functionally as a place where
any kind of d i g i t a l l y encoded information can be stored and i n turn retrieved.
Since
the
store
i s
considered
to
hold a large
collection of
binary
d i g i t s ,
most
machines use the binary number system for internal calculations, and are
hence
called binary computers.
To
simplify the
problem of
locating any sequence of digits
in the memory, the overall collection of storage elements i s usually
divided
into
smaller
addressable
packages, the
smallest
of
which
contains
eight
binary
bits
and
i s called a b y t e . Depending on the particular computer, groups of 16, 32, and 64
binary b i t s are called h a l f , f u l l ,
and
double words, respectively.
The
memories of
most
d i g i t a l computers
are
constructed
so that
the
content
of a memory
word
may be retrieved
or
read without
destroying
i t (non-destructive
read-out);
on
the othe r hand, when a new number i s stored or written into a
memory
word, the
previous content of that word
i s l o s t
(destructive read-in).
This
i s completely
analogous
to the operation of
a
tape recorder:
recorded information
may be played back without
destroying
i t ; when a
new
signal
i s
recorded over
previous
information, the
e a r l i e r
recording
i s
destroyed
or
erased
i n
the
process.
Access
time
(the time required to retrieve
one
or more
by te s of stor ed i n
formation) f o r
semi-conductor memories
i s
measured
i n
billionths
of
a se cond
or
1-2
-
7/26/2019 234136-Fortran 77 With Mts
16/307
1.1 Introduction t o Digital Computers
nano-seconds, and i s the same f o r a l l memory locations, regardless of their ad
dresses. The f a s t memory
i s
a relatively expensive part
of
the computer, although
advances i n semiconductor technology hav e r e duc e d the cost substantially over the
past
f ew
years.
To
back
up
the
f a s t store,
there
i s
a
variety
of
other
storage
devices
with longer access times, bu t with much larger capacities and lower i n i t i a l c o s t s .
The most important of the secondary or peripheral storage devices are the disk f i l e
(and i t s smaller version, the d i s k e t t e ) , and magnetic tape.
Input and
Output (I/O) Equipment. The function of the
I/O
equipment
i s
to allow communication between the use r of the machine and i t s memory. There
may be several
such
devices, depending
on
the s i z e
and
complexity of the comput
ing
installation.
For small computers, the
keyboard
i s
commonly
used for input.
However,
other
input
devices
can
include
punched
cards
and
magnetic-tape
units.
A
l i g h t -
pen
or
a movable
mouse
can
also
be used
f o r
drawing pictures on a cathode-ray
tube screen
or
f o r making selections from
a displayed
menu. In f a c t , almost
any
signal-generating device ( f o r
example,
an electrocardiograph mac hine ) c an be
adapted as a suitable input device for a computer. In some computing installations,
small computers are employed for input and output to larger computers.
The output devices are often similar to the input devices. For small comput
e r s , a TV monitor
and
a dot-matrix printer are commonly used f o r output. Other
output devices can include magnetic tape drives, high-speed laser-driven printers,
synthetic
speech
units,
and
so
on.
The
Arithmetic
Unit. The
a b i l i t y
to
save and retrieve information
i s
useful
i n i t s e l f . However, to solve a problem, we would l i k e to read information (data)
into some
words of memory, operate on
t h i s information i n some planned way
to
produce results
(which could
be stored in other
words of
the memory), and f i n a l l y
to display the data and results on some output device.
Information
i n the memory
that i s to
be manipulated
i s
f i r s t
read nonde-
structively
and passed to
a
part of
the computer
called the arithmetic u n i t .
The
latter
contains a l l the
circuitry required
to
perform the
standard arithmetical
oper
ations
(such
as
addition,
subtraction,
multiplication,
and
division)
and
also
many
other
nonarithmetical
operations such as
the
shifting or digit-wise examination of
numbers,
the comparison of numbers f o r
s i g n ,
relative magnitude, and
so
forth.
Each
digital computer has a fixed number
of
distinctly different operations,
called machine instructions, which the arithmetic
unit
i s capable of executing. In
addition, some other machine instructions are used for controlling the reading and
writing operations
of
the I/O devices. Most large computers have 200
or
more
such operations i n their instruction r e p e r t o i r e . The instruction repertoire for each
different model
of
computer i s usually
different from
that
of
a l l
other
machines.
The results of operations i n the arithmetic unit are then stored i n selected memory
locations.
With the addition of the arithmetic
unit,
the d i g i t a l computer now begins
to assume a meaningful
form.
The machine can read
data
from i t s environment
1- 3
-
7/26/2019 234136-Fortran 77 With Mts
17/307
Chapter 1ntroduction t o Computers and FORTRAN
and
enter them into
the
memory.
The contents of
various memory
words
can
then be manipulated in the
arithmetic
unit by
means of the
operations that the
computer
i s
designed to
perform. The results of these
operations
can be stored
i n
the memory
along
with
the
original
data
and
subsequently
retrieved
for
display
on
the output equipment.
The sequence
of
events i s :
1 .
Read data
into the
memory v ia the
input equipment.
2 . Operate ( i n the arithmetic unit)
on
the data stored i n the memory.
3 .
Store i n the memory the results of
these
operations.
4 . Retrieve the results from
the
memory for display on
the
output equipment.
The
Control Unit.
Obviously, i n
order to
process data i n a
meaningful
way
and
produce
useful r e u l t s ,
the
computer
must have
associated
with
i t
a
controlling
d ev ic e that supe rv ise s the
sequence
of
a c t i v i t i e s
taking
place
i n
a l l
parts of
the
machine . This
control equipment
must
decide:
1 .
When (and
with which input
device)
to bring information
into the memory.
2 . Where to place the information in the memory.
3 .
What sequence of operations on
information
i n the memory i s to be done i n
the arithmetic
unit.
4 .
Where intermediate
or
f i n a l
results
of
operations
i n the arithmetic
unit are
to
be
saved
i n
the
memory.
5 . When, and on which output device, results are to be displayed.
With the addition
of
the
control unit,
(see Fig. 1 . 1 ) , we
now have
a machine
that i s capable
of
solving suitably
stated
and defined problems, given a l i s t
of
com
mands for
the
operations to be performed. Often,
the
combination of
arithmetic
and control
units
i s
called the
central processing unit (CPU); i n microcomputers,
the
CPU
i s
called
the microprocessor.
How
does the
machine
user indicated what the machine i s to
do to
solve his
or
he r problem? First, the problem must be examined,
and
a
step-
by-step procedure
or algorithm outlined for i t s solution. A l i s t of commands, called a program, i s then
made from the
instruction
repertoire, i n order
to
cause the computer
to implement
the algorithm.
The
instruction
must
be
ordered
i n
the
proper
sequence; only
those
i n the instruction repertoire may appear i n the program.
Machine-Language Instructions. When using a poc ket calculator, the
user
assumes several
of the
functions
of the control unit in
deciding
which number
or
operation
i s to be used ne xt. Typically, a new number
or
instruction might
be
entered
every f ew
seconds.
With the
very high
internal operating speed
of a
digital
computer
(millions of
individual
instructions are executed
every second
on
the larger
machines),
i t i s impractical for the machine
user
to
enter
commands
from the keyboard. Consequently,
some other
approach i s necessary to allow very
rapid processing
of
machine
instructions.
Because direct communication
between the machine and
i t s
environment
involves the
use of
slow
mechanical equiment,
any
approach
that
requires such
1- 4
-
7/26/2019 234136-Fortran 77 With Mts
18/307
1.1 Introduction
t o Digital Computers
contact
continuously, such as
pressing
keys, or
even
reading from
punched cards,
i s impossibly
slow. One
solution to
t h i s
problem was
f i r s t suggested by
Burks,
Goldstine,
and
von
Neumann.
f
Since
a program
i s an assembly of information,
and
the memory of
a
computer
can
be v ie we d
as
a
place
f o r
storing
any
kind
of
information, the program can be stored in the memory
along
with the data and
r e s u l t s .
Virtually a l l
the
general-purpose computers now available are organized
t h i s way, and
are
consequently
known as
stored-program computers. Since only
digits may be
stored
i n
the
memory,
the
instructions
must
be
encoded
i n digital
form
before being stored.
The
code used
i s called
the
machine's
language,
and the
encoded program i s
known
as
a machine-language program.
The
ultimate
form
of
each machine-language
instruction must
be a sequence
(32 i n
number,
for example)
of
binary digits corresponding to
the
normal mode of
storage
i n
the
machine.
Since
a
complicated
problem
might
require
many
thou
sands
of
such
instructions,
the
writing of an
error-free
program
would
be
extremely
tedious. In addition, in order to
write
programs i n machine
language, one
must
be reasonably familiar with
a l l
the instructions in the repertoire of the machine.
Obviously, easier ways of communicating
with
the computer are required
to achieve programming e f f i c i e n c y , and symbolic languages for describing algo
rithms have been developed f or this purpose. Here, symbols and recognizable
words (rather than sequences of binary d i g i t s ) are used for representing the oper
ations
required i n the algorithm.
Indeed, most users
would
prefer
to
communicate
with the
machine in a
familiar
symbolic form that
comprises
algebraic notation, English
words,
e t c . , and
to be able to write a program i n a form such a s :
START
READ X. Y.
Z
U - ( X + Y)/Z
PRINT X. Y. Z.
U
GO TO
START
Thus,
detailed
knowledge
of
machine-language
instructions
would
be
unnec
essary and the language might
hopefuly
be used to describe
programs
for more than
one c om pu te r , making i t possible to interchange programs with other computer
users.
Computer languages similar
to
that
illustrated
above, and allowing straight
forward description of an algorithm, are called algebraic, algorithmic, or procedure-
oriented languages. The languages are (insofar as i s possible), machine indepen
dent;
that i s , details of
hardware
organization
or
of machine
language
are not
apparent to the user of the language. Programs written i n such languages cannot
t A.W.
Burks.
H.H. Goldstine.
and
J . von Neumann.
Preliminary Discussion of
th e Logical
Design of
an Electronic
Computing
Instrument. a report
prepared for
th e Ordnance Dept.. U.S.
Army.
at th e
Institute
of
Advanced Study, Princeton. New Jersey. J une . 194 6. Reprinted in Datamation. Vol. 8 ,
Nos. 9 and 10. 1962.
1-5
-
7/26/2019 234136-Fortran 77 With Mts
19/307
Chapter 1ntroduction t o Computers and
FORTRAN
be executed
directly by the computer;
they must f i r s t be translated into
equiva
lent machine-language programs. The
programs
that
implement
the translation
are called compilers.
Following
Fig.
1 . 2 ,
a
source
program
written
i n
the
procedure-oriented
lan
guage i s
f i r s t
translated
into an object
or
machine-language program.
The
resulting
object program i s then stored in
the
memory and i s
subsequently
executed by the
c ompute r. Thus, the processing
of
a program written in a procedure-oriented
language occurs i n two steps:
1 .
Translation
or compilation.
2 .
Execution.
INPUT
OUTPUT
Step 1
( e.
Step
2
Compiler
Program
Memory
Compiler
Program
Object
Program
Program
in
Machine Language
j.,
FORTRAN)
Memory
Program
Object
Program
Fig. 1.2 Steps during compilation and
execution.
FORTRAN-77 (or simply, FORTRAN) i s one of the
most
popular procedure-
oriented
languages
and w i l l
be
discussed
i n
the
rest
of
this
book.
Note
f i n a l l y
that
since each different
model
of computer has i t s own machine language, there must
be a c ompil er for each procedure-oriented language for
every
different kind of
computer
Fortunately,
FORTRAN translators are available f o r most machines on
the commercial market, so that FORTRAN programs
can
be processed
on
most
digital computers
now
i n use.
1.2 Development
of
the FORTRAN Language
FORTRAN-77
i s
a procedure-oriented language having a
well-defined syntax
or
grammar,
that
was
developed
to
allow the
unambiguous
description
and
imple
mentation of
algorithms, particularly those
of
a
numerical nature. FORTRAN-77
( o r , more simply,
FORTRAN)
i s
one
of the programming languages
that
i s most
1-6
-
7/26/2019 234136-Fortran 77 With Mts
20/307
1.2
Development
of the FORTRAN
Language
widely used
by s c i e n t i s t s
and
engineers today.
This popularity
stems
from three
historical
f a c t s :
1 .
FORTRAN-77
i s
a
direct
descendent
of
FORTRAN-I, the
f i r s t
procedure-
oriented language.
2 .
The FORTRAN family of languages
was
developed by the International
Business Machines
Corporation, which
has featured prominently i n
com
puter sales in the United
States
and around the
world.
3 .
In order to
remain competitive
i n both
hardware and
software, other man
ufacturers have developed FORTRAN compilers
f o r
their computers. As
a
result,
virtually every manufacturer supplies
a
FORTRAN
compiler
f o r
each
machine in i t s
product l i n e .
Almost by default, FORTRAN became for
many
years
the universal comput
ing
language f o r enginering
calculations,
while
many
other
universal
computing
languages
have
come and
gone.
This
i s
not
to
say that
FORTRAN w i l l
never
be
displaced by better
and more
powerful procedure-oriented
languages
such
as
PASCAL, but
that
i t s continued use seems assured.
We w i l l begin with a brief description of the development of the FORTRAN
family of programming languages. The f i r s t mention of a
FORmula
TRANslation
System
seems
to
have occurred i n an internal IBM report, dated l a t e i n 1954.f The
f i r s t translator f o r
the
original
FORTRAN
language,
later known
as
FORTRAN-
I ,
was released i n 1957 for
use on the
f i r s t
of
the
IBM large-scale computers,
the IBM 704. An improved translator that allowed users to def ine their own
functions
and
subroutines
was
released
in
1958,
and
this
expanded
language
was
called FORTRAN-II.
A FORTRAN-III language appears to have been developed and employed
internally at various IBM installations, bu t was never released f o r customer use.
In 1962, the
FORTRAN-IV
language specifications were published; by 1965,
most
manufacturers
were
committed to the production of compilers f o r some version of
FORTRAN
f o r
their machines.
Fortunately, the situation
has
been c l a r i f i e d somewhat i n the intervening
years by the publication
of two
reports by the
American
Standards Associa
t i o n , l a t e r known as the
American National
Standards
Institute
or ANSI.
The
f i r s t report,
r a t i f i e d i n 1966, defined a language that
has since
become known
as
FORTRAN-66, and that
corresponds essentially to FORTRAN-IV.
The second
ANSI report
has
defined ( i n 1978) a FORTRAN-77 language i n two versions: f u l l
FORTRAN and subset
FORTRAN.
The
latter contains
some
restrictions i n order
to
make
i t easier to
implement
on small
computers.
The FORTRAN-77
language
described
i n t h i s text w i l l hereafter be known
simply as FORTRAN. I t i s essentially the ANSI subset FORTRAN, and corre
sponds closely to MS-FORTRAN, a comprehensive dialect sold by the Microsoft
Corporation for the
MS-DOS Operating System
as used i n the IBM Personal Com
puters
i n
the
Freshman
Engineering
Computing
Laboratory
at
the U niv er sity of
t JE.
Sammet, Programming
Languages, History and Fundamental; Prentice-Hall. Englewood C l i f f s ,
N.J.. 1969.
1- 7
-
7/26/2019 234136-Fortran 77 With Mts
21/307
Chapter 1
ntroduction
t o Computers and FORTRAN
Michigan.
All
programs
reproduced i n t h i s text were
compiled
using the Microsoft
FORTRAN compiler i n conjunction with an IBM Personal Computer.
Before embarking upon a
description
of FORTRAN, i t seems
appropriate
to
discuss
the
nature
of
procedure-oriented
languages,
and
their
similarities
to
natural languages.
I t
i s essential to understand that the syntax for
a
computer-
programming language i s completely arbitrary. The syntactical rules
that
rigor
ously
characterize
which character strings
constitute a
grammatically
legal
pro
gram are usually drawn up by
a
committee. Thus, the
language
rules depend on
a
variety of influences,
not
the
l e a s t being personal
opinion
or
prejudices
of
those
formulating
them Language
design
i s sometimes
considered
more an
art than
a
science; consequently, some rules w i l l seem
very
a r t i f i c i a l or even s i l l y , particularly
to the beginning programmer.
Nevertheless,
the
rules
are
inviolable,
and
must
be
followed
to
the
l e t t e r .
In
this
respect,
programming
laguages are
similar
to a r t i f i c i a l
international lan
guages,
such
as Esperanto
and
Interlingua,
each
defined by
committees
of language
s p e c i a l i s t s . As an i l l u s t r a t i o n , Esperanto contains
a
rule on the
formation
of plural
nouns (domoj, tabloj,
pomoj,
f o r example): All plural
nouns
end with the l e t t e r j ;
only plural
nouns
may end
with the l e t t e r
j . The rule i s obviously
quite
arbitrary.
Nevertheless, because i t i s inviolable, there i s
never
any question
about
the
iden
t i t y of
plurals;
compare this with the irregularity of
plural
formation i n English:
books, sheep, men, for example. The rigid syntax of such languages makes them
much easier
to
learn than
natural languages,
which
are replete with irregularities
and
ambiguities.
Table 1.1 Analogies Between English and FORTRAN
English FORTRAN
Noun
Operand
Verb
Operator
Phrase
Expression
Sentence Statement
Paragraph
Subprogram
Chapter Assembly of
subprograms
There are some analogies between a program written i n a procedure-oriented
language and a paragraph
or
chapter written i n a natural language. For example,
each i s composed f rom a f i n i t e s e t of symbols
that
includes l e t t e r s , d i g i t s , punctu
ation
marks, and
parentheses;
i n FORTRAN,
this
collection of symbols i s called
the character s e t . In a natural language,
the
basic element with a distinct meaning
i s called a word; words are assigned an identity as to type, such as noun, verb,
or preposition.
In
FORTRAN,
we
r e f e r
to
the
basic
elements
as
operands,
opera
t o r s , character strings, delimiters, e t c . For example, the
FORTRAN
statement
or
sentence
1- 8
-
7/26/2019 234136-Fortran 77 With Mts
22/307
l.S
Analysis
of
a FORTRAN Program
X
=
3.4
+
Y
contains
the
three
operands
X , 3.4, and
Y (with
associated
numerical
values) and
the
two operators, = and
+ ,
which
act on
the
operands;
note the
(rather tenuous)
analogy
between verbs and nouns i n a natural language and
operators
and operands
in
FORTRAN. We can extend t h i s
analogy s t i l l further, and
increasing l e v e l s
of
complexity are summarized in Table 1 . 1 .
Two important
differences should also be
noted
between natural
languages
and well-designed
programming
languages:
1 . In a natural language, there i s
often
a
clear
meaning associated wi th f o rm s
that violate
the
syntax or grammar. For example,
the
collection of En
g l i s h words We a i n ' t got none of them
posters
l e f t violates elementary
English
grammar; yet,
the
fact
remains
that
anyone
fluent
i n
the
language
would understand
that
the posters are no
longer
available. A
comparable
syntactical
error
i n FORTRAN w i l l render
the
FORTRAN statement
ab
solutely
meaningless; that i s , only
perfect
FORTRAN
i s
acceptable
to
the
FORTRAN
compiler.
The
compiler
can
translate a l l
grammatically
cor
rect
symbol
s t r i n g s ;
i t cannot translate
any
grammatically incorrect symbol
string.
2 .
In
a
natural
language,
grammatically
correct constructions can s t i l l
provide
ambiguities for the
reader;
t h i s may be because
of
a poor choice
of
phrasing
or
word
order, or
because
the
same
symbol
string
may
have
several
meanings
associated
with
i t ,
as
i n
the sentence:
I have a b i l l .
Here, b i l l
might r e f e r
to
an invoice, a draft
f o r
a proposed statute, a piece
of paper money,
and
several l e s s l i k e l y p o s s i b i l i t i e s such as a bird's beak or a medieval weapon.
The meanings of l e g a l symbol strings are
usually
called the semantics of
a language; c l e a r l y , natural languages have a
very
complex semantics. The
semantics of most computing languages i s more readily formalized than f o r a
natural language. In general, the programmer should view any syntactically
correct
FORTRAN
program
as
having only one meaning.
No doubt,
the
comments of
the
preceding
paragraphs
distort definitions
sacred
to
linguists and
computing-language designers. Nevertheless, there are
obvious
similarities
betwen
the programmer
and
the
author.
Each
attempts
to
create
a complete
and unambiguous
representation
of an
idea
from
a basic
set
of
symbols
using a f or mal set of
syntactical
rules and an associated semantics.
1.3 Analysis
of a FORTRAN Program
We
include i n this
section
a simple,
but
complete, FORTRAN program with
data
and output.
I t
i s
unnecessary f o r the reader
to understand everything
included
here;
however,
he
or
she
should
gain
some feeling
f o r
the
nature
of
a
FORTRAN
program, and
i t s
relationship to the algorithm
(including
the flow diagram) for
solving
the
problem.
1-9
-
7/26/2019 234136-Fortran 77 With Mts
23/307
Chapter 1
ntroduction
t o Computers and FORTRAN
h
z/
45.0
10.0
Y
60.0 10.0
53.1
3.0
53.2
3.0
s\
A
89.0 10.0
Fig. l.S
Problem and
data.
The
problem. The
input
data,
shown
i n Fig. 1 . 3 , consist
of
several
pairs of
values for
the angl e
A ( i n degrees) between
the
base and hypotenuse
of
a
r i g h t -
angled triangle, and for the length X of the base. The problem i s then to compute
the
altitude
Y
and
hypotenuse
Z
of
the
triangle
from
the
following
formulas:
Y
=
XtanA,
Z = \/X2 + Y2 .
The output i s to show
the
numerical values of a l l the variables: A, X, Y, and
Z. The program should be able to process any number of sets of data.
Flow
diagram. The sequence
of
events
needed
to
accomplish the above
i s
represented diagrammatically in
Fig. 1 . 4 .
Fig. 1.4 Flow diagram
for
triangle
problem.
The
box
r esembl ing a
punched
card
denotes
an
input
operation,
in
which
we are reading two values and storing them i n the variables A and X, respectively.
Note that when we say store in the variable A, we consistently mean
that
the
value read for variable A i s stored in the memory word assigned to the variable A.
The rectangular box designates arithmetic computations; f o r example, Y = X tan
A means that the
result
of X times the tangent of A i s stored in the variable Y.
The box resembling a tor n she et of
paper
shows an output operation, in which the
values
of
A, X, Y, and
Z
are printed
or
otherwise displayed.
Note that i f
we
to
wish
to process several sets
of
data, the
arrow returning
to
junction
box
1
allows
us
to
repeat
the sequence.
In
t h i s
case,
i t
i s
understood
from the dotted arrow leading to the End box
that
repetition ceases when
there
are
no
more input data
to
be
read.
1-10
-
7/26/2019 234136-Fortran 77 With Mts
24/307
l.S Analysis of
a
FORTRAN Program
Example
Program 1.1riangle Problem, First Version
The following FORTRA.N program will solve the
above
problem.
Column: 123456789012345678901234567890
1 PROGRAM
TRI1
2 C
Triangle
Problem.
1st version.
3 1 READ
(*.*)
A. X
Line: 4 Y
= X*TAN(A*3. 14159/180.0)
5 Z
= SQRT(X**2 + Y**2)
6 WRITE (*.*) A. X.
Y.
Z
7
GO TO
1
8
END
Note
the
following
about the program:
1
. I t i s written entirely in terms
of
letters,
digits,
and special characters (such
as
*
.
.
/).
Upper-case
letters
such as PROGRAM. X. and GO TO ar e
generally
(but not universally)
needed
for
FORTRAN statements.
However,
lower
case letters
may
appear in
comments
(identified here by
C
in column 1)
,
and
may
also
be
manipulated
and
printed
by
a program
(not
shown here).
2 .
There
ar e
similarities
to
the
flow
diagram,
as
evidenced,
for
example,
in
the
variable
names,
A . X. Y. Z.
and
the
words
READ
and
WRITE.
3 .
There
i s
just one
statement per
line
of the program, and
most
of the state
ments
start in
column 7 (they could
continue through
column 72 i f
neces
sary).
4 .
Occasionally, ther e ar e
symbols in
columns
1-6.
5 . The column and line numbers ar e not par t of the program, but have been
given above for our own orientation.
Data and
output. The
following i s a
copy
of both the
data
that
we typed
on
the
IBM/PC
keyboard
( e . g . ,
45.0
and 10.0
for
A
and
X,
respectively),
and
of
the
output generated by the program on an IBM/PC monitor (screen):
45.0. 10.0
45.0000000
60.0. 10.0
60 . 0000000
53.1. 3.0
53.1000000
53.2.
3.0
53
. 2000000
89. .
10.
10.0000000
10
.
0000000
3
.
0000000
3
.
0000000
9 . 9999880
17
. 3204800
3.9956180
4.0101770
14.1421300
19.9999700
4
.
9964950
5.0081450
1-11
-
7/26/2019 234136-Fortran 77 With Mts
25/307
Chapter 1ntroduction to Computers and FORTRAN
89.0000000
10.0000000
572.8573000
572.9446000
89. 10
89.0000000
10.0000000
572.8573000
572.9446000
89
10
89.0000000
10.0000000
572.8573000 572.9446000
(We have underlined the input data values i n t h i s introduction, just
to distinguish
them
from
the output.)
Note
that
the
output
consists of
values f o r
the
variables
A.
X . Y . and
Z ,
and that there are at most
seven
significant
digits before
the computer
adds some extra zeros.
Actually,
as evidenced by the results f o r the f i r s t triangle,
i n which the altitude should equal
the
base, th e results are probably c or re ct to
about s i x significant d i g i t s , the same as
the approximation
to n introduced i n the
program.
Explanation of
statements i n the program.
We
now explain each program
l i n e in
turn. I f some of
the
ways
of
doing
things
seem a
l i t t l e strange or
contrived
to
you,
remember
that
we are playing according to
the
rules of
FORTRAN,
and that
a
program must
follow
f a i r l y r i g i d
rules
i f
i t
i s to be unambiguously
understood
by the computer.
Line 1 : PROGRAM
TRI1
Here, we are identifying our
program
with the name TRI1. Although such
naming of (main) programs i s
optional,
we
s h a l l
name a l l our programs for later
consistency with othe r type s of
program
units called
external functions
and
sub
routines,
which must
have names.
Line 2 : C
Triangle
Problem.
1st version.
The
C i n column 1 means that t h i s
l i n e
i s
to
be treated as a comment,
and
forms no
essential part
of
the
calculations.
Comments are inserted f o r the
convenience of
the user,
allowing internal documentation of
the
program. All such
l i n e s are ignored by
the
FORTRAN compiler, however.
I t
i s good programming
practice to
document
a l l FORTRAN
programs with a
l i b e r a l proportion of such
comment l i n e s , making the
program more readable not
only for
others,
but f o r
the
programmer
as w e l l
Line 3 : 1 READ (*.*) A.
X
The 1 i n column
4
i s
a
statement number
or
statement l a b e l , and may
actually
appear
anywhere i n
columns 1-5; i t
serves
the
same purpose
as
the
1
1-12
-
7/26/2019 234136-Fortran 77 With Mts
26/307
l.S Analysis
of a FORTRAN Program
i n the small
c i r c l e in the flow
diagram,
and we shall
return
to i t
l a t e r . (Statement
numbers may
be
anywhere i n
the range 1-99999; the choice i s ours,
except
that
we must
not
use the same number more
than
once i n
columns
1-5.)
READ
(*.*)
A.
X
t e l l s
the
computer
to
expect
two
data
values
to
be
read
during
each input operation,
and that these are
to
be assigned
to
the variables
A
and X respectively.
Where
these values come from, and how they are to appear,
i s
determined by
the (*.*)
part.
The f i r s t asterisk denotes an input device, and
s p e c i f i c a l l y means that the data w i l l be ty pe d i n at
ou r
computer
keyboard. The
second asterisk indicates that we are using free-format or l i s t - d i r e c t e d input, and
that we need not worry much about spacing. Note, f o r example, from
the
l a s t
three
sets of data that the values for A and X ( e . g . , 89.0 and 10. 0) can be separated
either
by
a blank space, or by a comma. We can even i n this case omit the decimal
point,
although
this
omission
i s
generally
not
good
programming
practice
lways
include a decimal point when dealing with numbers that may have fractional parts.
Such numbers are called floating-point or r e a l numbers.
Line 4 : Y
-
X*TAN(A*3. 14159/180.0)
This statement computes the tangent of the angle A ( o r , more precisely, of
the number stored i n the
memory
location assigned to
the variable A ) ,
multiplies
i t by X , and stores
the
results i n the variable Y . Note the following:
1 .
The name
of
the
FORTRAN
function
reference
for computing a
trigonomet
r i c tangent i s TAN.
The
programmer should
commit to
memory t h i s name
and those of several other commonly used functions, such as SQRT. SI N.
COS.
EXP, and ALOG.
2 . Like a l l other function references i n FORTRAN, the argument or number
on
which
TAN operates i s enclosed i n parentheses.
3 .
TAN
must have i t s argument i n radians. Since there are k
or
3.14159 radians
i n 180, the value
of
A must be multiplied by t t
and
divided by 180.
The
multiplication
and division
operators
are
designated
by *
and / ,
and must
be included where appropriate; f o r example, 3.14159A i s improper for
the
product of A and j r . (Other operators
are
+ for addition,
-
for subtraction,
and
**
for
exponentiation;
e . g . , A**3 indicates the cube of
A.)
Line 5 : Z = sqRT(X**2 + Y**2)
Here, \/X2 +Y2 i s evaluated and the result stored i n
Z .
Observe that we have
used another useful function reference,
named
SQRT,
and
that again the expression
forming
i t s argument i s
enclosed
i n
parentheses.
The
exponentiation
operator
**
takes i t s usual precedence over addition, i . e . , X and Y are individually squared
before being
added
together.
1-13
-
7/26/2019 234136-Fortran 77 With Mts
27/307
Chapter 1
ntroduction
t o Computers and
FORTRAN
Line 6 : WRITE (*.*) A. X. Y. Z
The
values
of
A
and
X
(read
as
data)
and
Y
and
Z
(computed
by
the
program)
are written or displayed on the monitor, as indicated by
the
f i r s t
a s t e r i s k .
The
second
asterisk
indicates free-format format or l i s t - d i r e c t e d output. The latter has
the
merit of simplicity i n programming, bu t
the
disadvantage that we must accept
whatever the computer decides i n the way
of
spacing
and
number of decimal places
printed.
For example, the f i r s t s e t of output values
f o r
A.
X.
Y. and
Z
was:
45.0000000 10.0000000 9.9999880 14.1421300
Observe
that
the
computer
was
unable
to
compute
the
altitude
of
the
trian
g l e
exactly
( i t
should have
been 1 0 . 0 ) ,
and
that there are
probably
more decimal
places
printed
than
we
really want.
We shall soon
see under Version 2 , with
some
additional statements
i n
the
program, how we
can overcome these
drawbacks.
Line 7 :
GO TO
1
Now
that
we have
processed one set
of
data,
we return to the READ statement
(whose
statement
number
was
l )
f o r
input
of
further
data
s e t s ,
i f
any.
Line
8 :
END
This must appear as the l a s t statement
of every program,
and indicates
that no
more statements
are to
be
processed. I t i s not normally
used
to
stop
the
program
from running
hat i s accomplished
by methods
we
s h a l l
discuss l a t e r .
Note that
the
program can process any number
of data
sets i n
sequence.
Whenever
control
returns
to
the
READ
statement,
the
next
data
l i n e
typed
at
the
keyboard
i s
read and the
appropriate
calculations are
made.
Most computing
systems are designed so that an
attempt to
read a data l i n e
when the
l a s t data
have
already been read
w i l l simply cause
the program
to
stop executing.
In
the
case
of the
IBM PC, execution can
also
be
halted
by
typing the
key-combination
or . Thus, i t i s
unnecessary
to
specify
ahead of
time the
number of
data
l i n e s to be
processed,
thereby avoiding the need to count i n the
program
i t s e l f
the
number
of
data
sets read.
Example
Program
1.2
riangle
Problem,
Second
Version
I f you f e e l moderately
confident
with Version 1 above, now
consider
the
following
modified
program.
1-14
-
7/26/2019 234136-Fortran 77 With Mts
28/307
1 . S Analysis of
a FORTRAN
Program
4
5
Col: 1234567890123456789012345678901234567890123456789012345678
PROGRAM TRI2
C
C
Triangle Problem.
2nd version.
C
C Define I/O units 5 and
6
OPEN
(5. FILE* TRI2.DAT')
OPEN (6.
FILE-'LPTl')
C
C Use formatted output
to
print
a
heading
WRITE (6.201)
201
FORMAT
Cl'.
6X.
'A'.
9X.
'X'.
9X.
'T'
.
9X.
'Z'/'
' )
C
C Read
input
values. perform computations
1 READ (5.*.END=999) A. X
Y - X*TAN(A*3.
14159/180.0)
Z - SQRT(X**2 + Y**2)
C
C
Use
formatted
output for
all variables
WRITE (6.202) A. X. Y. Z
202
FORMAT
('
' .
4F10.4)
C
C Return to statement 1 for more data
GO TO 1
C
C
No
more data remaining
in
input file
999
STOP
END
The
above
program
was
run
i n
conjunction
with
the
following
l i n e s
of
data,
which
were
stored separately i n
a
f i l e named
TRI2.DAT
(again, column numbers
have
been
added for convenience):
1 2
Col: 12345678901234567890
45.0. 10.0
60.0.
10.0
53.1. 3.0
53.2. 3.0
89.
.
10.
89.
10
89 10
1-15
-
7/26/2019 234136-Fortran 77 With Mts
29/307
Chapter 1
ntroduction
t o Computers and
FORTRAN
This new program produced the following output, which now has a much
better
appearance than
before (note
the column
alignment):
Col:
4
12345678901234567890123456789012345678901
A X Y Z
45.0000
10.0000 10.0000
14.1421
60.0000
10 . 0000 17.3205
20.0000
53
. 1000
3.0000 3.9956 4.9965
53.2000
3.0000 4.0102
5.0081
89.0000 10.0000
572.8573
572.9446
89
.
0000 10.0000 572.8573
572.9446
89 . 0000 10.0000 572.8573
572
9446
Concerning the program i t s e l f , which
i s named
TRI2,
several
more comment
statements
have
been added f o r
purposes of documentation. The
blank l i n e s with
just a C in column 1
are
purely
f o r
ease
of
reading.
The
calculations
are
exactly
the
same as
i n Version
1 ,
bu t there are several respects i n
which
the new program
i s
more sophisticated:
OPEN (5. FILE-' TRI2.DAT')
OPEN
(6. FILE-'LPTl')
Here, device number 5 (a
f a i r l y standard
number f o r input) i s
identified with
the f i l e
TRI2 .DAT, which
i s where the pairs of values f o r
A
and
X have been
stored.
Also, device number 6 (standard
for output) i s identified with the
local IBM
PC
printer,
whose
name
i s
LPT1.
Soon,
we
shall
see
that
devices
5
and
6
are
again
mentioned
i n the
READ and WRITE statements.
WRITE (6.201)
201 FORMAT ('1'. 6X. 'A\ 9X. 'X'. 9X. 'Y'. 9X. ' '/ ' )
Here,
we
are
using
formatted
output
to
generate
the
heading
that
appears
at
the
top of
the output, which now
appears
on
the printer
( and not the
TV monitor),
because
unit 6 was
identified with LPT1 i n the
OPEN statement. Two statements
are
1-16
-
7/26/2019 234136-Fortran 77 With Mts
30/307
l.S Analysis of a FORTRAN Program
needed
RITE and FORMAT;
note
that
they
ar e l inke d by the common statement
number 201. The exact spacing and appearance of the heading ar e controlled by
the codes that appear in parentheses in the FORMAT statement. With
one
exception,
to
be
mentioned
in
the
next paragraph,
any
characters
or
literal
strings
enclosed
in
primes (such as
' A ' ) will
appear directly
in
the
printed
output. Also,
an
entry such
as
9X
means that
there ar e
to
be exactly nine
blank spaces between, for
example,
the letters
A and X .
The one
exception i s
that
a character
enclosed in
primes,
and that
would
normally be printed
in column
1 ,
i s not
printed, but
i s treated as a
carriage-control
character
instead. Thus, ' 1 ' indicates that
the
output
i s to
start
at the
top
of a new
page. The slash (/) terminates
a
line of output, and the ensuing blank, ' ,
i s the
carriage-control character for single-line spacing. In
this
instance, however,
there
i s
nothing
to
be
printed
on
the
second line,
so
a completely blank
line
appears
between the heading and the numerical output.
1
READ
(5.*.END-999)
A.
X
Here,
values
for A and X ar e
to
be
read
from input device 5 (identified
with
the f i l e TRI2.DAT in the OPEN statement). As before, the asterisk denotes that l i s t -
directed (format-free) input i s involved. The additional code END=999 indicates
that i f there ar e no more data-values remaining to be read, then control should
automatically
be
transferred
to
the
statement
whose
number follows
the
equals
sign (999 has arbitrarily been
chosen
in this instance). You will se e later
wfcat
happens.
WRITE
(6.202)
A. X. Y.
Z
202 FORMAT (' ' . 4F10.4)
Now
comes
another WRITE/FORMAT combination, this time linked by the
statement number
202.
Its purpose i s to
print numerical values for
the four vari
ables A. X. Y.
and
Z, which appear
in
the WRITE statement. How th ese v al ue s
ar e to appear i s governed
by the format
specification
4F10.4,
whose parts have
the
significance
shown in Table
1.2.
Note
that a l l
sets
of f our
output
values conform
to the
F10.4 code, e . g . :
4
Column: 12345678901234567890123456789012345678901
Values:
53.2000 3.0000
4.0102 5.0081
< >< >
Variables: A X Y Z
Format
codes: F10.4 F10.4 F10.4 F10.4
1-17
-
7/26/2019 234136-Fortran 77 With Mts
31/307
Chapter 1
ntroduction to
Computers and FORTRAN
Table
1.2
Parts
of
the Format Code
4F10.4
Meaning
There
ar e four
numbers
receiving
similar
tre atme nt; the
format specifications could also have been written
fully
as
F10.4, F10.4, F10.4. F10.4.
The
numbers being read
ar e floating-point,
i . e . , they contain
a decimal point.
(FORTRAN
also
calls
these
real
num
bers, in contrast to integers,
which
do not
have
a decimal or
fractional part,
and for
which the format
code i s
I . )
Ten columns ar e to be allowed for each of the
four numbers,
with
four columns
after
the decimal point. Not a l l columns
have
to
be
used,
and
one
of them
must
be
reserved
for
the
decimal point. Also,
i f
any
number were
negative,
i t
would
be prefixed
with a
minus
sign, which also
requires one column
for output.
Observe
also
that
a blank has been
written into column 1 of
each
output
line,
and will be interpreted as a
carriage-control
character,
hence
insuring
single-line
spacing.
999 STOP
R ec al l th at when an attempt was made in statement number 1 to read non
existent data, control would automatically be passed to the statement numbered
999.
Well,
here
i t i s , and i t s purpose
i s
to
halt execution
of
the
program
and
return
control to DOS, the
operating system.
Conclusions. We
have
covered a great
deal
of ground in t he abov e. If most
of
Version
1
makes
sense,
you have already
made
a
good start. I f not, we
shall
be
going through a l l of i t
again in
subsequent chapters.
At any
rate, you should
now
be
somewhat familiar
with
several
of
the
topics
we
shall
be
discussing.
Exercises f or Section 1.3 (See Appendix A for answers to a l l exercises)
Referring to
the above FORTRAN
programs:
1 . In which
column do most
of the statements
that ar e not comment lines
start?
2 .
What
i s
the last
statement
in the
program?
3 .
What
characters
appear that ar e neither letters nor
digits?
4 . Which statements
appear
to go in
pairs?
5 .
Can
lower-case
letters
appear
in
comments?
6 .
What
i s the purpose of the
f i r s t
character
in every line
of output?
Code
4
F
10.4
1-18
-
7/26/2019 234136-Fortran 77 With Mts
32/307
-
7/26/2019 234136-Fortran 77 With Mts
33/307
Chapter 1
ntroduction t o Computers and
FORTRAN
might
appear at some point i n
the
program to
a s s i s t the reader
i n interpreting
i t . Note
that
the
comments
are
not written
as
part
of the
output from the object
program
generated
by
the
compiler; they simply appear
i n
the printed l i s t i n g
of
the
program.
Table l.S Use of Columns in a
FORTRAN Statement
Column
Contents
1 May contain
the l e t t e r C
or
an asterisk I f s o , the l i n e w i l l be
treated
as
a comment,
and w i l l
be
ignored by
the
compiler.
1-5
May
contain
a state me nt
number
or
l a b e l ,
positioned
any
where i n
the
five-column f i e l d . I t i s often necessary to make
specific reference
to
particular statements i n a program. A
statement may be uniquely identified by prefixing i t with any
integer
from
1 through 99999. The statement numbers need
not
be in any particular
sequence,
but a given statement num
be r can correspond to only one statement. I f
no
reference
i s
made to
a statement elsewhere in the program, then that
statement i s usually (and preferably) not assigned a
number;
any executable statement (not yet definedee below) may
be
given
a
statement
number,
however.
Labels
shoul d not
appear
on
any nonexecutable statement (sometimes called a
declaration), except f o r a FORMAT declaration, described i n
Chapter 3 .
7-72 Contains
a FORTRAN declaration or executable statement
between columns
7 and 72 i n c l u s i v e .
6 I f the statement
i s
very long, i t may be continued i n columns
7-72
of
one or
more
subsequent l i n e s ,
up
to a
maximum
of 1 9 ;
in t h i s
case,
each
subsequent
l i n e
must
contain
a
continuation
code i n the form of any character other than 0 ( o r a blank)
in column 6 . We suggest that
the
d i g i t s 1-9 be used.
Messages can be written
by the executing
object program only
i f
WRITE
statements (see Chapter 3) appear i n
the program.
Comments
are not
considered
to
be FORTRAN
statements, and need
not
conform to the
format shown i n Fig.
1 . 5 .
Each comment
must
have a C
or *
i n column
1 ; thereafter,
characters
may
appear
in
any
columns; a continuation code
i s not
allowed.
A purely
blank l i n e
i s
also
treated
as
a
comment
l i n e .
AH
l i n e s
i n a
FORTRAN
program
that
are not
comments
are statements,
and
must
be
written
to conform to the
format
shown
i n Fig. 1 . 5 .
To
write valid
1-20
-
7/26/2019 234136-Fortran 77 With Mts
34/307
1 . 4
Fundamental
Concepts
and Notation
algorithms, the programmer must
adhere
to standard statement types with r i g i d
basic structures.
Table
1.4
General
References
Used
in
This
Book
References Meaning
A Dummy argument for a subprogram.
A*
Calling argument for a subprogram.
B Logical (Boolean) expression.
. 2 3 . Logical (Boolean) operator.
C
Arithmetic or logical constant.
'C
Literal (character) constant.
P
Input/output
de vic e or
f i l e .
Arithmetic expression ( r e a l or integer).
C Character expression.
I
Integer
expression.
Z Real expression.
7 Subprogram
(function
or subroutine) name.
I
Integer constant or unsubscripted
integer variable.
C A l i s t , usually
of
variable names.
M Statement number
or
l a b e l ; an
array
name
in some circumstances.
MAMS
Labeled COMMON block name.
.. Relational operator.
5 Executable statement.
T Type or mode.
U Unit number f o r input
or
output.
V
Arithmetic
variable ( r e a l or integer).
VB Logical
(Boolean)
variable.
VC Character variable.
VI
Integer
variable.
VZ
Real variable.
There
are
two fundamentally
different
types of
statements
or
sentences
in
the
language: e xecutable and nonexecutable. The
executable statements are
those that c ause the FORTRAN
compiler to
generate machine-language instruc
tions
that w i l l be executed when
the obje ct or
machine-language version
of the
FORTRAN source program
i s
loaded into the memory
of
the computer. Nonexe
cutable statements, also known as declarations, are special statements that do not
cause
machine
code
to
be
generated,
but which
supply
information
to
the compiler,
such as the types
of
variables ( s e e the INTEGER and
REAL
declarations i n Sections
2.4 and
9 . 3 ,
for example), and
the
amount of memory
to
be assigned
to
subscripted
1-21
-
7/26/2019 234136-Fortran 77 With Mts
35/307
Chapter 1ntroduction to Computers
and
FORTRAN
variables (Section 7.2). Except for the FORMAT statement, declarations must not
have statement
labels; each FORMAT statement,
however, must have
a label.
In the chapters
that
follow, a
basic
set of
FORTRAN
statement types
will
be
described,
using
the general
references
shown
in
Table 1.4.
General
references
will
be described
when
they
ar e
f i r s t used.
Any
printed or
typed characters,
such as
words and punctuation marks, shown in t he ge ne ral formulation of each statement
type
ar e part of the basic statement structure and must no t be altered in any way.
Exercises f or Section 1.4
1 .
Check
true or false, as
appropriate:
(a)
Statement
numbers
in a FORTRAN program may be T F
in
columns
1
through
6
inclusive.
(b) A
completely blank
line
i s
a
comment
line. T
F
(c) A comment may be inserted in a FORTRAN program T F
by
typing
a C
in column 1
or
2 .
(d)
A C
in
column
6 identifies that line in
a FORTRAN
T
F
program as a continuation of t he pr e vi ou s
one.
(e)
Statement
numbers
in
a
FORTRAN
program
may be
T
F
in the range 0 through 99999.
( f )
The
maximum
number
of
columns
available
for
a
T
D
F
D
FORTRAN statement, not i nc l ud ing t he statement
number
and
continuation code, i s 65.
1.5 Three
Statements:
GO TO,
STOP, and END
We
conclude this chapter
by describing
three simple statements:
GO TO,
STOP, and END. The END statement must appear once in ever y program;
however,
the other two ar e also
used
quite frequently.
Transfer Statements. Statements
ar e
normally executed in
sequence,
but
the
unconditional transfer e xecutable statement,
GO TO
JJ
at
any
point in the program will cause the statement numbered M to be
executed
next.
Examples are:
GO TO
10
GO TO 75
1-22
-
7/26/2019 234136-Fortran 77 With Mts
36/307
1.5 Thr e e S tate me nts:
GO
TO.
STOP.
and END
There
ar e
two
principal uses
for
such a statement: (a) after processing a set
of data, to return to the beginning of a program to read another
se t
of
data
(for
example,
GO TO
1 could
be
used to
return
to a READ statement numbered
1 ) ;
and
(b)
to
bypass a
segment
of
a
program
under
certain
circumstances,
sometimes
in
conjunction
with
the logical
IF
statement of Chapter 6 .
Terminating Program
Execution
he
STOP Statement. Execution
of
the
object
program produced
by
the compiler
can be
terminated
in a variety
of ways.
Most
programs
ar e
written to process more
than one se t
of
data
values;
in fact,
the
number of
such data sets
i s
usually arbitrary.
In
these cases, the natural
way
of
terminating computation i s with
the attempted execution
of
an
input
(READ)
statement after the last data set has already been
read.
The approach was
used
in
the
f i r s t program
of Section 1.3,
in which any number
of
sets
of
data
for
triangles
could
be
processed.
An alternative way of terminating execution and transferring c ontr ol t o the
operating
system
i s
to
use the
STOP statement,
which i s
available in three forms
shown below.
In
each case, execution of the
program
ceases immediately, and
a
message
i s
displayed in the
output.
Here, 7
i s an
unsigned
integer constant of
five
digits
or
less
(see
Section 2.3),
and 'C
i s a
character constant (Section 11.1).
Form of Statement Displayed Message
STOP
STOP -
Program terminated.
STOP I I
STOP
'C 'C'
Examples of the STOP statement are:
STOP
STOP 7
STOP 'Negative
value
not allowed for
variable
X'
The STOP statement can be executed at any time, not necessarily after the
end of data has been reached; in fact, one occasionally
encounters
programs in
which
a l l essential
information i s
generated
internally,
so that
no data
ar e
read
at
a l l , and no input statements even appear.
The END
Statement.
The
statement
END
must
appear
once, as the
very last statement, in ever y program.
I t i s
a signal
to the compiler
that no more
statements
appear in
that
program.
Unlike
other
executable
statements,
the END
declaration
should not
be numbered. Usually, END
i s
not the
last
statement
to
be
executed
in
a
program.
A
transfer
statement,
a
STOP
statement, or a call for
return
to the
operating system will almost invariably
be
the
final
executable statement preceding the
END statement.
1-23
-
7/26/2019 234136-Fortran 77 With Mts
37/307
Chapter 1
ntroduction
t o
Computers and FORTRAN
Notes
1-24
-
7/26/2019 234136-Fortran 77 With Mts
38/307
CHAPTER
2
ARITHMETIC STRUCTURES
2 . 1
Introduction
Our
f i r s t goal
i n this chapter i s
to
describe completely
the
syntactical
or
grammatical
rules
for
the
formation
of
a l l
proper
statements
of
the
form
V
-
where V designates the name of
an
arithmetic
variable
and
i s an arithmetic
expression.
The equal
sign i n
FORTRAN i s called the assignment
operator;
the
left-pointing arrow would would seem
to
be a more
appropriate character to
use
f o r
assignment, but
the
equal
sign i s always
used i n
FORTRAN instead. In
every
case, a numerical value
i s
computed
for
the
expression on the right-hand side of the
equal
s i g n ; this numerical value
i s then
assigned as the new value
of
the variable
appearing
on the
l e f t - s i d e
of the
equal s i g n .
Such
statements
are
called arithmetic assignment
( o r
substitution)
state
ments and appear whenever
arithmetical
operations are
to
be performed and a
new
numerical value
i s to
be assigned
to
a
memory
location.
Representative
ex
amples a r e :
Y = X*TAN(A*3. 14159/180.0)
Z = SQRT(X**2 +
Y**2)
In each case, the
statement
causes the expression on the right-hand side
of
the
equal sign
to
be
evaluated
and the
numerical result to
become assigned
as
the new
value of the variable on the left-hand s i d e .
Table 2.1 Building Blocks for
Arithmetic Expressions
Operands
Operators
Integer
constants **
(exponentiation)
Real (floating-point)
constants * (multiplication)
Integer variables
/ (division)
Real
variables
+
(addition)
Integer function
references
-
(subtraction)
Real function references (negation)
2-1
-
7/26/2019 234136-Fortran 77 With Mts
39/307
Chapter 2rithmetic Structures
Arithmetic expressions ar e constructed from the basic operations and op
erators listed in Table 2.1. Table 2.2 illustrates how a representative assignment
statement
can be broken down
into i t s constituent
parts.
Table 2.2 Analysis of an Assignment Statement
Statement:
Arithmetic Operands:
Constants
Variables
Function Reference
Arithmetic Operators:
Multiplication
Division
Y
X*TAN(A*3. 14159/180.0)
3.14159,
180.0
A. X, Y
TAN( )
/
Assignment
Operator.
The action
of
the assignment
statement
i s
in two parts, as follows:
1 . Evaluate the ar ithm eti c
expression
on the
right-hand
side of the
equal
sign.
This
involves
retrieving from memory the current val ue of the
variable A,
which i s
presumably
in
degrees,
since i t i s converted to r ad ian s ( by m ul ti pl y
ing
by
tt/180)
before
evaluating
i t s
trigonometric
tangent
and
multiplying
the
result by the c ur r ent v al ue
of the
variable X.
2 .
Assign
the
numerical value of the expr ession thus
computed
as the new
value
of the variable
Y.
Note that the
previous
number associated with
variable
Y
has been over-written in the process,
and
i s no longer stored in the
memory
of
the
computer.
2.2 Types: Integer
and
Real
Certain
quantities,
such as the number of a
particular month
in the year
(January
=
1 ,
February
=
2 ,
etc.),
or
the
number
of
points
in
a
football
score,