C ++类间隔算术平均值程序
我们给了三个数组,其中第一个数组包含算术平均值的上限,第二个数组包含算术平均值的下限,第三个数组包含频率,任务是生成给定类间隔的算术平均值。
什么是算术平均值?
算术平均值是通过将集合中所有元素的总和除以给定集合中元素的总数而得出的平均值。
如何计算类间隔算术平均值
给定下限,上限,频率
由于有两个值,因此通过将上限和下限相加并将最终结果除以2来计算中点。之后,将类间隔的中点乘以其相应的频率。
通过将m*f之和除以频率之和得出最终结果来计算算术平均值。
class区间算术平均值=m*f的总和/f的总和=(1.5+7.0+16.5+30.0)/(1+2+3+4)=5.5
示例
Input-: LowerLimit[] = {1, 6, 11, 16, 21} UpperLimit[] = {5, 10, 15, 20, 25} freq[] = {10, 20, 30, 40, 50} Output: 16.3333 Input-: UowerLimit[] = { 2, 4, 6, 8, 10 } LpperLimit[] = { 1, 3, 5, 7, 9 } freq[] = { 1, 2, 3, 4, 5 } Output: 5.5
算法
START Step 1-> declare function to calculate class interval arithmetic mean float AM(int LowerLimit[], int UpperLimit[], int freq[], int terms) Declare float mid[terms] declare and set float sum = 0 and Sum_freq = 0 Loop For int i = 0 and i < terms and i++ Set mid[i] = (LowerLimit[i] + UpperLimit[i]) / 2 Set sum = sum + mid[i] * freq[i] Set Sum_freq = Sum_freq + freq[i] End return sum / Sum_freq Step 2-> In main() Declare int LowerLimit[] = { 2, 4, 6, 8, 10 } Declare int UpperLimit[] = { 1, 3, 5, 7, 9 } Declare int freq[] = { 1, 2, 3, 4, 5 } Declare int size = sizeof(freq) / sizeof(freq[0]) Call AM(LowerLimit, UpperLimit, freq, size) STOP
示例
#include <bits/stdc++.h> using namespace std; //计算类间隔的算术平均值。 float AM(int LowerLimit[], int UpperLimit[], int freq[], int terms) { float mid[terms]; float sum = 0, Sum_freq = 0; for (int i = 0; i < terms; i++) { mid[i] = (LowerLimit[i] + UpperLimit[i]) / 2; sum = sum + mid[i] * freq[i]; Sum_freq = Sum_freq + freq[i]; } return sum / Sum_freq; } int main() { int UowerLimit[] = { 2, 4, 6, 8, 10 }; int LpperLimit[] = { 1, 3, 5, 7, 9 }; int freq[] = { 1, 2, 3, 4, 5 }; int size = sizeof(freq) / sizeof(freq[0]); cout<<"Arithmetic mean is : "<<AM(LowerLimit, UpperLimit, freq, size); return 0; }
输出结果
Arithmetic mean is : 5.5