C++ program to implement standard deviation of grouped data


In this tutorial, we will be discussing a program to implement standard deviation of grouped data.

For this we will be given with class interval and frequency of the class. Our task is to find the standard deviation of the grouped data.

Example

#include <bits/stdc++.h>
using namespace std;
//finding mean of grouped data
float calc_mean(float mid[], int freq[], int n){
   float sum = 0, freqSum = 0;
   for (int i = 0; i < n; i++) {
      sum = sum + mid[i] * freq[i];
      freqSum = freqSum + freq[i];
   }
   return sum / freqSum;
}
//finding standard deviation of data
float calc_deviation(float lower_limit[], float upper_limit[], int freq[], int n){
   float mid[n], sum = 0, freqSum = 0, sd;
   for (int i = 0; i < n; i++) {
      mid[i] = (lower_limit[i] + upper_limit[i]) / 2;
      sum = sum + freq[i] * mid[i] * mid[i];
      freqSum = freqSum + freq[i];
   }
   sd = sqrt((sum - freqSum * calc_mean(mid, freq, n) * calc_mean(mid, freq, n)) / (freqSum - 1));
   return sd;
}
int main(){
   float lower_limit[] = { 50, 61, 71, 86, 96 };
   float upper_limit[] = { 60, 70, 85, 95, 100 };
   int freq[] = { 9, 7, 9, 12, 8 };
   int n = sizeof(lower_limit) / sizeof(lower_limit[0]);
   cout << calc_deviation(lower_limit, upper_limit, freq, n) << endl;
   return 0;
}

Output

15.757
raja
Published on 03-Dec-2019 10:35:06
Advertisements