next up previous contents
Next: Erroneous Code Up: Standard Deviation Previous: Standard Deviation

Solution

    PROGRAM Main
     REAL, ALLOCATABLE, DIMENSION(:) :: X
     INTEGER :: n_elts, istat

      PRINT*, "How big is the array? "
      READ*, n_elts

      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*, "Std Dev = ", Std_Dev(X,n_elts)
       DEALLOCATE(X)
      END IF

     CONTAINS

      REAL FUNCTION Std_Dev(X,n_elts)
       INTEGER            :: n_elts
       REAL, DIMENSION(:) :: x
       REAL               :: mean
       mean = SUM(X)/REAL(n_elts)
       Std_Dev = SQRT(SUM((X-mean)**2)/REAL(n_elts))
      END FUNCTION Std_Dev

    END PROGRAM MAIN

The results for the specified sequences are:

5.0 3.0 17.0 -7.56 78.1 99.99 0.8 11.7 33.8 29.6
 Std Dev =    33.56556

and

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0
 Std Dev =    4.031129


next up previous contents
Next: Erroneous Code Up: Standard Deviation Previous: Standard Deviation

©University of Liverpool, 1997
Thu May 29 10:11:26 BST 1997
Not for commercial use.