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 ....5....0....5....0....5....0 Average Computation Input Data No Data === ====== 1 100.00 2 231.00 : : : 14 250.00 15 379.00 Average = 1.7860001E+02
Click here to download this program.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(*,*) WRITE(*,Title) " ", "Input Data" WRITE(*,*) WRITE(*,Title) " ", " No Data " WRITE(*,Title) " ", "=== ======" DO i = 1, ActualSize WRITE(*,"(I4, F7.2)") i, x(i) END DO WRITE(*,*) WRITE(*,"(A,A,ES15.7)") " ", "Average = ", Average END PROGRAM Mean
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 No Data === ====== 1 100.00 2 231.00 3 67.00 4 179.00 5 315.00 6 78.00 7 111.00 8 410.00 9 98.00 10 25.00 11 245.00 12 90.00 13 101.00 14 250.00 15 379.00 Average = 1.7860001E+02
Since we need a space in the first position followed by the string "Average Computation", there are two CHARACTER items (a space is a character). Therefore, a possible format is "(A,A)", where the first A is for printing the space " " while the second A is for printing "Average Computation". For convenience, we define a CHARACTER variable Title for this purpose:1 1 2 2 3 ....5....0....5....0....5....0 Average Computation Input Data
The first heading is printed using the following WRITE:CHARACTER(LEN=30) :: Title = "(A, A)"
WRITE(*,Title) " ", "Average Computation"
The table heading can be printed the same way as the first two lines, although it appears to have three items, a space, No and Data. The trick is combining the last two into a single string. In this way, we still are printing two CHARACTER items, a space and " No  Data". Please note that there is a space before N and two spaces between o and D. Therefore, the two table heading lines are printed using:1 1 2 2 3 ....5....0....5....0....5....0 No Data === ====== 1 100.00 2 231.00 : : : 14 250.00 15 379.00
WRITE(*,Title) " ", " No Data " WRITE(*,Title) " ", "=== ======"
DO i = 1, ActualSize WRITE(*,"(I4, F7.2)") i, x(i) END DO
We need a space, a string "Average = " and a REAL number. The first two are easy, two As would be sufficient. For the average, we choice to use ES as an example. Since w must be greater than or equal to d+7. If we want to have seven digits as shown, d is 7 and w must be at least 14. We choice 15 and therefore, the format is (A,A,ES15.7). The following is the statement:1 1 2 2 3 ....5....0....5....0....5....0 Average = 1.7860001E+02
WRITE(*,"(A,A,ES15.7)") " ", "Average = ", Average