# Computing Mean, Variance and Standard Deviation with Array

### Problem Statement

The mean, variance and standard deviation of a set of data can be computed with the following formulas:

Write a program to read in a set of real values and use the above formulas to compute the mean, variance and standard deviation. Moreover, this program should generate a table containing the following addition information:

• the input data,
• the distance between the data to the mean, and
• if a data is greater than the sum of mean and standard deviation, a mark Good should be displayed; if a data is less than mean - standard deviation, a mark Bad should be displayed.

### Solution

```! --------------------------------------------------------------------
! PROGRAM  MeanVariance:
!    This program reads in an array and computes the mean, variance
! and standard deviation of the data stored in the array.  Then, it
! displays an analysis table.  If a value is greater than the value
! of (mean + standard deviation), it displays a "good".  If a value
! is less than the value of (mean - standard deviation), it displays
! --------------------------------------------------------------------

PROGRAM  MeanVariance
IMPLICIT  NONE

INTEGER, PARAMETER :: MAX_SIZE = 50       ! maximum array size
REAL, DIMENSION(1:MAX_SIZE) :: Data       ! input array
REAL               :: Mean, Variance, StdDev   ! results
INTEGER            :: n                   ! actual array size
INTEGER            :: i                   ! running index

READ(*,*)  (Data(i), i = 1, n)
WRITE(*,*)  "Input Data:"            ! display the input
WRITE(*,*)  (Data(i), i = 1, n)

Mean = 0.0                           ! compute mean
DO i = 1, n
Mean = Mean + Data(i)
END DO
Mean = Mean / n

Variance = 0.0                       ! compute variance
DO i = 1, n
Variance = Variance + (Data(i) - Mean)**2
END DO
Variance = Variance / (n - 1)
StdDev   = SQRT(Variance)            ! compute standard deviation

WRITE(*,*)                           ! display result
WRITE(*,*)  "Mean               : ", Mean
WRITE(*,*)  "Variance           : ", Variance
WRITE(*,*)  "Standard Deviation : ", StdDev
WRITE(*,*)
WRITE(*,*)  "Analysis Table:"        ! display an analysis table
DO i = 1, n
IF (Data(i) > Mean + StdDev) THEN
WRITE(*,*)  Data(i), Data(i) - Mean, "<-- Good"
ELSE IF (Data(i) < Mean - StdDev) THEN
WRITE(*,*)  Data(i), Data(i) - Mean, "<-- Bad"
ELSE
WRITE(*,*)  Data(i), Data(i) - Mean
END IF
END DO

END PROGRAM  MeanVariance
```

### Program Input and Output

If the input data consist of the following:
```10
6.6  6.0  4.0  9.0
4.5  7.3  9.5  8.0
7.0  5.2
```
The out of the program is:
```Input Data:
6.5999999,  6.,  4.,  9.,  4.5,  7.30000019,  9.5,  8.,  7.,  5.19999981

Mean               : 6.71000004
Variance           : 3.3498888
Standard Deviation : 1.83027017

Analysis Table:
6.5999999,  -0.110000134
6.,  -0.710000038
9.,  2.28999996<-- Good
7.30000019,  0.590000153
9.5,  2.78999996<-- Good
8.,  1.28999996
7.,  0.289999962
5.19999981,  -1.51000023
```

### Discussion

• The maximum size of array Data() is MAX_SIZE and the actual size of the input is read and stored in n. Therefore, the value of n cannot be greater than MAX_SIZE.
• The first READ(*,*) reads in 10 for n. The second READ(*,*) uses an implied DO to read 10 values into Data(1), Data(2), ..., Data(10).
• The first DO-loop computes the sum of all input data items. Then, the mean is computed and stored back to Mean.
• Knowing the value of mean, the second DO-loop computes the variance.
• The third DO-loop generates the analysis table. For each Data(i), if it is larger than Mean + StdDev, the output line would contain <-- Good. If Data(i) is less than Mean - StdDev, the output line would contain <-- Bad.