0$房天下问答|麻烦大家看个程序,里面有个警告看不懂,搞不转。。。。$https://m.fang.com/ask/ask_1800022.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=1800022
-
麻烦大家看个程序,里面有个警告看不懂,搞不转。。。。
/*输入10个学生5门课的成绩,分别用函数实现下列功能:1.计算每个学生平均分;2.计算每门课平均分;3.找出所有50个分数中最高的分数所对应的学生和课程;4.计算平均分方差;*/#include<stdio.h>#include<math.h>float score[10][5];int i,j;float largest=0;void main(){ float aver_st(int i); float aver_score(int j); float highest(int student,float subject); float fc=0; int student=1,subject=1; printf("请输入10个学生的成绩,按照每门课顺序依次输入:\n"); for(i=0;i<10;i++) { printf("第%d个学生",i+1); for(j=0;j<5;j++) {printf("第%d门课的成绩:\n",j+1); scanf("%f",&score[i][j]); } } for(i=0;i<10;++i) printf("第%d个学生的平均分为:%.2f\n",i+1,aver_st(i)); for(j=0;j<5;++j) printf("第j门课的平均分为:%.2f\n",j+1,aver_score(j)); printf("50个分数中最高的分数是第%d个学生的第%d门课,分数为:%.2f\n",student,subject,largest); printf("平均分方差为:%.2f\n",fc);} float aver_st(int i)/*求每个学生的平均分*/{float score[10][5]; return ((score[i][0]+score[i][1]+score[i][2]+score[i][3]+score[i][4])/5);}float aver_score(int i,int j)/*求每门课的平均分*/{float y=0;float score[10][5];for(i=0;i<10;i++)y=y+score[i][j]; return (score[i][j]=y/5);} float highest(int student,float subject) /*找出50个分数中的最高分及对应的学生和课程*/ { for(i=0;i<10;++i) { for(j=0;j<5;++j) { if(largest<score[i][j]) { largest=score[i][j]; } subject=j+1; } student=i+1; } return(largest); }
更多
共1个回答
-
-
-
真彩名片
丨Lv 0
你定义参数和变量的方式有点不合理,全局变量和局部变量常常都定义为一个变量名了~~~~还有就是在使用的全局变量的函数中,返回的居然是全局变量?(如:return(largest); )这个让我有点汗颜。。。。你的代码我改了挺多地方(PS:其实我感觉另外重新写过比较好。。。但是我有点懒,直接在上面修改了),还有就是由于一次要输入50个数,这个调试起来比较麻烦,所以我改成只输入两个学生的数据了,你可以改回来;还有就是帮你完善了最高分的算法,平均分方差就没搞了。。。因为我不清楚你到底需要怎么算。另外,我建议你,谨慎使用全局变量。代码如下:#include<stdio.h> #include<math.h> float score[10][5]; int i,j; void main() { float aver_st(int i); float aver_score(int j); float highest(int &student,int &subject); float fc=0; float largest=0; int student=1,subject=1; printf("请输入2个学生的成绩,按照每门课顺序依次输入:\n"); for(i=0;i<2;i++) { printf("第%d个学生",i+1); for(j=0;j<5;j++) {printf("第%d门课的成绩:\n",j+1); scanf("%f",&score[i][j]); } } for(i=0;i<2;++i) printf("第%d个学生的平均分为:%.2f\n",i+1,aver_st(i)); for(j=0;j<5;++j) printf("第%d门课的平均分为:%.2f\n",j+1,aver_score(j)); largest=highest(student,subject);printf("10个分数中最高的分数是第%d个学生的第%d门课,分数为:%.2f\n",student+1,subject+1,largest); printf("平均分方差为:%.2f\n",fc); } float aver_st(int i)/*求每个学生的平均分*/ {//float score[10][5]; return ((score[i][0]+score[i][1]+score[i][2]+score[i][3]+score[i][4])/5); } float aver_score(int j)/*求每门课的平均分*/ {float y=0; //float score[10][5]; for(i=0;i<2;i++) y=y+score[i][j]; return (score[i][j]=y/2); } float highest(int &student,int &subject) /*找出50个分数中的最高分及对应的学生和课程*/ { float largest=0; for(i=0;i<2;++i) { for(j=0;j<5;++j) { if(largest<score[i][j]) { largest=score[i][j]; student=i;subject=j;} } } return(largest); }
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功