C Program To Compute Standard Deviation For Non-Grouped Data

The standard deviation is a measure to understand how much data deviates from the mean value. The data may be grouped or non-grouped where non-group data is a list of data.

In this example program, we compute the standard deviation for input non-grouped data and output the result.

Learn the basics of C programming before you begin with this example. Continue reading if you know the basics.

In the next section, we will discuss the problem in detail.

Problem Definition

The standard deviation is a measure useful in comparing two sets of data when their mean value is the same. To calculate the standard deviation we use the following formula in this example C program.

SD =  \sqrt{\frac{(x - \overline{x})^2}{n}}

Where \overline{x} is the mean value.

Given the formula, you can find standard deviation in the following steps.

  • Find the mean value.
  • Take the sum of each data element x subtracted from mean and squared.
  • Take the square root of sum/n where n is the number of data elements.

The first step is to find the mean value of the given non-grouped data. The program takes the sum of all data elements and divides it with the total number of elements.

Mean\hspace{1mm} value = \frac{Sum \hspace{1mm}of\hspace{1mm} all \hspace{1mm}elements}{Number\hspace{1mm} of \hspace{1mm}elements}

The second step in process of finding standard deviation is to take the sum of each data element( say x ) and subtract it from the mean and then square it.

Sum = Sum + (x[i] - mean \hspace{1mm}value)^2

Where x[i] represents a data element from the list of non-grouped data set.The last step is to compute the standard deviation using the following.

Standard \hspace{1mm} deviation =  sqrt( Sum / n)

where n is the total number of elements in data set and Sum is the value found in the previous step.

Program Source Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Function Declaration for Standard Deviation */
float standard_deviation(float data[], int n);
int main()
{
    int n,i;
    float data[100];
    printf("Enter Number of Data elements( Less than 100):");
    scanf("%d",&n);
    printf("Enter Elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%f",&data[i]);
    }
    printf("\n");
    printf("Standard Deviation = %.3f",standard_deviation(data,n));
    getch();
    system("PAUSE"); 
    return 0;
}

/* Function Definition for Standard Deviation */
float standard_deviation(float data[], int n)
{
    float mean=0.0, sum_deviation=0.0;
    int i;
    for(i=0;i<n;++i)
    {
        mean = mean + data[i];
    }
    mean = mean/n;
    for(i=0;i<n;++i)
    {
         sum_deviation = sum_deviation + ((data[i] - mean) * (data[i] - mean));
    }
    return sqrt(sum_deviation/n);
}

Output – Compute Standard Deviation

When we execute the program whose code is given above. It asks for the number of data elements which should be less than 100. For the purpose of example, you can enter any number (say 7).

The program then asks for data elements, we entered following – 56, 45, 89, 78, 12, 23, 35. The standard deviation value is 26.031. To prove the correctness of the output, we leave it as an exercise for you.A screenshot of the output is shown below.

Enter Number of Data elements( Less than 100):7
Enter Elements:
56
45
78
89
23
12
35

Standard Deviation - 26.031
error

Enjoy this blog? Please spread the word :)