# Computing Means - Revisited

### Problem Statement

The arithmetic, geometric and harmonic means of three positive numbers are defined by the following formulas:

Write a program to read three positive numbers and use three functions to compute the arithmetic, geometric and harmonic means.

### Solution

```! ----------------------------------------------------------
!    This program contains three functions for computing the
! arithmetic, geometric and harmonic means of three REALs.
! ----------------------------------------------------------

PROGRAM  ComputingMeans
IMPLICIT  NONE

REAL  :: a, b, c

WRITE(*,*) 'Input: ', a, b, c
WRITE(*,*)
WRITE(*,*) 'Arithmetic mean = ', ArithMean(a, b, c)
WRITE(*,*) 'Geometric mean  = ', GeoMean(a, b, c)
WRITE(*,*) 'Harmonic mean   = ', HarmonMean(a, b, c)

CONTAINS

! ----------------------------------------------------------
! REAL FUNCTION  ArithMean() :
!    This function computes the arithmetic mean of its
! three REAL arguments.
! ----------------------------------------------------------

REAL FUNCTION  ArithMean(a, b, c)
IMPLICIT  NONE

REAL, INTENT(IN) :: a, b, c

ArithMean = (a + b + c) /3.0
END FUNCTION  ArithMean

! ----------------------------------------------------------
! REAL FUNCTION  GeoMean() :
!    This function computes the geometric mean of its
! three REAL arguments.
! ----------------------------------------------------------

REAL FUNCTION  GeoMean(a, b, c)
IMPLICIT  NONE

REAL, INTENT(IN) :: a, b, c

GeoMean = (a * b * c)**(1.0/3.0)
END FUNCTION  GeoMean

! ----------------------------------------------------------
! REAL FUNCTION  HarmonMean() :
!    This function computes the harmonic mean of its
! three REAL arguments.
! ----------------------------------------------------------

REAL FUNCTION  HarmonMean(a, b, c)
IMPLICIT  NONE

REAL, INTENT(IN) :: a, b, c

HarmonMean = 3.0 / (1.0/a + 1.0/b + 1.0/c)
END FUNCTION  HarmonMean

END PROGRAM  ComputingMeans
```

### Program Input and Output

The following is the output from the above program.
```Input: 10.,  15.,  18.

Arithmetic mean = 14.333333
Geometric mean  = 13.9247675
Harmonic mean   = 13.5
```

### Discussion

• Each of these functions is simple and does not require further explanation.
• Note that the main program and all three functions use the same names a, b and c. By Scope Rule 3, they are all different entities. That is, when function ArithMean() is using a, it is using its own local formal argument rather than the global variable a declared in the main program.