## 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

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.'
PRINT *,'Enter the radius of the curve in meters.'

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)
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

• 4x prints 4 blanks.
• I2 tells the computer to print the first variable of the PRINT list, mph, as a right-justified integer with two positions.
• 15x prints 15 blanks.
• F10.1 prints the second variable, force, as a floating-point number with a total of 10 positions, 1 of which is a decimal position (to the right of the decimal point).

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:

• The printing of a value is right-justified; blanks are inserted to the left of the value to fill in unused positions. In table 2 blanks are indicated by ^'s.
• If a value is too large to be printed in the specified format, a string of asterisks, indicating format overflow, will be printed.
• The E format is primarily used for very small values or very large values. It may also used when the magnitude of the number is uncertain.
• Real numbers are not truncated by the formatting, but are rounded off to the number of decimal places specified.

Table 2: Example of Format Specifiers
Stored Value Format Specifier Output
1.234567 F8.2 ^^^^1.23
0.00001 F5.3 0.000
-12345 I5 *****
12345 I6 ^12345
0.00001234 E10.3 ^0.123E-04
0.0001234 E11.4 ^0.1234E-03
-0.0001234 E8.3 ********
1234567.89 E9.2 ^0.12E+07
James A5 James
1.66587326432D+00 D17.10 ^0.1665873264D+01

Format specifiers may be used in a PRINT statement as a character string in place of the *.

mph=72
force=12.18

The format specifiers are enclosed in single quotes and parentheses. If a literal expression is to be printed, two single quotes must be used around the literal expression (see next example).

Example

PROGRAM example

REAL xval, yval, zval

xval=25.567
yval=18.314
zval=10.215

STOP
END

Output: x= 25.57 y= 18.31 z= 10.22
(Notice that the numbers have been rounded to two decimal places, not truncated.)

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

REAL press, vol, temp

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
```