Professional Documents
Culture Documents
trated in Figure
3-1. The steps involved are as follows:
2. Define the inputs required by the program and the outputs to be produce
d by
the program.
The inputs to the program and the outputs produced by the program m
ust be
heme.
ably in
in that
at may
ormat
specified so that the new program will properly fit into the overall processing sc
In the above example, the coefficients of the equations to be solved are prob
some pre-existing order, and our new program needs to be able to read them
order. Similarly, it needs to produce the answers required by the programs th
follow it in the overall processing scheme, and to write out those answers in the f
needed by the programs following it.
It is at this stage in the process that top-down design techniques come into pla
designer looks for logical divisions within the problem, and divides it up into sub
along those lines. This process is called decomposition. If the subtasks are thems
large, the designer can break them up into even smaller sub-subtasks. This
process
which
continues until the problem has been divided into many small pieces, each of
is fur
ment, a
designer starts with a general description of what the piece of code should do, an
d then
defines the functions of the piece in greater and greater detail until they are s
pecific
with
ring the
or she
went through in solving the problem by hand will be better able to apply deco
mposi
will be very simple. All the programmer will have to do is to replace pseudocod
e with
a program, we must verify that it works correctly for all legal input data sets. It i
s very
common for a program to be written, tested with some standard data set, and rel
eased
for use, only to find that it produces the wrong answers for crashes) with a d
ifferent
input data set. If the algorithm implemented in a program includes different bran
ches,
we must test all of the possible branches to confirm that the program operates cor
rectly
ed for
general use (see Figure 3-2). The first stage of testing is sometimes called uni
t test
ing. During unit testing, the individual subtasks of the program are tested separate
ly
(1ngica]_expr)
Statement
Statement
1
2
THFN
Code
Block
END
IF
If the logical expression is true, the program executes the statements in the block
between the IF and END IF statements. If the logical expression is false, then llie pro
gram skips all of the statements in the block between the IF and END IF statements,
and executes the next statement after the END IF. The flowchart for a block 1F con
struct is shown in Figure 3-5.
The IF (...) THEN is a single Fortran statement that must be written all
together on the same line, and the statements to be executed must occupy separate
lines below the I F (...) THEN statement. An END IF statement must follow the
m
on a separate line. There should not be a statement number on the line containing
the END IF statement. For readability, the block of code between the IF and END
I F statements is usually indented by two or three spaces, but this is not actuall
y
required.
r loop
constructs. By following them consistently, your code will contain fewer bugs, w
ill be
easier to debug, and will be more understandable to others who may need to wor
k with
it in the future.
1. Always indent code blocks in block IF and CASE constructs to make the
m more
2.
, since
readable.
Be cautious about testing for equality with real variables in an IF construct
round-off errors may cause two variables that should be equal to fail a
test for
equality. Instead, test to see if the variables are nearly equal within the rou
nd-off
3.
logical
4.1
CONTROL CONSTRUCTS: LOOPS
Loops are Fortran constracts that permit us to execute a sequence of statements more
than once. There are two basic forms of loop constructs: while loops and iterative
loops (or counting loops). The major difference between these two types of loop is
in how the repetition is controlled. The code in a while loop is repeated an indefinite
number of times until some user-specified condition is satisfied. By contrast, the code
in an iterative loop is repeated a specified number of times, and the number of repeti
tions is known before the loop starts.
4.1.2
The DO
WHILE Loop
There is an alternative form of the while loop in Fortran 95/2003, called the DO WHILE
loop. The DO WHI LE construct has the form
DO WHILE (logicdl_expr)
END DO
"I
I
Statement 1
Statement 2
Statement n
If the logical expression is true, statements 1 through n will be executed, and then con
trol will return to the DO WHILE statement. If the logical expression is still taic, the state
ments will be executed again. This process will be repeated until the logical expression
becomes false. When control returns to the DO WHILE statement and the logical expres
sion is false, the program will execute the first statement after the END DO.
This construct is a special case of the more general while loop, in which the exit
test must always occur at the top of the loop. There is no reason to ever use it, since the
general while loop does the same job with more flexibility.
DO index = istart,
Statement 1
iend. incr
1
> Code Block
Statement n
J
END DO
where 1 ndex is an integer variable used as the loop counter (also known as the loop
index). The integer quantities i start, iend, and incr are the parameters of the
counting loop; they control the values of the variable i ndex during executi
on. The
own as
oop.
constant,
values
are calculated before the start of the loop, and the resulting values are
used to
2.
he value
ram ex
<
i e n d * i n c r, the prog
3.
l vari
index +
incr
If 1 ndex*incr is still < iend*i ncr, the program executes the statements
within
4.
hen this
e end
of the DO loop.
6.1
DECLARING ARRAYS
Before an array can be used, its type and the number of eleiueuts it contains m
ust
be declared to the compiler in a type declaration statement, so that the compiler will
know what sort of data is to be stored in the array, and how much memory is require
d
to hold it. For example, a real array voltage containing 16 elements could be declare
d
as follows,'
REAL,
DIHENSI0N(16)
::
voltage
The DIMENSION attribute in the type declaration statement declai-es the size of the array
being defined. The elements in array vol tag e would be addressed as v o l t a g e ( l )
,
' An alternative
way to declare an array is to attach the dimension information directly to the array name:
REAL ::
voltage(16)
This declaration style is provided for backward compatibility with earlier versions of Fortran. It is full
y
equivalent to the array declaration shown above.
20),
DIMENSIDN(50)
::
last.name
into two or more dimensions. These arrays are convenient for representing data t
hat is
normally organized into multiple dimensions, such as map information, tempe
rature
d for a
st_naniip
arrays
ent of
vul
50. The
e array
e rank
umber
y is the
is 20,
consist
between
Fortran
r is / ).
g five
later in Chapter 8.
The number of elements in a given dimension of an array is called the ext
llie array in thai dimension. The extent of the first (and only) subsciipt of array
a ge is 20, and the extent of the first (and only) subscript of array 1 a s t_n ame is
shape of an array is defined as the combination of its rank and the extent of th
in each dimension. Thus, two arrays have the same shape if they have the sam
and the same extent in each dimension. Finally, the size of an array is the total n
of elements declared in that array. For simple rank 1 arrays, the size of the arra
same as the extent of its single subscript. Therefore, the size of array vol tage
and the size of array 1 ast_name is 50,
Array constants may also be defined. An array constant is an array
ing entirely of constants. It is defined by placing the constant values
special delimiters, called array constructors. The starting delimiter of a
95 array constaictor is (/, and the ending delimiter of an array constructo
For example, the expression shown below defines an array constant containin
integer elements;
6.2
USING ARRAY ELEMENTS IN FORTRAN STATEMENTS
This section contains some of the practical details involved in using arrays in Fortran
programs.
ment may be used in any place where an ordinary variable of the same type may be
used. Array elements may be included in arithmetic and logical expressions, and the
results of an expression may be assigned to an array element. For example, assume
that arrays i ndex and temp are declared as:
INTEGER, DIMENSION(IO) :: index
REAL. DIMENSI0N(3) :: temp
::
',
j
j(l)
The array j has been declared by the type declaration statement, but no values have
been placed into it yet. Since the contents of an uninitialized array are unknown and
can vary from computer to computer, the elements of the array should never be used
until they are initialized to known values.
...
DIHENSI0N(5) :;
arr
lems, however, it is more convenient to address the array elements with oth
scripts. For example, the possible grades on an exam might range from 0 to
we wished to accumulate statistics on the number of people scoring any given
grade,
d from
student's
ers that
nge, we
, with