



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




D. Haugli, Lecturer Aer E 160 Aerospace Engineering
Formatted Output
Consider the variables step, velocity, units and direction. Assume step is an integer, velocity is real, units is a character string and direction is logical and that the values of each are,
units = "ft/s" step = 14 direction = .true. velocity = 14.
List-directed write statements of the form,
WRITE (,) "At step ", step, ", the velocity is ", velocity, units,"." WRITE (,) "Upward? ", direction, "."
would create output (to the screen) that might look something like this:
At step 14 , the velocity is 14.42200 ft/s Upward? T.
The output, while in the correct order, contains extra spaces and extra digits (on velocity). How could the format be improved (i.e., controlled)? Through the use of FORMAT statements:
WRITE (,10) step, velocity, units 10 FORMAT (1X,"At step ",I2,", the velocity is ",F6.3,1X,A4,".") WRITE (,12) direction 12 FORMAT (1X,"Upward? ",L1,".")
These statements would produce the output,
At step 14, the velocity is 14.422 ft/s. Upward? T.
This output is free from extra spaces and has the correct number of digits for velocity.
Statement Numbers. FORMAT statements are preceded by a number (sometimes called a label ), and that number is used in WRITE statements to refer to the FORMAT statement. A single FORMAT statement may be referenced multiple times (i.e., with more than one WRITE statement). The number can be anywhere from 1 to 99999 and is at the discretion of the programmer.
Suggestions:
D. Haugli, Lecturer Aer E 160 Aerospace Engineering
D. Haugli, Lecturer Aer E 160 Aerospace Engineering
The I Descriptor. This descriptor controls the format of INTEGER strings and has the form I#, where # is the number of digits to print out. Examples:
Code Segment: INTEGER :: i, j, k i = 1000 j = 12 k = 519 WRITE (*,7) i, j, k 7 FORMAT (3(1X,I3))
Output: *** 12 519
Notes:
The F Descriptor. This descriptor controls the format of REAL strings and has the form F#.#, where left # is the number of digits to print out (including the decimal point) and the right # is the number of decimal places. Examples:
Code Segment: REAL :: a, b, c, d, e a = 97. b = 0. c = 51900. d = 713133. e = 15. WRITE (*,9) a, b, c, d, e 9 FORMAT (1X,F4.1,1X,F6.4,2(1X,F7.1),1X,F4.1)
Output: 97.5 0.0001 51900.0 ******* 15.
Notes:
D. Haugli, Lecturer Aer E 160 Aerospace Engineering
The E Descriptor. This descriptor causes REAL strings to be printed in exponential form, and has the form E#.#, where left # is the number of digits to print out (including the decimal point and power of ten) and the right # is the number of decimal places. Examples:
Code Segment: REAL :: a, b, c, d, e a = 97. b = 0. c = 51900. d = 713133. e = 15. WRITE (*,11) a, b, c, d, e 11 FORMAT (1X,E9.3,1X,E7.1,2(1X,E11.5),1X,E4.1)
Output: 0.975E+02 0.1E-03 0.51900E+05 0.71313E+06 ****
Notes:
The L and / Descriptors. This descriptor controls the format of LOGICAL strings and has the form L#, where # is the number of spaces to output with the values "T" or "F". The / descriptor causes output to be written to a new line. Examples:
Code Segment: LOGICAL :: answer, switch answer = .true. switch = .false. WRITE (*,13) answer, switch 13 FORMAT (1X,"The answer is ",L1,"."/1X,"The switch is on ",L5,".")
Output: The answer is T. The switch is on F.
Notes:
13 FORMAT (1X,"The answer is ",L1,".",/,1X,"The switch is on ",L5,".")