输入n个数,输出其总和,平均值,方差,输入的数以!结束
1条回答 默认 最新
- 五一编程 2021-12-29 16:05关注
#include <iostream> #include<math.h> #include <memory> #include <iomanip> using namespace std; double sum,avg,fc,bzc,rmax,rmin; void deviation(double f[], int size) { int i; double s=0; rmax=rmin=f[0]; for(i=0;i<size;i++) { sum+=f[i]; if(f[i]>rmax)rmax=f[i]; if(f[i]<rmin)rmin=f[i]; } avg = sum/size;//平均值 for(i=0;i<size;i++) { s+=pow(f[i]-avg,2);// 偏离平均数的距离和 } fc=s/size;//方差 bzc=sqrt(fc); //标准差 } int main() { int i=0; bool isZero = false; double f[100],tmp; while(!isZero) { cout<<"请输入数字 ==> "; cin>>tmp; if(tmp)f[i++]=tmp; else isZero = true; } cout<<endl; double* res = new double[i]; for(int j=0;j<i;j++) { res[j]=f[j]; } deviation(res,i); delete res; cout<<"输入的数字个数:"<<i<<endl; cout<<fixed<<setprecision(5)<<right<<endl; cout<<"总和: "<<sum<<endl; cout<<"平均值: "<<avg<<endl; cout<<"方差: "<<fc<<endl; cout<<"标准差: "<<bzc<<endl; cout<<fixed<<endl; cout<<"最大值: "<<rmax<<endl; cout<<"最小值: "<<rmin<<endl; system("pause"); return 0; }
测试:
请输入数字 ==> 100
请输入数字 ==> 50.64
请输入数字 ==> 300
请输入数字 ==> 210
请输入数字 ==> 45
请输入数字 ==> 120
请输入数字 ==> 20.86
请输入数字 ==> 0输入的数字个数:7
总和: 846.50000
平均值: 120.92857
方差: 8736.93050
标准差: 93.47155最大值: 300.00000
最小值: 20.86000
请按任意键继续. . .本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器