        PROGRAM AVGVAR
C======================================================================*
C AVGVAR -  computes the average and variance of a set of measurements
C
C  This program will read a set of numbers from the terminal
C  and compute the average and variance of this set of numbers.
C  Numbers are read until you enter zero.
C
C  Eric Myers   <myers@scarlett.vassar.edu>  14 June 1994 :  Exercise 04
C  Department of Physics and Astronomy, Vassar College, Poughkeepsie, NY
C  (Typed into the file avgvar.f by Matthew Vassar on 19 June 2003)
C======================================================================*
C Declare all variables:

        INTEGER N

        REAL  SUM, SUMSQ, X
        REAL  AVG, VAR
C==================================================*
C Begin:

        PRINT *, 'This program will compute the average and variance'
        PRINT *, 'of a set of numbers.  Please enter the numbers one'
        PRINT *, 'at a time.  Enter ZERO to stop input and print the'
        PRINT *, 'final results.'
        PRINT *, ' '
        PRINT *, 'Enter your numbers:'

C Initialize counter and sums

        N = 0
        SUM = 0.0
        SUMSQ = 0.0

C Read in the data until user enters zero

 10     CONTINUE
          READ *, X
          PRINT *, X
          IF(X.EQ.0.0) GOTO 20

C Add to count, sum and sum of squares, and then go get next number

          N = N + 1
          SUM = SUM + X
          SUMSQ = SUMSQ + X**2
        GOTO 10
        
C Compute final results and display them

 20     CONTINUE
        PRINT *,' '
        PRINT *,'You entered ',N,' numbers.'

        IF(N.GT.0) THEN
          AVG = SUM / N
          PRINT *,'The average is: ',AVG 
        ENDIF

        IF(N.GT.1) THEN
          VAR = (SUMSQ - N*AVG**2) / (N-1)
          PRINT *,'The variance is: ',VAR
          PRINT *,'(The standard deviation is ',SQRT(VAR),')'
        ENDIF

        STOP
        END
