资源描述:
《C++ 计算平均数,方差,标准化变量.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C++计算平均数,方差,标准化变量#include#includedoubledata[4][12]={{2.560,2.605,2.478,2.114,1.711,1.203,0.769,0.305,0.133,0.045,0.118,0.192},{0.410,0.686,0.972,1.141,1.224,1.102,0.936,0.674,0.546,0.450,0.435,0.288},{0.221,0.174,0.227,0.339,0.463,0.
2、598,0.682,0.778,1.028,1.320,1.456,1.544},{1.456,1.159,0.796,0.517,0.297,0.060,-0.193,-0.487,-0.762,-1.037,-1.150,-1.222}};doubleave[4]={0};doublevar[4]={0};doublestdvar[4][12]={0};doublesquare[4]={0};voidsetAverage(){inti,j;doubleamount;for(i=0;i<4;i
3、++){amount=0.0;for(j=0;j<12;j++)amount+=data[i][j];ave[i]=amount/12;}}voidsetVariance(){inti,j;doublesquAmount;for(i=0;i<4;i++){squAmount=0.0;for(j=0;j<12;j++)squAmount+=(data[i][j]-ave[i])*(data[i][j]-ave[i]);var[i]=squAmount/12;square[i]=sqrt(var[i
4、]);}}voidsetstdvar(){for(inti=0;i<4;i++)for(intj=0;j<12;j++)stdvar[i][j]=(data[i][j]-ave[i])/square[i];}voidshow(doublea[4][12]){intyear=1992;printf("-----------------------------------------------------------------------------");printf("年份JanFebMa
5、rAprMayJunJulAugSepOctNovDec");for(inti=0;i<4;i++){printf("%d",year);for(intj=0;j<12;j++)printf("%2.3lf",a[i][j]);year++;printf("");}printf("-----------------------------------------------------------------------------");}intmain(){inti,j;intye
6、ar=1992;setAverage();setVariance();setstdvar();show(data);printf("年份avevarsquare");year=1992;for(i=0;i<4;i++){printf("%d",year);printf("%2.5lf",ave[i]);printf("%2.5lf",var[i]);printf("%2.5lf",square[i]);printf("");year++;}printf("标准化变量:");show(
7、stdvar);return0;}