Computing and Printing the Input Data and Their Average: Version 3

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    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.

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

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

    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

Discussion