! This is a basic starting shell for your programs
MODULE const
INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0)
REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0
END MODULE const
!
PROGRAM prog
USE const
IMPLICIT NONE
!
! 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.
OPEN(10,file='hw15b.in')
! First read loop to get the mean
DO
READ(10,*,END=100) rvalue
total1 = total1 + rvalue
n = n + 1
ENDDO
100 CONTINUE
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.
REWIND(10)
! Second read loop to accumulate deviations.
DO
READ(10,*,END=200) rvalue
total2 = total2 + (rvalue-avg)**2
ENDDO
200 CONTINUE
! 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
CLOSE(10)
END PROGRAM prog
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! avg = -0.49299999999999999
! stdev = 5.7822873204678062
! n = 15000