PROGRAM Mean_of_a_List
!------------------------------------------------------------------
! Program to read a list of numbers Item(1), Item(2), ...,
! Item(NumItems) and calculate their mean using the function
! subprogram Mean.  Identifiers used are:
!   NumItems : number of items
!   Item       : one-dimensional array of items
!   Mean      : 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

  PRINT *, "Enter number of items:"
  READ *, NumItems
   
  ALLOCATE (Item(NumItems))

  PRINT *, "Enter the items:"
  READ *, Item

  PRINT 100, NumItems, Mean(Item)
  100 FORMAT(1X, "Mean of the ", I3, " numbers is ", F6.2)
      
END PROGRAM Mean_of_a_List


! Mean-------------------------------------------------------------
! Function to find the mean of elements of the array X.
!
! Accepts:  Array X
! Returns:  The mean of the numbers stored in X
!------------------------------------------------------------------

REAL FUNCTION Mean(X)

  REAL :: X(:)

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

