Up to this point, we have not formatted our output or specified how we want it to look.
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
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.
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:
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 *.
Example
Format specifiers may be used in a separate FORMAT statement.
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',/, + '-----------------------------------------',/)
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 END3. 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) END4. Given the following code
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.
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