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 ....5....0....5....0....5....0....5 Input Data a = 1.0000000 b = 2.0000000 c = 3.0000000 Computed Results Arithmetic Mean = 2.0000000 Geometric Mean = 1.8171207 Harmonic Mean = 1.6363636
Click here to download this program.PROGRAM Means IMPLICIT NONE REAL :: a, b, c REAL :: Am, Gm, Hm CHARACTER(LEN=*), PARAMETER :: FMT = "(1X, A//3(1X, A, F15.7/))" READ(*,*) a, b, c Am = (a + b + c)/3.0 Gm = (a * b * c)**(1.0/3.0) Hm = 3.0 / (1.0/a + 1.0/b + 1.0/c) WRITE(*,FMT) "Input Data", & "a = ", a, & "b = ", b, & "c = ", c WRITE(*,FMT) "Computed Results", & "Arithmetic Mean = ", Am, & "Geometric Mean = ", Gm, & "Harmonic Mean = ", Hm END PROGRAM Means
The output of the program is:1 1 2 2 3 ....5....0....5....0....5....0 1.0 2.0 3.0
1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data a = 1.0000000 b = 2.0000000 c = 3.0000000 Computed Results Arithmetic Mean = 2.0000000 Geometric Mean = 1.8171207 Harmonic Mean = 1.6363636
Thus, the title is printed with A. Then, we see the first / forces the end of the first output line. The next / forces the end of the second output line.(1X, A//....)
For each output, since it has a form of character followed by a real. We can use 1X, A, F15.7. Since each of these output is on its own line, we need a / to terminate the current line. As a result, printing one output line requires 1X,A,F15.7/. Since we have three such output lines, we can repeat this group three times 3(1X,A,F15.7/). Appending this part to the format, we have
Note that 1X is used to skip the first position for printer control.(1X, A//3(1X, A, F15.7/))
Once this WRITE starts, Fortran begins format scanning with 1X which skips the first position of the first output line. Then, Fortran sees A, which requires a value. Thus, scanning stops here (temporarily) and looks for a value. The next value to be printed is the CHARACTER string "Input Data". Therefore, it is printed with A. Then, format scanning continues and sees /, which forces to end the first output line. Format scanning continues and sees the second /, which forces the end of the second output line. Up to now, we have the following output:CHARACTER(LEN=*), PARAMETER :: FMT = "(1X, A//3(1X, A, F15.7/))" WRITE(*,FMT) "Input Data", & "a = ", a, & "b = ", b, & "c = ", c
1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data <----- printed by 1X, A / <----- printed by the second /
1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data <----- printed by 1X, A / <----- printed by the second / a = 1.0000000 <----- printed by 1X,A,F15.7 /
1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data <----- printed by 1X, A / <----- printed by the second / a = 1.0000000 <----- printed by 1X,A,F15.7 / b = 2.0000000 <----- printed by 1X,A,F15.7 / c = 3.0000000 <----- printed by 1X,A,F15.7 /
This is the reason that there is a blank line between the line containing the value of c and the title line of the second part.1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data <----- printed by 1X, A / <----- printed by the second / a = 1.0000000 <----- printed by 1X,A,F15.7 / b = 2.0000000 <----- printed by 1X,A,F15.7 / c = 3.0000000 <----- printed by 1X,A,F15.7 / <----- printed by the end of format!
1 1 2 2 3 3 ....5....0....5....0....5....0....5 Input Data <----- printed by 1X, A / <----- printed by the second / a = 1.0000000 <----- printed by 1X,A,F15.7 / b = 2.0000000 <----- printed by 1X,A,F15.7 / c = 3.0000000 <----- printed by 1X,A,F15.7 / <----- printed by the end of format! Computed Results <----- printed by 1X, A / <----- printed by the second / Arithmetic Mean = 2.0000000 <----- printed by 1X,A,F15.7 / Geometric Mean = 1.8171207 <----- printed by 1X,A,F15.7 / Harmonic Mean = 1.6363636 <----- printed by 1X,A,F15.7 / <----- printed by the end of format!