WARNING: This example assumes the output is sent to a printer, and as a result, every formatted output contains printer control.
1 1 2 2 3 3 4 4 5
....5....0....5....0....5....0....5....0....5....0
Average Computation
Input Data
100.00 231.00 67.00 179.00 315.00
78.00 111.00 410.00 98.00 25.00
245.00 90.00 101.00 250.00 379.00
Average = 1.7860001E+02
This problem is identical to the previous
one; however, we want to print the input five items per row as shown above.
PROGRAM Mean
IMPLICIT NONE
INTEGER, PARAMETER :: SIZE = 20
REAL, DIMENSION(1:SIZE) :: x
INTEGER :: ActualSize
INTEGER :: i
REAL :: Average
CHARACTER(LEN=30) :: Title = "(A, A)"
READ(*,*) ActualSize
READ(*,*) (x(i), i = 1, ActualSize)
Average = 0.0
DO i = 1, ActualSize
Average = Average + x(i)
END DO
Average = Average / ActualSize
WRITE(*,Title) " ", "Average Computation"
WRITE(*,Title) " "
WRITE(*,Title) " ", "Input Data"
WRITE(*,Title) " "
WRITE(*,"(5F10.2)") (x(i), i = 1, ActualSize)
WRITE(*,Title) " "
WRITE(*,"(A, A,ES15.7)") " ", "Average = ", Average
END PROGRAM Mean
Click here to download this program.
The output of the program is:15 100.0 231.0 67.0 179.0 315.0 78.0 111.0 410.0 98.0 25.0 245.0 90.0 101.0 250.0 379.0
Average Computation
Input Data
100.00 231.00 67.00 179.00 315.00
78.00 111.00 410.00 98.00 25.00
245.00 90.00 101.00 250.00 379.00
Average = 1.7860001E+02
1 1 2 2 3 3 4 4 5
....5....0....5....0....5....0....5....0....5....0
100.00 231.00 67.00 179.00 315.00
78.00 111.00 410.00 98.00 25.00
245.00 90.00 101.00 250.00 379.00
We see that each number is printed using 10 positions with
two positions for the fractional part. This means F10.2.
Putting five of them together gives a format "(5F10.2)".
Therefore, the WRITE statement should be:
Note here that the format rescanning rules are used. If the number of input data items is more than 5, then the first line contains x(1) to x(5) and is printed. The format is rescanned and the second line contains x(6) to x(10), and so on.WRITE(*,"(5F10.2)") (x(i), i = 1, ActualSize)
That is, the first F edit descriptor uses 11 positions rather than 10. Thus, an explicit printer control character is incorporated into the first edit descriptor.WRITE(*,"(F11.2,4F10.2)") (x(i), i = 1, ActualSize)