# Computing and Printing the Input Data and Their Average

 WARNING: This example assumes the output is sent to a printer, and as a result, every formatted output contains printer control.

### Problem Statement

Write a program that reads in a set of values into a REAL array, computes the average, and prints a report like the following:
```         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
```

### Solution

```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(*,*)  (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
```

### Program Input and Output

If the input data consist of the following:
```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
```
The output of the program is:
``` 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
```

### Discussion

• Let us see how to print the first heading lines:
```         1    1    2    2    3
....5....0....5....0....5....0
Average Computation

Input Data
```
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:
```CHARACTER(LEN=30)          :: Title = "(A, A)"
```
The first heading is printed using the following WRITE:
```WRITE(*,Title)  " ", "Average Computation"
```
• The second heading can be printed the same way. More precisely, we print a " " followed by "Input Data" with the format stored in Title.
• Then, let us find a way to print the table heading and the data:
```         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
```
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:
```WRITE(*,Title)  " ", " No  Data "
WRITE(*,Title)  " ", "=== ======"
```
• Let us count the spaces in each of the data item. For the No, each integer uses three positions and therefore a I3 is a natural choice. But, since the first position has to be a space for advancing to the next line, we use I4. This will produce a space in the first position. For the Data part, it starts with the position next to the integer and has seven positions. The number is printed with two digits after the decimal point. Therefore, F7.2 should be used. Combining these two edit descriptors together, the format is (I4,F7.2):
```DO i = 1, ActualSize
WRITE(*,"(I4, F7.2)")  i, x(i)
END DO
```
• Finally, let us see how to print the average:
```         1    1    2    2    3
....5....0....5....0....5....0
Average =   1.7860001E+02
```
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:
```WRITE(*,"(A,A,ES15.7)")  " ", "Average = ", Average
```
• There are several blank lines in the output. Since listed-directed input/output can be mixed with formatted input/output, we simply use WRITE(*,*).