PROGRAM Mean_of_a_List_2
!-----------------------------------------------------------------------
! Program to read a list of numbers Item(1), Item(2), ... ,
! Item(NumItems) and to calculate their mean using the function 
! subprogram Mean.  Identifiers used are:
!   Item     : one-dimensional array of numbers
!   NumItems : number of items (named constant)
!   Mean     : function that finds the mean of a set of numbers
!
! Input:  NumItems and a list of NumItems real numbers
! Output: The mean of the numbers
!-----------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER, PARAMETER :: NumItems = 10
  REAL, DIMENSION(NumItems) :: Item

  PRINT *, "Enter the", NumItems, " real numbers:"
  READ *, Item
  PRINT '(1X, "Mean of the ", I3, " Numbers is ", F6.2)', &
        NumItems, Mean(Item, NumItems)
  
CONTAINS

  !-Mean----------------------------------------------------------------
  ! Function to find the mean of the NumElements elements of the array 
  ! X.  The size (NumElements) of X is passed as an argument.
  !
  ! Accepts:  Array X and NumElements
  ! Returns:  The mean of the numbers stored in X
  !--------------------------------------------------------------------
  
  FUNCTION Mean(X, NumElements)
  
    INTEGER, INTENT(IN) :: NumElements
    REAL, DIMENSION(NumElements), INTENT(IN) :: X
    REAL :: Mean
  
    Mean = SUM(X) / REAL(NumElements)
  
  END FUNCTION Mean

END PROGRAM Mean_of_a_List_2
