MODULE Simple_Stats IMPLICIT NONE INTEGER, SAVE :: mean_use = 0 INTEGER, SAVE :: std_dev_use = 0 CONTAINS REAL FUNCTION mean(vec) REAL, INTENT(IN), DIMENSION(:) :: vec mean = SUM(vec)/REAL(SIZE(vec)) mean_use = mean_use + 1 END FUNCTION mean REAL FUNCTION Std_Dev(vec) REAL, INTENT(IN), DIMENSION(:) :: vec Std_Dev = SQRT(SUM((vec-mean(vec))**2)/REAL(SIZE(vec))) std_dev_use = std_dev_use + 1 END FUNCTION Std_Dev END MODULE Simple_Stats
The test program:
PROGRAM Test_Stats USE Simple_Stats IMPLICIT NONE REAL, ALLOCATABLE, DIMENSION(:) :: X INTEGER :: n_elts, istat DO PRINT*, "How big is the array? " READ*, n_elts IF (n_elts == 0) EXIT ALLOCATE(X(n_elts),STAT=istat) IF (istat .NE. 0) THEN PRINT*, "Allocation request failed" ELSE PRINT*, "Type in your ",n_elts," numbers" READ*, X PRINT*, "Mean = ", Mean(X) PRINT*, "Std Dev = ", Std_Dev(X) DEALLOCATE(X) END IF END DO PRINT*, "Function ""Mean"" used", mean_use, "times." PRINT*, "Function ""Std_Dev"" used", std_dev_use, "times." END PROGRAM Test_Stats