Table of Contents
In this example program, we compute the standard deviation for input non-grouped data and output the result.
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 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 program.
\Large SD = \sqrt{\frac{(x - \overline{x})^2}{n}}Where \Large \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 \Large x , subtracted from mean and then squared.
- Take the square root of \frac{\Large sum}{\Large n}where \Large n
<mark style="background-color:rgba(0, 0, 0, 0);color:#bb1717" class="has-inline-color"></mark>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.
\Large 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.
\Large Sum = Sum + (x[i] - mean \hspace{1mm}value)^2Where \Large 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.
\Large Standard \hspace{1mm} deviation = sqrt( Sum / n)where \Large n is the total number of elements in data set and Sum is the value found in the previous step.
Program Codes – Standard Deviation for Non-grouped Data
#include <stdio.h>
#include <math.h>
/* Function Declaration */
float standard_deviation(float data[], int n);
int main()
{
int n, i;
float data[100];
printf("Enter number of data elements (1 to 100): ");
scanf("%d", &n);
if (n <= 0 || n > 100)
{
printf("Invalid number of elements\n");
return 0;
}
printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%f", &data[i]);
printf("Standard Deviation = %.3f\n",
standard_deviation(data, n));
return 0;
}
float standard_deviation(float data[], int n)
{
float mean = 0.0, sum = 0.0;
int i;
for (i = 0; i < n; i++)
mean += data[i];
mean /= n;
for (i = 0; i < n; i++)
sum += (data[i] - mean) * (data[i] - mean);
return sqrt(sum / n); /* Ungrouped data SD */
}
#include <iostream>
#include <cmath>
using namespace std;
float standardDeviation(float data[], int n)
{
float mean = 0, sum = 0;
for (int i = 0; i < n; i++)
mean += data[i];
mean /= n;
for (int i = 0; i < n; i++)
sum += (data[i] - mean) * (data[i] - mean);
return sqrt(sum / n);
}
int main()
{
int n;
float data[100];
cout << "Enter number of elements (1 to 100): ";
cin >> n;
if (n <= 0 || n > 100)
{
cout << "Invalid input\n";
return 0;
}
cout << "Enter elements:\n";
for (int i = 0; i < n; i++)
cin >> data[i];
cout << "Standard Deviation = "
<< standardDeviation(data, n) << endl;
return 0;
}import java.util.Scanner;
class StandardDeviation
{
static double standardDeviation(double data[], int n)
{
double mean = 0, sum = 0;
for (int i = 0; i < n; i++)
mean += data[i];
mean /= n;
for (int i = 0; i < n; i++)
sum += (data[i] - mean) * (data[i] - mean);
return Math.sqrt(sum / n); // Population SD
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
double[] data = new double[100];
System.out.print("Enter number of elements (1 to 100): ");
n = sc.nextInt();
if (n <= 0 || n > 100)
{
System.out.println("Invalid input");
return;
}
System.out.println("Enter elements:");
for (int i = 0; i < n; i++)
data[i] = sc.nextDouble();
System.out.println("Standard Deviation = "
+ standardDeviation(data, n));
sc.close();
}
}import math
n = int(input("Enter number of elements (1 to 100): "))
if n <= 0 or n > 100:
print("Invalid input")
exit()
data = []
print("Enter elements:")
for _ in range(n):
data.append(float(input()))
mean = sum(data) / n
variance = sum((x - mean) ** 2 for x in data) / n
std_dev = math.sqrt(variance)
print("Standard Deviation =", round(std_dev, 3))Output – Compute Standard Deviation for non-grouped data
When we execute the program whose code is given above. It asks for the number of data elements which should be less than \Large 100. For the purpose of example, you can enter any number (say \Large 7).
The program then asks for data elements, we entered following – \Large 56, 45, 89, 78, 12, 23, 35. The standard deviation value is \Large 26.031. To prove the correctness of the output, we leave it as an exercise for you. 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