【真题1】 给定程序的功能是计算scroe 中m 个人的平均成绩aver,将低于aver 的成绩放在below 中,通过函数名返回人数。 例如,当score={10,20,30,40,50,60,70,80,90},m=9 时,函数返 回的人数应该是4,below={10,20,30,40}。 #include < stdio.h >#include < string.h >int fun(int score[], int m, int below[]) { int i, j = 0 ; float aver = 0.0 ; for(i = 0 ; i < m ; i++) aver += score[i] ; aver /= (float) m ; for(i = 0 ; i < m ; i++) /**************found**************/ if(score[i] < aver) below[j++] = ___1___ ; return j ; } main() { int i, n, below[9] ; int score[9] = {10, 20, 30, 40, 50, 60, 70, 80, 90} ; /**************found**************/ n = fun(score, 9, ___2___) ; printf( "\nBelow the average score are: " ) ; /**************found**************/ for (i = 0 ; i < n ; i++) printf("%d ", ___3___) ; } (2011年2月) 解析: 本题的考核点是求平均值。 解题思路: 1、首先定义变量aver,用aver += score[i]计算总分数,用aver /= (float) m 来计算平均分; 2、通过循环,计算出数组score[]中各个分数的总和,并放至aver中; 3、根据由主函数传过来的m,计算平均分,并放至aver 中; 4、通过循环,让每个分数和平均分比较,低于平均分的分数给below[]; 6、在以上循环的同时,定义了一个变量j,每当发现一个小于平均分的分数,j 便自动加1; 7、最后返回j值,即是数组score[]中低于平均分的元素个数。 答案:【1】score[i] 【2】 below 【3】below[i] 【真题2】 给定程序modi.c 中,函数fun 的功能是:计算输出high 以内最大的 10 个素数之和。high由主函数传给fun 函数。 若high 的值为:100,则函数的值为:732。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main 函数, #include < conio.h >#include < stdio.h >#include < math.h >int fun( int high ) { int sum = 0, n=0, j, yes; /************found************/ while ((high >= 2) && (n < 10) { yes = 1; for (j=2; j< =high/2; j++ ) /************found************/ if (high % j ==0 ){yes=0; break } if (yes) { sum +=high; n++; } high--; } return sum ; } main ( ) { clrscr( ); printf("%d\n", fun (100)); } (2011年2月) 解析: 本题的考核点是C语言中一些规定的应用。 本题中的第一处错误是:在C语言中,"{"和"}"应成对出现,所以程序中的语 句" while ((high >=2)&&(n< 10)"应改为"while ((high >=2)&&(n< 10))"或相同作用的语句。 本题中的第二处错误是:C 语言中,";"是语句的一部分,是语句结束的标志, 不可缺少,所以," if (high%j==0){yes=0;break}"应改为"if (high%j==0){yes=0;break;}"或相同作用的语句。 【真题3】 请编写函数fun,函数的功能是:实现B=A+A',即把矩阵A 加上A 的 转置,存放在矩阵B 中。计算结果在main 函数中输出。 例如,输入下面的矩阵: 其转置矩阵为: |1 2 3| |1 4 7| |4 5 6| |2 5 8| |7 8 9| |3 6 9| 程序输出: |2 6 10| |6 10 14| |10 14 18| 注意:部分源程序存在文件prog.c 中。 请勿改动主函数main 和其他函数中的任何内容,仅在函数fun 的花括号中填入你编 写的若干语句。 #include < conio.h >#include < stdio.h >void fun ( int a[3][3], int b[3][3]) {……} main( ) /* 主程序 */ { int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, t[3][3] ; int i, j ; clrscr() ; fun(a, t) ; for (i = 0 ; i < 3 ; i++) { for (j = 0 ; j < 3 ; j++) printf("%7d", t[i][j]) ; printf("\n") ; } } (2011年2月) 解析: 本题的考核点是矩阵A加上A的转置算法。 提示思路:A的转置即指原先在i行j列的元素现放置在j行i列上,通过一个 两重循环即可求得矩阵与其转置的和。 void fun(int a[3][3],int b[3][3]) {int i,j; for(i=0;i< 3;i++) for (j=0;j< 3;j++) b[i][j]=a[i][j]+a[j][i];/*按从行到列的顺序,求得矩阵与其转置的和*/ } |