Basic Scientific Programming
Format
Input/Output
There are two types of input/output statements in Fortran: List directed: format of input or output
is automatically supplied by the compiler.
Programmer-Formatted: format of input or output is supplied by the programmer.
Formatted Output
There are two output statements in Fortran, the print statement and the write statement.
Print: print format specifier, output-list
Write: write (control_list), output-list
Format Specifier
Format specifier is one of the following: * (an asterisk) A character constant or a character
variable whose value specifies the form of the output.
The label of a FORMAT statement.
* indicates a list directed output. As for the character constant or variable,
the formatting information is given the form: ‘(list of format descriptors)’
or “(list of format descriptors)”
Ex: print ‘(1X, I5, F8.2)’, Number,Temperature
The formatting information may be supplied by a format statement whose statement number is specified.
form label format(list of formal descriptors)where label is an integer in the range 1 through 99999.Ex: print 20, Number,temperature 20 format(1X, I5,F8.2)
1X, I5, F8.2 are format descriptors that specify the format in which the values of number and temperature are to be displayed.
Control Characters
Some compilers use the first character of each line of output to control vertical spacing.
Control Character Effect . blank Normal spacing, advances to the next line before printing 0 Double spacing, skip one line before printing 1 Advance to top of next page before printing + Overprint the last line printed
Print ‘(I3)’, NI3 indicates that the value to be printed is an integer, and is to be printed on the first three positions of a line.
In N=15 the three position are filled with b15. So it will be interpreted as normal spacing and print on the first 2 positions of the next line 15 --
If N=150 the first three positions will be 150. The 1 in the first position is removed and interpreted as a control character. So it will print 50 at the top of the new page.
To avoid confusion, we can explicitly indicate for each output line what printer control is desired. 1X or “ “ normal spacing “0” for double spacing “1” for advancing to a new page “+” for overprinting.Note: in all of the following examples, 1X will be used at the beginning of each output format specifier.
Integer Output (I Descriptor)
rIw or rIw.m
I: denotes integer data.w: integer constant indicating the width of the field in which the data is to be displayed.r: integer constant. Repetition indicator.m: minimum number of digits to be displayed.
Ex:
Integer:: number=3, L=5378,k=-12345 print ’(1X,2I5,I7,I10)’,number,number-3,L,kappa
Or print 30, number,number-3, L, kappa 30 format (1X,2I5,I7,I10)
_____3____0___5378____-12345 numbers will be right justified.
Ex: Print ’(1X,2I5.2,I7,I10.7)’,number,number-
3,L,kappa
____03___00___5378__-0012345
Print ’(1X,2I5.0,I7,I10)’,number,number-3,L,kappa _____3________5378____-12345
Print ’(1X,4I3)’,number,number-3,L,kappa ____3__0******
Real Output
rFw.d
F: denotes real (floating point) data.w: integer constant indicating the total width of the field in which the data is to be displayed.d: integer constant indicating the number of digits to the right of the decimal pointr: integer constant. Repetition indicator.
Ex:
Integer:: in=625, out = -19real:: a= 7.5, b=0.182, c=625.327
print 55, in,out,a,b,c55 format (1X,2I4,2f6.3,f8.3)
__625_-19_7.500_0.182_625.327
Ex.
Real:: beta = -567.89
print 20, 123.4 print 20, beta20 format (1X,F5.2)
***** *****
Real Output rEw.d or rEw.dEe
E: indicates that the data is to be output in exponential notation.w: integer constant indicating the total width of the field in which the data is to be displayed.d: integer constant indicating the number of digits to the right of the decimal pointr: integer constant. Repetition indicator.e: number of positions for displaying the exponent.
Character Output
Character constants may be displayed by including them in the list of descriptors of a format specifier.
Real:: a = 0.3, b = 7.9print ’(1X,”a=“,F6.2,” b=“,F6.2)’,a,b _a=__0.30_b=__7.90
print ’(1X,A,F6.2,A,F6.2)’,”a=“,a,” b=“,b will produce the same output.
Positional Descriptors (X and T)
Two format descriptors can be used to provide spacing in an output line:
X: inserts blanks on an output lineform nX where n is the # of blanks
T: similar to the tab keyform Tc where c is an integer denoting the number of a space on a line at which a field is to begin.
Ex:
Print 75, “John Q. Doe”, “CPSC”,10575 format(1X,A11,3X,A4,2X,I3)
or75 format(1X,A11,3X,T16,A4,2X,I3)
_John Q. Doe___CPSC__105
Repeating groups of format descriptors
3F10.2 is eqv. to F10.2,F10.2,F10.2
‘(1X,A,F6.2,A,F6.2)’ is eqv to ‘(1X,2(A, F6.2))’
‘(1X,F18.2,I3,A,I3,A, F18.2,I3,A,I3,A,F8.4)’
‘(1X,2(F18.2,2(I3,A,)),F8.4)’
The slash (/) descriptor
The slash causes the output to begin on a new line.
It can be used with a repetition indicator to skip several lines.
Print 85, “name”
print 85,”--------”print*print*print 85, first_name85 format(1X,A)
Print 85, “name”,”--------”,first_name85 format (1X,A,/1X,A,2/ 1X,A)