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

  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