已知求成绩的平均值和均方差公式:,,其中,n为学生人数,si为第i个学生成绩。求某班学生的平均成绩和均方差。
#include<iostream> #include<cmath> using namespace std; void aveMsd( double [], int, double &, double & ); //求平均值和均方差值函数 int main() { double s[] = { 76, 85, 54, 77, 93, 83, 90, 67, 81, 65 }; double ave, msd; int i,n; n = sizeof( s )/sizeof( double ); //求数组元素的个数 cout<<"学生成绩:"; for( i=0; i<n; i++ ) cout<<s[i]<<" "; cout<<endl; aveMsd( s, n, ave, msd ); cout << "平均值:" << ave << endl << "均方差值:" << msd << endl; } void aveMsd( double s[], int n, double &ave, double &msd ) { int i; double sum1=0, sum2=0; for( i=0; i<n; i++ ) //求平均值 sum1 += s[i]; ave = sum1/n; for( i=0; i<n; i++ ) //求均方差 sum2 += pow( s[i]-ave, 2 ); msd = sqrt( sum2/n ); }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题