码迷,mamicode.com
首页 > 其他好文 > 详细

课后习题参考8.14求方差

时间:2015-04-22 11:44:17      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:8.14

#include<stdio.h>
#define STU_NUM     10
#define COURSE_NUM  5
double CalAverage(double *pScore,int iLen)
{
int i;
double dSum = 0;
for (i = 0;i < iLen;i++)
{
dSum += pScore[i];
}
return (dSum/iLen);
}
double CalCourseAverage(double (*pStudent)[COURSE_NUM],int iStuNum)
{
int i;
double dSum = 0;
for (i = 0;i < iStuNum;i++)
{
dSum += **pStudent;
}
return (dSum/iStuNum);
}
void FindMax(double pScore[][COURSE_NUM],int * pRow,int * pColum)
{
int i,j;
double dMax = pScore[0][0];
for( i=0; i< STU_NUM; i++)               /*  行上的搜索  */
{
for( j=0; j< COURSE_NUM; j++)        /*   列上的搜索  */
{
if(pScore[i][j] > dMax) 
{
dMax= pScore[i][j];
*pRow=i;    
*pColum=j;  
}
}
}
}
double CalSquare1(double *pStuAv,int iStuNum)
{
int i;
double dSum = 0;
for (i = 0;i < iStuNum;i++)
{
dSum += (pStuAv[i] * pStuAv[i]);
}
return (dSum/iStuNum);
}
double CalSquare2(double *pStuAv,int iStuNum)
{
int i;
double dSum = 0;
for (i = 0;i < iStuNum;i++)
{
dSum += ((pStuAv[i] * pStuAv[i])/(iStuNum * iStuNum));
}
return dSum;
}
double CalAverageVariance(double *pStuAv,int iStuNum)
{
return (CalSquare1(pStuAv,iStuNum) - CalSquare2(pStuAv,iStuNum));
}
void main()
{
int i,j,iRow,iColum;
    double dStuAv[STU_NUM] = {0},dScore[STU_NUM][COURSE_NUM];
printf("请输入 10 个学生 5 门课成绩:\n");
for (i = 0;i < STU_NUM;i++)
{
for (j = 0;j < COURSE_NUM;j++)
{
scanf("%lf",&dScore[i][j]);
}
}
    for (i = 0;i < STU_NUM;i++)
    {
dStuAv[i] = CalAverage(dScore[i],COURSE_NUM);
printf("\n第 %d 个学生的平均分:%f.\n",i+1,dStuAv[i]);
    }
for (i = 0;i < COURSE_NUM;i++)
    {
printf("\n第 %d 门课程的平均分:%f.\n",i+1,CalCourseAverage(dScore,STU_NUM));
    }
FindMax(dScore,&iRow,&iColum);
printf("\n第 %d 个学生第 %d 门课程最高:%f.\n",iRow+1,iColum+1,dScore[iRow][iColum]);
printf("\n平均分方差为:%f.\n",CalAverageVariance(dStuAv,STU_NUM));
}

课后习题参考8.14求方差

标签:8.14

原文地址:http://blog.csdn.net/mark_tanlixing/article/details/45191243

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!