Computing Arithmetic, Geometric and Harmonic Means

Problem Statement

The arithmetic mean (i.e., average), geometric mean and harmonic mean of a set of n numbers x1, x2, ..., xn are defined as follows:

Since computing geometric mean requires taking square root, it is further required that all input data values must be positive. As a result, this program must be able to ignore those non-positive items. However, this may cause all input items ignored. Therefore, before computing the means, this program should have one more check to see if there are valid items.


! ----------------------------------------------------------
! This program reads a series of input data values and
! computes their arithmetic, geometric and harmonic means.
! Since geometric mean requires taking n-th root, all input
! data item must be all positive (a special requirement of
! this program , although it is not absolutely necessary).
! If an input item is not positive, it should be ignored.
! Since some data items may be ignored, this program also
! checks to see if no data items remain!
! ----------------------------------------------------------

PROGRAM   ComputingMeans

   REAL    :: X
   REAL    :: Sum, Product, InverseSum
   REAL    :: Arithmetic, Geometric, Harmonic
   INTEGER :: Count, TotalNumber, TotalValid

   Sum        = 0.0                     ! for the sum
   Product    = 1.0                     ! for the product
   InverseSum = 0.0                     ! for the sum of 1/x
   TotalValid = 0                       ! # of valid items

   READ(*,*)  TotalNumber               ! read in # of items
   DO Count = 1, TotalNumber            ! for each item ...
      READ(*,*)  X                      ! read it in
      WRITE(*,*) 'Input item ', Count, ' --> ', X
      IF (X <= 0.0) THEN                ! if it is non-positive
         WRITE(*,*) 'Input <= 0.  Ignored'   ! ignore it
      ELSE                              ! otherwise,
         TotalValid = TotalValid + 1    ! count it in
         Sum        = Sum + X           ! compute the sum,
         Product    = Product * X       ! the product
         InverseSum = InverseSum + 1.0/X     ! and the sum of 1/x
      END IF

   IF (TotalValid > 0) THEN             ! are there valid items?
      Arithmetic = Sum / TotalValid     ! yes, compute means
      Geometric  = Product**(1.0/TotalValid)
      Harmonic   = TotalValid / InverseSum

      WRITE(*,*)  'No. of valid items --> ', TotalValid
      WRITE(*,*)  'Arithmetic mean    --> ', Arithmetic
      WRITE(*,*)  'Geometric mean     --> ', Geometric
      WRITE(*,*)  'Harmonic mean      --> ', Harmonic
   ELSE                                 ! no, display a message
      WRITE(*,*)  'ERROR: none of the input is positive'

END PROGRAM  ComputingMeans
Click here to download this program.

Program Input and Output

This program uses the same format of input as discussed in previous example. More precisely, the first line gives the number of data items, followed by that number of input line on each which is a data value. Except for the first input value, which gives the number of input, all other values are real numbers.


This example is quite simple and does not require further explanation.