To add remarks, comments, or other annotations, type the letter c or
C in column 1, followed by the
annotation Every line of your annotation must have a c in column 1.
All commands begin in column 7 and may not extend past column 72.
Statement numbers go in columns 1-5. You may give any command
line a statement number, even
though this line is never addressed in the program by a command. See Logic Statements below.
Anything in column 6 indicates a continuation from the preceding line. For example: & or +.
It is suggested that you explain what the program is and the meaning
of some of the variable names at
the beginning of the program or where appropriate in the program.
The first executable lines must be for defining storage space in memory
for any matrices (indexed or sub-
scripted variables) if you are going to use them, or for changing the integer or real default status of your
The last command of your program must be "end"
Real and Integer Variables
By default, all variable beginning with the letters a through h and
o through z are real ( also called
floating point), that is, they may have decimal point values.
By default, all variable beginning with i through n are integer, that is, they can not have decimal values.
E.G..: Integer adat, Real index.
The variable "adat" is real by default, but the statement "integer adat"
converts it to a integer variable.
The variable "index" is integer by default, but the statement "real index" converts it to a real or floating
Precision of Numbers and Variables
The default precision of numbers, called single precision, is 8 digits.
So extremely small numbers like
0.000000012 will be rounded off to 0.0. To increase the precision (number of digits) and sizes for variables, declare them as follows:
Real *8 var1, var2, etc.
This is called double precision and you can get up to 17 digits this
way. Another way to declare double
precision is to declare a variable as follows:
double precision var1, var2, etc
Constants may be entered in two formats, e. g., the speed of light in km/s:
c= 299000, or c=2.990E5. Avoid c = 2.990 * 10**5. Do not put commas in numbers.
Also, you may enter a number in double precision this way:
1.2345D5 instead of 1.2345E5
You may use upper or lower case letters for commands
However, some compilers are case sensitive. Therefore, for variables, be consistent. Once you
introduce a variable, continue to write it exactly the same way everywhere in the program, or you
could run into serious problems.
Defining an array of memory spaces:
To set up an array
of indexed variables such as x(i), use the Dimension
Statement as follows:
Dimension x(20), a(20), b(20), etc.
The above statement
allows you to assign 20 different values for x, a, or b, which must be
indexed in the program. For example x must be written as x(i) in the program. One could use
any integer variable for the index such as j, k, l, m, jj, etc. One may use a specific value for x
by writing, for example, y=3*x(8).
Opening and Labeling Files
Opening files must
come after dimension statements and declaration of real and integer variables.
The following command opens a file named Input.txt:
Open (1, file = 'Input.txt')
This file is identified as "1" elsewhere in the program when reading the file
x, y, z etc. The asterisk means unformatted or as is. Files
may be formatted as
Entering data from the keyboard.
The keyboard is the
default input device, indicated by an asterisk, *.
It is suggested that you print a message on the monitor that tells you what to enter in the
print *, '
Enter values for x, y, and z, separated by commas'
read *, x,y,z
The program will pause when it encounters the above read command and begins when
you press the enter key. If the read command is for 3 paramters and you enter only 2 and
then press enter, the computer continues to pause until you have entered the correct
number of parameters.
You could read in 20 values of x from a file by using a "do loop," where x is indexed. For example:
100, i = 1, 20
read (1,*) x(i)
The above would read
20 different values of x into memory from an unformatted (*) file
labeled as 1.
See above how to label a file . The values of x must be a single column in the file.
The statement number 100 is written in columns 3, 4, and 5. If you wrote 100 in columns 1, 2, and 3,
it would be read as 10000.
To read values of x from a row in the file, use the command: read (1,*) x(i), (i=1, 20)
An alternative to a do loop is "do while":
DO WHILE (PHI.LT.360.0)
Or and "if" statement:
IF (PHI.LT.360) then
continue ( or alternative command lines go here)
Note: the arguments of trig functions must
be in radians. In the above commands, the
angle PHI (in degrees) is dividid by 57.29 to convert to radians.
Notes on formatting output or input:
First declare an
output/input file which can be imported into WORD or PICO for editing and
or read into the program.
open (2, file=' output.dat')
To read the data in the file:
read (2,*) x, y, z
The asterisk means to read the file as
is and not according to some specified format. If you want to
format the data in the file in some specific way then use:
read (2,121) x,y,z
In the above 121 identifies the format
statement such as the one given below as 101. Also see below
for how to format data and set up a format statement.
To write a row of column headings into an output file, such as,
Index Var1 Var2 X DELX OUTPUT,
Put the following command on a line somewhere
before you write the actual numbers to the file,
This needs to be done before a do loop in which the actual data printing is done:
101 format ( 10x,' Index', 8x,'Var1',8x, 'Var2',12x, 'X', 12x,'DELX', 8x,'OUTPUT', / )
The different values of nx (here 10x)
is the number of spaces to skip between headers and this
is determined by trial and error. The slash skips a line.
A format statement, like the one
above, describes how the data are or will be formated
or arranged in an input or output file.
To write the data to this file correctly
aligned in the columns, use the following output
command in a Do loop after all variables are computed but before any variables are
incremented in the loop:
write (2,102) I, Var1, Var2, X, DELX, OUTPUT
102 format (5x, I5, 2(f10.4,5x), f8.3, 5x, f6.3, 6x, f10.4)
You need to determine the values of nx
by trial and error so that all decimal places
are in the same column for each field of data.
"I5" formats the variable I as an integer
number (no decimal point) that is less than
5 spaces long. This allows I to be a value between 1 and 9999.
If more than 1 variable is of the same
format, you may lump together the size of the
fields, e.g. f10.4, and the spaces between them, nx, as was done above for Var1 and Var2,
viz., 2(f10.4, 5x). In the term "f10.4", f means floating point or a number with decimals,
10 is the size of the field in spaces, and 4 is the number of decimal places in that field.
For example, when f10.4 is used for the number 502.3245, the decimal point is always
counted as one of the 10 spaces in the field. So the total field taken up by the above
number is 8 spaces. The 2 other unused spaces in the f10 field are before the 5 not after
the last decimal place. You need to take this into account when deciding what the
values are for nx to align the columns of data under the header of the table.
All results from
your program must be professionally formatted in tables as explained
If a table is longer than 1 page, the headers must be placed on each page.
You can control this
by setting up a counter and when the value exceeds
a certain number of lines, you skip a page with the command
write (2, 103)
103 format (2x, ////) ,
where the number
of slashes is the remaining lines to be skipped at
the bottom of the page + lines to be skipped at top of next page. This is found by trial
Setting up a counter
within a do loop and use what is called an "If - Else -End" logic
sequence as shown below:
Do 500 i =1, N
write output to file command goes here, followed by:
If (J.eq.50) then
103 format format (2x, ////)
( the following commands resets the counter for the next page and sets up the printing of the
column headings again )
104 format (2x, //, 'Table 2 Continued',/)
When writing to a
file, each time a WRITE command is encountered during execution,
such as in a DO loop, values are written in the file as a new row, in successive order.
Only when you close a file and then reopen it, or rerun the program, will you begin
writing into the file at row 1.
At the end of a program,
you should close each file that you have opened by the following
This closes file 2.
The last command of your program must be "end"
A sample program
is given at the end of this document. You should try to compile and
the program. However, to make it work you will need to type up an input file of fictious data.
Library of Math Functions:
ex : exp(x)
ln x: alog(x)
sqare root of x: sqrt(x)
log10 x: alog10(x)
cos x : cos(x)
sin x : sin(x), etc. for other trig functions.
arctan x : atan(x) (the angle x must be in radians, not degrees)
Very large numbers
may be entered thusly: y= 2.567E22, which is the equivalent
of 2.567 x 1022.
The numerical limit for a variable is 1030 unless you declare it as real *8. See above.
C This is a program
that computes the standard deviation of a set of measurements that should
C obey the law: v=0.35+0.72*t(i)+5.30*t(i)**2, where i indexes a specific value for t. Corresponding
C measurements of v and t are read from an input file named input.txt. The number of lines of data
C in the input file is N, which is the first number in the file.
Dimension t(100), vmeas(100) or real t(100), etc
Open (1, file='Input.txt')
read (1,*) N
Do 100, i=1,N
read (1,*) t(i), vmeas(i)
print *, t(i),vmeas(i)
Sum = 0.0
Do 500 i=1,N
vcomp = 0.35+0.72*t(i)+5.30*t(i)**2
vres= vmeas(i) - vcomp
Sum = Sum +vres**2.0
print *, i, t(i),vmeas(i),vcomp,vres,Sum
print *, ' Std. Dev.=',sigma