C Program to Calculate Standard Deviation

How to write a C Program to Calculate Standard Deviation, Mean, and variance with an example?. Before this example, we have to understand a few concepts, such as Mean and Variance, before understanding the Standard Deviation. To understand these concepts better, we are taking some example data here.

For example, we have 5 items, and their Price values are 10, 25, 30, 67, 92. Let us calculate the Mean, Variance, and Standard Deviation in C programming.

Mean in C

Mean can also be called as Average and we can calculate using the formula:

Mean = Sum of each individual/total number of items
Mean = (10 + 25 + 30 + 67+ 92) / 5
Mean = 224 / 5 = 44.8

Variance in C

Before Calculating the Variance in C, we have to find the difference between the original value and the Mean because

Difference = ((OriginalValue – Mean)² + (OriginalValue – Mean)² +…. )/Total number of items
Difference = (10 – 48) + (25 – 48) + (30 – 48) + (67 – 48) + (92 – 48)
Difference = (- 38) + (- 23) + (- 18) + (19) + (44)

We can calculate the Variance using the formula:
Variance = ( (- 38)² + (- 23)² + (- 18)² + (19)² + (44)² ) / 5
Variance = (1444 + 529 + 324 +361 + 1936) / 5
Variance = 4594 / 5
Variance = 918.8

Standard Deviation in C

The Square root of Variance is called as Standard Deviation.
Standard Deviation = √918.8
Standard Deviation = 30.31

C Program to Calculate Standard Deviation, Mean and Variance

This C program calculates the Mean, Variance, and Standard Deviation of the array of given numbers.

/* C Program to Calculate Standard Deviation, Mean and Variance */
#include <stdio.h>
#include <math.h>

void main()
{
  float Price[50];
  int  i, Number;
  float Mean, Variance, SD, Sum=0, Differ, Varsum=0;

  printf("\nPlease Enter the N Value\n");
  scanf("%d", &Number);

  printf("\nPlease Enter %d real numbers\n",Number);
  for(i=0; i<Number; i++)
   {
     scanf("%f", &Price[i]);
   }

  for(i=0; i<Number; i++)
   {
     Sum = Sum + Price[i];
   }
  
  Mean = Sum /(float)Number;

  for(i=0; i<Number; i++)
   {
     Differ = Price[i] - Mean;
     Varsum = Varsum + pow(Differ,2);
   }
  
  Variance = Varsum / (float)Number;
  SD = sqrt(Variance);
  
  printf("Mean               = %.2f\n", Mean);
  printf("Varience           = %.2f\n", Variance);
  printf("Standard deviation = %.2f\n", SD);
}
C Program to Calculate Standard Deviation

Within this C Program to Calculate Standard Deviation, First Printf statement ask the user to enter the N value (Number of items inside the Price Array)

printf("\n Please Enter the N Value \n");

scanf(“%d”, &Number) statement will assign the user input value to variable Number

First For loop

for(i=0; i<Number; i++)
{
  scanf("%f", &Price[i]);
}

(i<Number) – the condition helps the compiler not to exceed more than N value (Since array starts with 0 and end with N-1, and we mentioned i < Number)

scanf("%f", &Price[i]) )

The scanf statement in this C program will assign the user input values to the Price array. So the first value will be assigned to Price[0], second at Price[1], etc.

Second For loop

for(i=0; i<Number; i++)
{
   Sum = Sum + Price[i];
}

Sum = Sum + Price[i]; statement will calculate the sum of each individual element in the Price Array

Initially, we assigned Sum = 0. So, the first iteration will be
Sum = 0 + Price[0] = 0+ 10 = 10
Sum = 10 + Price[1] = 10 + 20 = 30
Sum = 30 + Price[2] = 30 + 30 = 60
Sum = 60 + Price[3] = 60 + 40 = 100
Sum = 100 + Price[4] = 100 + 50 = 150

Next line, we are calculating the Mean or average in C
Mean = Sum /(float)Number;
Mean = 150 / 5 = 30

Since the Number is an integer value and Mean is a float value, we have to typecast the Number value to float (i.e. (float)Number). If you forgot to Typecast, then it will give the wrong values.

Third For loop

for(i=0; i < Number; i++)
{
   Differ = Price[i] - Mean;

   Varsum = Varsum + pow(Differ, 2);
}

Within the for loop, pow() is the math function that will calculate the power of Differ by 2 times (Equal to Differ2).

Instead of writing in two lines we can write the above code in 1 line also:

Varsum = Varsum + pow( (Price[i] – Mean), 2);

Varsum At First Iteration
Varsum = 0 + pow( (Price[0] – 30), 2)
= 0 + pow( (10 – 30), 2)
= 0 + (20 * 20) = 400

Varsum At Second Iteration
Varsum = 400 + pow( (Price[1] – 30), 2)
= 400 + pow( (20 – 30), 2)
= 400 + (10 * 10) = 500

Varsum At Third Iteration
Varsum = 500 + pow( (Price[2] – 30), 2)
= 500 + pow( (30 – 30), 2)
= 500 + (0 * 0) = 500

Varsum At Fourth Iteration
Varsum = 500 + pow( (Price[3] – 30), 2)
= 500 + pow( (40 – 30), 2)
= 500 + (10 * 10) = 600

Varsum At Fifth Iteration
Varsum = 600 + pow( (Price[4] – 30), 2)
= 600 + pow( (50 – 30), 2)
= 600 + (20 * 20) = 1000

Next line, we are calculating the Variance in C Programming

Variance = Varsum / (float)Number;

Variance = 1000 / 5 = 200

Now, its time to calculate the Standard Deviation in c programming

SD = sqrt(Variance);

SD = sqrt (200) = 14.142

sqrt() is a math function which calculates the Square Root of any given number

C Program to Calculate Standard Deviation using Functions

This C program calculates the Mean, Variance, and Standard Deviation of the given numbers in an array using Functions.

We have already seen in the above example. There are a lot of for loops and many calculations. It is often confusing to put it all together, so in this program, we separated the calculation using the Function concept.

#include<stdio.h>
#include<math.h>
float StandardDeviation (float Price[],int Number);

int main()
{
  float Price[50], SD;
  int  i, Number;
 
  printf("\nPlease Enter the N Value\n");
  scanf("%d", &Number);

  printf("\nPlease Enter %d real numbers\n",Number);
  for(i=0; i<Number; i++)
   {
     scanf("%f", &Price[i]);
   }

  //Calling the Function StandardDeviation
  SD = StandardDeviation (Price, Number);

  printf("Standard deviation = %.2f\n", SD);
    return 0;
}

float StandardDeviation (float Price[],int Number)
{
  float Mean, Variance, SD, Sum=0, Varsum=0;
  int i;
 
  for(i=0; i<Number; i++)
   {
     Sum = Sum + Price[i];
   }
  Mean = Sum /(float)Number;
 
  for(i=0; i<Number; i++)
   {
     Varsum = Varsum + pow((Price[i] - Mean),2);
   }
 
  Variance = Varsum / (float)Number;
  SD = sqrt(Variance);
  
  printf("Mean               = %.2f\n", Mean);
  printf("Varience           = %.2f\n", Variance);
 
  return SD;
}
Please Enter the N Value
6

Please Enter 6 real numbers
5
15
25
35
45
55
Mean               = 30.00
Varience           = 291.67
Standard deviation = 17.08

Within this C Program to Calculate Standard Deviation, When the compiler reaches the statement

SD = StandardDeviation (Price, Number);

Then it will jump to function

float StandardDeviation (float Price[ ], int Number)

For better understanding, we used the same arguments in the function declaration and Function Calling. In real-time, names may change, but the data types should be the same.

float StandardDeviation (float Price[ ],int Number)

Within the above function, we calculated the Mean, Variance, and Standard Deviation of the array of N numbers. Logic inside the function is the same as we explained in the first example. So to understand Please refer First Example Analysis section.

It’s better to revise the Function article in our Blog Functions to understand the Functions.