SOLUTIONS
,Contents
1 About This Solutions Manual 1
1.1 To the Student . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 To the Instructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Solutions to the Exercises 3
Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Chapter 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Chapter 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Chapter 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Chapter 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Chapter 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
3 Extra Exercises and Their Solutions 221
4 Teaching F ORTRAN Programming 259
v
,1
About This Solutions Manual
1.1 To the Student
This Solutions Manual is intended for the instructor of a course that uses Classical FORTRAN
as a textbook, and for the student who is learning the subject independently.
If you are a student taking the course for credit you should know that it is a violation of
academic ethics for you to consult this Solutions Manual, whether you copy verbatim from it
or only use it to get ideas about how to work the exercises. The exercises are a valuable aid
to learning the material in the textbook, but only if you work them yourself ! Looking up the
answers instead of figuring them out deprives you of an opportunity to learn the material. If
an answer is for credit, cheating is also personally degrading to you and unfair to your
classmates, and it might place you in jeopardy of disciplinary action. If this is an illicit
copy, please destroy it now. If this is a stolen copy, please return it to its rightful owner.
Either way, STOP READING .
If you are a student learning the subject by yourself then you are your own instruc-
tor. Please read the next section.
1.2 To the Instructor
This Solutions Manual is intended for the instructor of a course that uses Classical FORTRAN
as a textbook, and for the student who is learning the subject independently.
If you are in either category you should know that it greatly diminishes the usefulness of
the exercises for graded work if their solutions become public. Please refrain from loaning
this book to others, distributing solutions to others, or posting solutions on the World Wide
Web. If you want to show a group how to solve a problem, I suggest making a transparency
or scanning to a .pdf file for projection, or posting the solution in a display case under
glass, so as to reveal the answer while discouraging photocopying or electronic scanning.
At the same time, it is prudent for instructors to expect that some students will have access
to the solutions and thus an unfair advantage over their classmates who do not. If you suspect
this might be the case, you should consider assigning textbook exercises whose solutions are
not included in this Manual or making up problems of your own, perhaps modeled on
exercises in the textbook.
Chapter 2 of this Manual provides solutions to about half of the exercises in the text,
distributed in such a way as to represent each problem type in each chapter while favoring
easy problems and early chapters. Chapter 3 of this Manual contains some additional
exercises and their solutions. Chapter 4 contains some materials that I have found useful in
teaching programming from Classical FORTRAN.
,2
Solutions to the Exercises
Each solution includes my rough estimate of how difficult the exercise is. Easy problems,
marked [E], test the student’s recall of facts and concepts discussed in the text. Hard
problems, marked [H], need some independent thought and possibly some programming
but usually do not explicitly require the student to deliver a program. Projects, marked
[P], typically ask for a finished program as part of the solution to the exercise, and in most
cases providing one requires analysis, program design, and debugging as well as coding in
FORTRAN. The fraction of solutions presented here varies from one part of the textbook to
another and by problem difficulty, according to the table below.
part (see §0.5.3) [E] easy [H] hard [P] project
INSPIRATION none none none
1 1
ELEMENTARY all 2 3
1 1 1
INTERMEDIATE
2 3 4
1 1 1
ADVANCED
3 4 5
REFERENCE none none none
The parts of the book that are listed in the left column are those identified in the table of
§0.5.3 in the text. The actual number of solutions provided for each chapter and difficulty
level is ⌈ n ×f ,⌉ where n is the number of problems having that difficulty in the chapter
and f is the fraction solved of that difficulty in the part of the book to which the chapter
belongs. Applying the formula yields the distribution of solutions shown in the table on the
next page.
Many of the solutions provide a more thorough discussion of the problem than one might
expect from a student, a few refer incidentally to text sections that the student need not have
read yet, and some of the projects are sufficiently open-ended that many different “answers”
could be considered correct. Thus, these solutions are meant to enlighten the grader rather
than to serve as strict templates against which student work is directly compared.
Because each solution is self-contained, literature citations in this Manual are given within
each solution rather than being collected in a bibliography. Section references appearing
in the solutions are to sections in the textbook, not to sections of this Manual. Output
formatting in the terminal session excerpts varies slightly depending on the UNIXTM system (Sun,
IBM, SGI, or Linux) that I used to run each program.
If you need a solution that was not selected for inclusion here, please contact the author
by sending email to or paper mail to PO Box 215, Cropseyville, NY
12052.
3
,4 Classical FORTRAN Solutions Manual
part chapter easy hard project total
given done given done given done given done
INSPIRATION 0 19 0 4 0 0 0 23 0
ELEMENTARY 1 12 12 2 1 0 0 14 13
2 10 10 2 1 1 1 13 12
3 15 15 3 2 4 2 22 19
4 25 25 15 8 8 3 48 36
5 19 19 9 5 8 3 36 27
6 27 27 11 6 4 2 42 35
INTERMEDIATE 7 12 6 8 3 3 1 23 10
8 11 6 7 3 5 2 23 11
9 10 5 10 4 6 2 26 11
10 9 5 9 3 24 6 42 14
ADVANCED 11 22 8 6 2 8 2 36 12
12 29 10 10 3 2 1 41 14
13 24 8 19 5 3 1 46 14
14 17 6 5 2 2 1 24 9
15 10 4 11 3 0 0 21 7
16 7 3 8 2 1 1 16 6
17 32 11 19 5 7 2 58 18
REFERENCE 18 11 0 9 0 3 0 23 0
totals 321 183 167 60 89 30 577 268
,Solutions to the Exercises 5
Hello, World!
1.5.1 [E] (a) The syntax of a programming language is the set of rules prescribing how
its symbols may be assembled to form meaningful statements. (b) The semantics of a
programming language are the rules describing what meaning attaches to each syntactically-
correct statement. (c) Source code is human-readable text consisting of comments and
syntactically-correct statements in some programming language. (d) Object code is a
sequence of machine-readable symbols, usually not corresponding to printable characters,
representing the machine instructions that result from translating a source program for a
particular processor. (e) Linkage editing is the process of concatenating the object code for the
various routines of a program, including user-written, library, and system subprograms, and
filling in their (relative) starting addresses wherever those might be required to resolve
references by each routine to the others making up the program. (f) An executable is a
concatenation of object modules with all of the crossreferences resolved, complete and ready
to be loaded into the memory of a computer where it can be branched to in order to run the
compiled program.
1.5.2 [E] In this book (according to §1.1) the following character set is used in F ORTRAN
statements.
ABCDEFGHIJKLMNOPQRSTUV
WXYZ 1234567890
+-*/=.,:()’
The lower-case alphabetic characters, and occasionally other characters, are also used in
FORTRAN comments in the textbook. The semicolon ; is used as the continuation character in
the textbook. The underscore character is mentioned in 13.2.2 § and appears in 16.2 § in
the names of MPI routines and variables. In 13.6.1, the
§ ampersand character & is mentioned.
1.5.3 [E] Classical FORTRAN uses fixed source form, in which statements begin in or to
the right of column 7 and extend only through column 72. Therefore, blanks are significant
in columns 1-6 of each statement and may not be omitted, and blanks used for indentation
must not push the text of a statement past column 72 (but see §13.1.3). As discussed in
10,
§ blanks may occur in character strings and they are then significant as data. Otherwise,
blanks may be inserted or omitted at the programmer’s option. Thus, for example, the
following programs are regarded as equivalent by the g77 compiler.
A=3.14 AA = 3 . 1 4
B=4.753 BB = 4 . 7 5 3
C=SQRT(A**2+B**2) C = S Q R T ( A A * * 2 + B B * *
2 ) PRINT*,C P R I N T * , C
STOP S T O P
END E N D
Advice is given in §12.4.5 about how much white space to use where so that your programs
will be easy to read.
,6 Classical FORTRAN Solutions Manual
1.5.4 [E] According to §1.1, (a) a comment is any source line that has a C in column 1
and (b) a statement must appear in columns 1-72. (c) Any text extending beyond column
72 is ignored by the compiler. (d) A comment can be continued only by inserting another
comment line. (e) A statement can begin in column 8, or in any other column to the right
of column 7, as long as it ends before column 73.
1.5.5 [E] Each comment line in a Classical FORTRAN program has a C in column 1, and
is ignored by the compiler. Comments can be continued only by inserting another comment
line. In this book, comments are written using mixed case.
Each statement line in a Classical F ORTRAN program begins in or to the right of column
7 and ends in or before column 72. It must be constructed according to the rules of FORTRAN
syntax, and is translated by the compiler. Statements can be continued by placing a nonblank
character other than 0 in column 6 of each continuation line (see 13.1.8). In§ this book,
statements are written using upper case letters, and the continuation character is ;.
An executable statement generates machine instructions that are executed at run time,
after control is transferred by the operating system to the executable program.
The non-executable statements of a program provide the compiler with information about
the source text (e.g., END marks the end of the source for each routine) or about the data
structures that will be used by the program (e.g., COMMON statements describe the layout
of shared data areas, as explained in 8). Non-executable
§ statements (e.g., type declarations) can
affect the machine code that is generated for executable statements, or (e.g., compile-time
initializations) can cause the generation of machine code that stores data when the program is
loaded into memory.
1.5.6 [E] The three statements introduced in this chapter are PRINT, STOP, and END. The
PRINT statement can be used to print text or (as we shall see later) the values of variables.
The STOP statement is executable, and returns control of the processor to the operating
system. The END statement is non-executable, and marks the end of each routine’s source
text.
1.5.7 [E] To begin running your program, the operating system copies your executable
into memory and transfers control to its first machine instruction. When your program is
finished it executes a STOP statement to return control of the processor to the operating
system. A program can contain any number of STOP statements (though it makes sense and
is syntactically legal to have more than one only if the program has more than one possible
path for the flow of control, as explained in §3). In contrast, END is the last statement of
the source text of a routine and is not executable. In a file containing several routines, the
END statements show the compiler where one routine ends and the next begins (this can
happen when a program consists of several routines, as explained in §6).
1.5.8 [E] Classical F ORTRAN is a rather low-level language, but it is easier to write than
assembler language and much easier to write than machine language. An assembler- or
machine-language program can be used only on processors having a particular instruction
set, but a F ORTRAN program can be used on any computer that has a F ORTRAN compiler.
,Solutions to the Exercises 7
1.5.9 [E] According to §1.2, the compiler’s two main tasks are translating F ORTRAN source
code into machine instructions and linking in any operating system routines used by the
program. As we shall see later it is also possible for a program to be made up of several
routines, and to use routines stored in a subprogram library, and in that case the linkage
editing step performed by the compiler combines all of the parts to make an executable.
1.5.10 [E] If a Classical F ORTRAN source program is in the file source.f, we could
compile and run it by using UNIXTM commands similar to these.
unix[1] f77 source.f
unix[2] a.out
1.5.12 [E] An executable prepared for one computer can be run on another only if the
processors of the two machines both use the same set of machine instructions, because the
object code in an executable consists of machine instructions for the processor of the com -
puter where the program was compiled. Even if the processors are the same, the executable
won’t run on the second computer if the computers have different system routines, which
they probably will unless the operating systems are identical versions of UNIX TM. To move
a FORTRAN program from one machine to another of a different kind it is usually necessary to
recompile the program (that is, translate the source to machine language and link in the
system routines) on the new machine.
1.5.13 [H] As I write up this solution, my computer is a PC running Linux. I used the
vi text editor to type the text of the program into a file named hello.f, changing the
programmer’s name to my own. Then I followed the sample terminal session in § 1.3 to
compile the program and run it, as shown by the following conversation.
mike[1243] vi hello.f
mike[1244] more hello.f
C
Code by Michael
Kupferschmid C
C This program says hello.
C
PRINT *,’Hello,
world!’ STOP
END
mike[1245] g77 hello.f
mike[1246] a.out
Hello, world!
mike[1247]
My UNIXTM prompt is my login name, which is mike, followed by a command number in
square brackets. I had been using my machine for a while on the day that I did this, and
the command number had reached 1243 when I began. To show what I entered in the file
using vi, I used the UNIXTM more command to list its contents. The name of the Linux
compiler is g77, so I used that to compile the program. Then I entered the name of the
executable produced by the compiler, a.out, to run the program.
,8 Classical FORTRAN Solutions Manual
1.5.14 [E] According to the Index (page 552 of the textbook) the PROGRAM statement is
discussed in §13.1.5. The statement was omitted from this chapter because in the UNIX TM
environment that we have assumed, it adds nothing and has the potential to be confusing.
PROGRAM was left out of the § 1.4 table of statements because we do not consider it a part of
Classical FORTRAN .
, Solutions to the Exercises 9
Expressions and Assignment Statements
2.9.1 [E] The only statements that can be interchanged in the hypotenuse program without
affecting its results are the assignments to A and B.
2.9.2 [E] The FORTRAN variable GOD is real, because it does not begin with I, J, K,
L, M, or N. Whether God is real is beyond the scope of this text; you will have to decide for
yourself.
2.9.3 [E] It is not necessary for the value of a F ORTRAN variable to change during the
execution of a program; it can retain the same value throughout the calculation. F ORTRAN
considers variables beginning with the letters I, J, K, L, M, and N to be integers by default.
A space is not a legal character in a variable name, so variables cannot have names such as
ITEM A (but see Exercises 1.5.3 and 3.8.13). It is legal to use the same variable name
for more than one quantity, but as we shall see in §12.4.2 that is usually not a good idea.
2.9.4 [E] (a) [e1] 3.2 = e1×3.2 = e3.2 so the expression can be rewritten EXP(3.2). The
numerical value of EXP(3.2) is 24.5325317, so the expression could instead simply be
replaced by that number. (b) [e3.2] 3.2 = e3.2×3.2 = e3.2 2so the expression can be rewritten
EXP(3.2**2) or replaced by 28001.1465, which is its numerical value. (c) 3.22 = 10.24,
so EXP(3.2**2) can in turn be rewritten EXP(10.24) or replaced by 28001.1465 as in
(b).
(d) eln (A) = A and eln (B) = B, so EXP(ALOG(A)+ALOG(B)) can be replaced by A*B.
2.9.5 [H] It is to be shown that I/J/K/L=I/(J*K*L) even though integer division trun-
cates. The following proof is due to M. S. Krishnamoorthy.
In number theory, truncation is just the “floor” operation, ⌊a, b⌋, yielding the smallest inte-
ger not less than the real quotient a/b. Thus it suffices to prove that ⌊⌊x, i⌋, j⌋ = ⌊x, (i × j)⌋.
Let x = p × i + q, 0 ≤ q < i; then ⌊x, i⌋ =
p. Let p = r × j + s, 0 ≤ s < j; then ⌊p, j⌋
= r.
Now consider the right-hand side, ⌊x, (i × j)⌋. Substituting, we find this.
x= r×i×j +s×i+q
Because j > 0, we can multiply thru the second inequality above to obtain 0 ≤ s × i < j ×
i. Then s × i + q < j × i + q. But 0 ≤ s < j, so the largest the integer s can be is j
— 1 and s × i + q ≤ (j — 1) × i + q. Similarly, because 0 ≤ q < i, the largest the integer
q can be is i — 1. Then s × i + q ≤ j × i — i + (i — 1) = i × j — 1. Thus s × i + q
< i × j and
⌊x, (i × j)⌋ = r, which completes the proof. ✷