!//////////////////////////////////////////////////////////////
!//
!// mean_func.f90
!//
!// Function to calculate the mean value from
!// a data set
!//
!//////////////////////////////////////////////////////////////

FUNCTION mean(array) RESULT(X_M)
  !// Prevent implicit declarations of vatriables
  IMPLICIT NONE
  !// Argument declarations
  !// An array of real values N items long
  REAL,DIMENSION(:), INTENT(IN)            :: array
  !// The result of the calculations returned
  !// to the calling procedure
  REAL                                     :: X_M
  !// Variable declarations
  !// A help variable
  REAL                                     :: s
  !// A variable to contain the length of the array
  INTEGER                                  :: array_length

  !// An index variable
  INTEGER                                  :: i
  !// Get the legnth of the array
  array_length = SIZE(array)
  !// Initialze the variable s to zero. 
  !// Note the . after the 0
  !// This tells the compiler that this is a floating point
  !// value and not an integer
  s = 0.
  !// Loop through all elements in the array
  DO i = 1, array_length
    !// Sum up the value of all the elements
    s = s + array(i)
  END DO
  !// Calculate the mean value of the array
  X_M = s/array_length
  !// Display the mean value on the screen
  PRINT *, X_M
END FUNCTION mean
