! This is a basic starting shell for your programs
INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0)
REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0
END MODULE const
! HW 15, 2
! Compute mean and standard deviation of a set of integers
INTEGER :: n,total1,rvalue
REAL(kind=dp) :: total2,avg,stdev
! 2 sum version
! For this program, the mean will be computed first. Once this is known,
! a second pass through the data file will be used to accumulate the deviations.
! For this to work, the data file needs to be rewinded between the two read loops.
! Set mean accumulation variable (total1) and standard deviation accumulation
! variable (total2) to 0. Set number of values to 0.
! Total1 can be an integer since the data is integer, but total2 needs to
! be a double.
total1 = 0
total2 = 0
n = 0
! Open the data file.
! First read loop to get the mean
total1 = total1 + rvalue
n = n + 1
avg = total1/DBLE(n)
! At this point the file pointer is at the bottom of the file. It needs to be put back
! at the top. This can be done by rewinding the file or closing and reopening the file.
! Second read loop to accumulate deviations.
total2 = total2 + (rvalue-avg)**2
! Compute standard deviation and print results.
stdev = SQRT(total2/(n-1))
WRITE(*,*) 'avg = ',avg
WRITE(*,*) 'stdev = ',stdev
WRITE(*,*) 'n = ',n
! Close data file and end
END PROGRAM prog
! avg = -0.49299999999999999
! stdev = 5.7822873204678062
! n = 15000