请编写一个函数fun(int score [][3],int num),该函数返回有一门成绩以上课程成绩在85分以上,其余课程成绩不低于70分的人数。数组score按行存放num名考生各自的三门期末考试成绩。 注意:部分源程序已存在文件test31_

admin2010-02-08  20

问题 请编写一个函数fun(int score [][3],int num),该函数返回有一门成绩以上课程成绩在85分以上,其余课程成绩不低于70分的人数。数组score按行存放num名考生各自的三门期末考试成绩。
   注意:部分源程序已存在文件test31_2.cpp中。
   请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。
程序输出结果如下:
3
文件test31_2.cpp清单如下:
   #include <iostream.h>
   int fun(int score[] [3],int num)
   {
   }
   void main ( )
   {
    int score[4] [3]={{70,89,92},{70,76,93},(80,86,98},{65,73,45});
    cout<<fun(score,4)<<end1;
   }

选项

答案int fun(int score[] [3],int num) { int total=0; int flag=0; for (int i=O;i { for(int j=O;j<3;j++) { if (score[i] [j]<70) { flag=-1; j=3; } else if(score[i] [j]>=85) flag=1; } if (flag==1) total=total+1; flag=0; } return total; }

解析 本题主要考查考生对数组和基本控制语句的熟练程度。对于4*3的二维数组score,其下标的范围是从(0,0)到(3,2),这一点是需要特别注意的。另外程序中通过设置,临时变量flag来标志每个人的分数信息,以得到所要统计的人数的方法是需要在编程中灵活掌握的。
转载请注明原文地址:https://jikaoti.com/ti/iFkiFFFM
0

最新回复(0)