PROGRAM Processing_Failure_Times_2
!-----------------------------------------------------------------------
! Program to read a list of failure times, calculate the mean time to
! failure, and then print a list of failure times that are greater
! than the mean.  An allocatable array is used to store the failure 
! times.  Identifiers used are:
!     FailureTime          : one-dimensional array of failure times
!     NumTimes             : size of the array
!     I                    : subscript
!     Sum                  : sum of failure times
!     Mean_Time_to_Failure : mean of the failure times
!
! Input:  A list of NumTimes failure times
! Output: Mean_Time_to_Failure and a list of failure times greater
!         than Mean_Time_to_Failure
!-----------------------------------------------------------------------

  IMPLICIT NONE
  CHARACTER(20) :: FileName
  REAL, DIMENSION(:), ALLOCATABLE :: FailureTime
  INTEGER :: NumTimes, I, OpenStatus, AllocateStatus,InputStatus
  REAL :: Sum, Mean_Time_to_Failure

  ! Get the name of the data file and open it for input

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "Enter name of the data file "
  READ *, FileName
  OPEN (UNIT = 10, FILE = FileNAme, STATUS = "OLD", IOSTAT = OpenStatus)
  IF (OpenStatus > 0) STOP "*** Cannot open the file ***"
  ! Get the number of failure times and allocate an array
  ! to store the failure times
  READ (10, IOSTAT = InputStatus) NumTimes
  IF (InputStatus > 0) STOP "*** Input Error ***"
  IF (InputStatus < 0) STOP "*** Not enough data ***"
  ALLOCATE(FailureTime(NumTimes), STAT = AllocateStatus)
  IF (AllocateStatus /= 0) STOP  "*** Not enough memory ***"

  ! Read the failure times and store them in array FailureTime

  PRINT *, "Enter the ", NumTimes, "failure times, as many per line &
          &as desired"
  READ *, FailureTime

  ! Calculate the mean time to failure

  Sum = 0.0
  DO I = 1, NumTimes
     Sum = Sum + FailureTime(I)
  END DO
  Mean_Time_to_Failure = Sum / REAL(NumTimes)
  PRINT '(/ 1X, "Mean time to failure =", F6.1)', Mean_Time_to_Failure

  ! Print list of failure times greater than the mean

  PRINT *
  PRINT *,  "List of failure times greater than the mean:"
  DO I = 1, NumTimes
     IF (FailureTime(I) > Mean_Time_to_Failure) &
        PRINT '(1X, F9.1)', FailureTime(I)
  END DO

! Deallocate the array of failure times
  DEALLOCATE(FailureTime)
  STOP
END PROGRAM Processing_Failure_Times_2
