9 FORMATTED INPUT/OUTPUT

Up to this point, we have not formatted our output or specified how we want it to look.

PRINT *, "The output values are ", var1, var2, var3

The * indicates list-directed output, output printed according to FORTRAN's built-in rules.

Formatted output allows the programmer to control the appearance or format of the output.

Example

Problem Statement
When a train rounds a level curve, it exerts a horizontal force, called centrifugal force, outward on the rails. Find the horizontal forces (to the nearest tenth) for speeds of 50 mph to 70 mph.

Input/Output Description
The user is asked to input the weight of the train in tons and the radius of the curve in meters. Each line of output contains the velocity (mph) and the force (Newtons) at that velocity.

Mathematical Equation
Centrifugal force (in Newtons) equals the mass (kg) times the square of the velocity (meters per second) divided by the radius (meters). Because the user is asked to input the weight of the train in tons and the velocity in miles per hour, the program converts miles per hour to meters per second and tons to kilograms using the following conversion factors:

Algorithm
1. Ask the user to enter the weight of the train in tons and the radius of the curve in meters.
2. Calculate the centrifugal force of the train on the tracks for velocities 50 to 70 mph.
3. Print the velocities and forces.

Code
      
  	PROGRAM force

        REAL weight, radius, force
        INTEGER mph

c       Ask the user to enter the weight of the train (tons) and the
c       radius of the curve (meters).

	PRINT *, 'Enter the weight of the train in tons.'
        READ *, weight
        PRINT *,'Enter the radius of the curve in meters.'
        READ *, radius

c       Converts the weight in tons to kilograms.

        mass = 1.016047e03 * weight

c       Prints a header for the output.
        PRINT *,'Velocity (mph)','   ','Centrifugal Force (Newtons)'

c       Computes the centrifugal force of a train on its tracks using
c       the formula force=(mass * velocity**2)/radius.  Each mph is
c       converted to meters per second by the conversion equations:
c       1 mile = 1.609344e03 meters and 1 hour = 3600 seconds.
c       The mph and forces are then printed.

        DO 10 mph=50, 70
           veloc = (1.609344e03/3600.) * real(mph)
           force=(mass*veloc**2)/radius
           PRINT 200, mph, force
200        FORMAT (4x,I2, 15x, F10.1)
10      CONTINUE

        STOP
        END

Output (56 tons was entered for the weight of the train and 15 meters was entered for the radius of the curve)

The format of the output was specified in the statement


200        FORMAT (4x,I2, 15x, F10.1)

The specifier

I2 and F10.1 are called format specifiers.

A format specifier tells the computer the horizontal spacing to be used when printing the output.

Table 1: Format Specifiers
Format
Specifiers
Used for Notes
Iw Integers w is the number of positions reserved for the printing of the integer
Fw.d Floating-point
numbers
w is the total width including the decimal point
d is the number of positions to the right of the decimal point
Ew.d Floating-point
numbers in
exponential
forms
w is the total width including the decimal point, leading numbers in zeros, positive/negative signs, and the E
d is the number of positions to the right of the decimal point
0.245E+02 has a total width of 9 with 3 decimal positions
Dw.d Double-precision
numbers
Double-precision values can be printed with an F or E format specifier, but use of the D indicates that the value is a double-precision value.
Aw Characters w is the total number of positions in the character value to be printed
nX blanks n specifies the number of blanks to be printed

Note:

Format specifiers may be used in a separate FORMAT statement.

The PRINT is followed by the label number of the FORMAT statement. The FORMAT statement can appear anywhere in a program after the variable declarations and before the END statement.

Note:
If there are more format specifiers than variables, as many of the format specifiers are used as needed; the rest are ignored. If there are more variables than there are format specifiers, the specifiers are used over again from the beginning until all variables have been printed. To avoid confusion, the number of format specifiers should match the number of variables.

Vertical Spacing (the slash)

Blank lines in output can be created with PRINT statements, i.e.
        PRINT *
        PRINT *
        PRINT *,'FORMAT SPECIFIER','    ','VALUE','    ','PRINTED OUTPUT'
        PRINT *,'--------------------------------------------'
        PRINT *

or, a slash (/) can be used. The / is a special format code that terminates the current line and positions the printing of the output on the next line. The following PRINT and FORMAT statements have the same output as the code above.

        PRINT 500
500     FORMAT (//,'FORMAT SPECIFIER',3x,'VALUE',3x,'PRINTED OUTPUT',/,
     +  '-----------------------------------------',/)

EXERCISES

1. Show how each value would be printed with the given format specifier: (Use ^ to indicate spaces.)

Table 3: Formated Output
Format Specifier Value Printed Output
I5 10
F6.0 0.0052
E10.4 357.101
F6.3 -123.456
F8.2 0.123-01
A5 Travis
E13.6 123.456E30
E9.2 -0.0005678
D10.2 7.1234567.10123D-08

2. Show exactly what would be printed by the following program (use ^ to indicate a blank):

        PROGRAM exercise2

        INTEGER year
        REAL avght, avgwt

        year = 1996
        avght=68.87
        avgwt=158.54

        PRINT '(1x,I4,3x,F6.2,F7.2)',year, avght, avgwt
        PRINT '(1x,I5,E11.4,E10.2)',year, avght, avgwt
        PRINT '("YEAR",1x,I4, 2(F7.2))',year, avght, avgwt

        END
3. Give the output for the following program.
        PROGRAM exercise3

        INTEGER read
        REAL press, vol, temp

        read=35
        press=16.8
        vol=4.07
        temp=503.6

        PRINT 100, read, press, vol, temp
        PRINT 200, read, press, vol, temp
        PRINT 300, read, press, vol, temp

c       Three different format statements.

100     FORMAT (//,' Reading = ',I4,/,' Pressure = ',F7.2,/,
     +  ' Volume = ', F7.2,/,' Temperature = ',F6.2,//)

200     FORMAT ('Reading: ',I4,/,'Pressure: ',F8.3,3x,'Volume: ',F8.3,
     +  3x,'Temperature: ',F8.3,//)

300     FORMAT ('Reading = ',I3,/,'------------',/,'P = ',F7.3,4x,'V = ',
     +  F7.3,4x,'T = ',F7.3)

        END
4. Given the following code
INTEGER hour, pulse
REAL temp
hour=12
pulse=89
temp=99.5

write the combination of PRINT and FORMAT statements that would produce the following output:

a. 12^^^^89^^^^99.5

b. At^hour ^^12,^the^pulse was^^^89^and^the^temperature^was^^99.5

c. Pulse:^^89
Temperature:^^99.5
Hour:^^^^12

5. Write a program to read a temperature in degrees Fahrenheit (F) and convert it to degrees Celsius (C), degrees Kelvin (K) and degrees Rankin (R). Print the output to two decimal places.

6. The radioactive decay of carbon-14 is given by the equation

where P represents the initial amount of carbon-14 and t represents the time elapsed in years. Assign P the initial value of 200.0 and let t range from 0 to 25000 years by increments of 1000 yrs. Then calculate and write the time elapsed and the amount of carbon-14 left after the specified time has elapsed to a file. The half-life (the time necessary for half the amount of a radioactive material to decay) of carbon-14 is 5,730 years. Use the following output form:

INITIAL VALUE OF CARBON-14:  xxx.xxx

  TIME ELAPSED      REMAINING AMOUNT OF CARBON-14
     xxxxx                xxx.xxx
     xxxxx                xxx.xxx