PROGRAM Mean_of_a_List_3
!------------------------------------------------------------------
! Program to read a list of numbers Item(1), Item(2), ...,
! Item(NumItems) and calculate their mean using the external 
! function subprogram Mean.  Variables used are:
!   NumItems  : number of items
!   Item      : one-dimensional array of items
!   Mean      : external function to find the mean
!
! Input:  NumItems and a list of NumItems real numbers 
! Output: The mean of the numbers
!------------------------------------------------------------------

  IMPLICIT NONE
  REAL, DIMENSION (:), ALLOCATABLE :: Item
  INTEGER :: NumItems

  INTERFACE
    REAL FUNCTION Mean(X)
      REAL X(:)
    END FUNCTION Mean
  END INTERFACE

  WRITE (*, '(1X, A)', ADVANCE = "NO") & 
        "How many numbers are in the data set? "
  READ *, NumItems
  ALLOCATE(Item(NumItems))

  PRINT *, "Enter the", NumItems, " real numbers:"
  READ *, Item

  PRINT '(1X, "Mean of the ", I3, " Numbers is ", F6.2)', &
          NumItems, Mean(Item)
  DEALLOCATE(Item)      

END PROGRAM Mean_of_a_List_3


! Mean-------------------------------------------------------------
! External function to find the mean of the elements of the 
! array X.  Local variables used are:
!   Sum : sum of the numbers 
!   I   : subscript
!
! Accepts:  Assumed-shape array X
! Returns:  The mean of the numbers stored in X
!------------------------------------------------------------------
FUNCTION Mean(X)

  IMPLICIT NONE
  REAL :: Mean
  REAL, DIMENSION(:), INTENT(IN) :: X

  Mean = SUM(X) / REAL(SIZE(X))
    
END FUNCTION Mean
