Programming Example: Computing Means

Problem Statement

Given three real numbers, its arithmetic mean (average), geometric mean and harmonic mean are defined as follows:

Write a program to compute and display the means of three REAL variables initialized with positive real values.

Solution

! -------------------------------------------------------
!   Computes arithmetic, geometric and harmonic means
! -------------------------------------------------------

PROGRAM  ComputeMeans
   IMPLICIT  NONE

   REAL  :: X = 1.0, Y = 2.0, Z = 3.0
   REAL  :: ArithMean, GeoMean, HarmMean

   WRITE(*,*)  'Data items: ', X, Y, Z
   WRITE(*,*)

   ArithMean = (X + Y + Z)/3.0
   GeoMean   = (X * Y * Z)**(1.0/3.0)
   HarmMean  = 3.0/(1.0/X + 1.0/Y + 1.0/Z)

   WRITE(*,*)  'Arithmetic mean = ', ArithMean
   WRITE(*,*)  'Geometric mean  = ', GeoMean
   WRITE(*,*)  'Harmonic Mean   = ', HarmMean

END PROGRAM ComputeMeans
Click here to download this program.

Program Output

 Data items: 1.,  2.,  3.

 Arithmetic mean = 2.
 Geometric mean  = 1.81712067
 Harmonic Mean   = 1.63636363

Discussion