在本教程中,我们将讨论实现分组数据标准差的程序。
为此,我们将给出班级间隔和班级频率。我们的任务是找到分组数据的标准偏差。
#include <bits/stdc++.h> using namespace std; //查找分组数据的均值 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; } //查找数据的标准偏差 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; }
输出结果
15.757