1、二级 C语言机试 99及答案解析(总分:100.00,做题时间:90 分钟)一、B填空题/B(总题数:1,分数:30.00)1.请补充 main函数,该函数的功能是:输出一个 NXN矩阵,要求非周边元素赋值 0,周边元素赋值 1。 注意:部分源程序给出如下。 请勿改动主函数 main和其他函数中的任何内容,仅在 main 函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdio .h #define N 10 main() int bbN IN; iht i,j,n; clrscr 0; printf (“ /nInput n: /n“); scanf (“%d“,
2、for (i=0; in; i+ ) for (j=0; jn; j+) if(U【1】/U) bbi j=l; else U【2】/U; printf(“ /n* the result * /n“); for (i=0; in; i+) printf (“ /n/n“); for (j=0; jn; j+) printf (“%4d“,bb i j ); (分数:30.00)填空项 1:_二、B改错题/B(总题数:1,分数:30.00)2.下列给定程序中,函数 fun()的功能是:给定 n个实数,输出平均值,并统计平均值以上(含平均值)的实数个数。例如, n=8 时,输入 193.199,1
3、95.673,195.757,196.051,196.092, 196.596,196.579,196.763,所得平均值为 195.838745,在平均值以上的实数个数应为 5。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include conio.h #include stdio.h int fun(float x, int n) /*found*/ iht j, c=0;float xa=0.0; for (j=0; jn;.j+) xa+=xj/n; printf(“ave=%f/n“,xa); fo
4、r (j=0; jn;j+) if(xj=xa) c+; returnc; main() float xl00= 193.199,195.673,195.757, 196.051,196.092,196.596, 196.579,196.763; clrscr (); printf(“%d/n“,fun (x,8); (分数:30.00)填空项 1:_三、B编程题/B(总题数:1,分数:40.00)3.请编写函数 fun(),该函数的功能是:计算 n门课程的平均分,计算结果作为函数值返回。 例如 x有5门课程的成绩是 90.5,72,80,61.5,55,则函数的值为 71.80。 注意:部分
5、源程序给出如下 请勿改动主函数 main和其他函数中的任何内容,仅在函数 fun的花括号中填入所编写的若干语句。 试题程序:#include stdio.h float fun (float *a, int n) main () float score30=(90.5,72,80,61.5,55, aver; aver=fun(score, 5); printf(“/nAverage score is: %5.2f /n“,aver); (分数:40.00)_二级 C语言机试 99答案解析(总分:100.00,做题时间:90 分钟)一、B填空题/B(总题数:1,分数:30.00)1.请补充 m
6、ain函数,该函数的功能是:输出一个 NXN矩阵,要求非周边元素赋值 0,周边元素赋值 1。 注意:部分源程序给出如下。 请勿改动主函数 main和其他函数中的任何内容,仅在 main 函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdio .h #define N 10 main() int bbN IN; iht i,j,n; clrscr 0; printf (“ /nInput n: /n“); scanf (“%d“, for (i=0; in; i+ ) for (j=0; jn; j+) if(U【1】/U) bbi j=l; else U【2】/U;
7、 printf(“ /n* the result * /n“); for (i=0; in; i+) printf (“ /n/n“); for (j=0; jn; j+) printf (“%4d“,bb i j ); (分数:30.00)填空项 1:_ (正确答案:1i=0i=n-1j=0j=n-1)解析:2bbij=0 解析 填空 1:用二维数组表示 nn矩阵时,周边元素是行下标为 0或 n-1,列下标为。或 n-1的元素,判断时四个条件中只要有一个条件满足,则该元素就是周边元素,所以用“”运算符。填空 2:题目要求将非周边元素赋值为 0。二、B改错题/B(总题数:1,分数:30.00)
8、2.下列给定程序中,函数 fun()的功能是:给定 n个实数,输出平均值,并统计平均值以上(含平均值)的实数个数。例如, n=8 时,输入 193.199,195.673,195.757,196.051,196.092, 196.596,196.579,196.763,所得平均值为 195.838745,在平均值以上的实数个数应为 5。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include conio.h #include stdio.h int fun(float x, int n) /*found*/
9、 iht j, c=0;float xa=0.0; for (j=0; jn;.j+) xa+=xj/n; printf(“ave=%f/n“,xa); for (j=0; jn;j+) if(xj=xa) c+; returnc; main() float xl00= 193.199,195.673,195.757, 196.051,196.092,196.596, 196.579,196.763; clrscr (); printf(“%d/n“,fun (x,8); (分数:30.00)填空项 1:_ (正确答案:错误:缺 正确:加)解析:解析 本题我们可以拆分成两个简单的 c语言试题:
10、 (1)求出 n个数的平均值。 (2)将大于平均值的数字个数计算出来。可利用循环和条件选择语句,对 x数组进行遍历,若某个值大于平均成绩,则将该元素累加计入 c中。我们先来摘清楚一个简单的求 10个整数平均值的例子。 #include “stdio.h“ void main() int i,a10,s=0; float aver=0.0; for (i=0;i三、B编程题/B(总题数:1,分数:40.00)3.请编写函数 fun(),该函数的功能是:计算 n门课程的平均分,计算结果作为函数值返回。 例如 x有5门课程的成绩是 90.5,72,80,61.5,55,则函数的值为 71.80。 注
11、意:部分源程序给出如下 请勿改动主函数 main和其他函数中的任何内容,仅在函数 fun的花括号中填入所编写的若干语句。 试题程序:#include stdio.h float fun (float *a, int n) main () float score30=(90.5,72,80,61.5,55, aver; aver=fun(score, 5); printf(“/nAverage score is: %5.2f /n“,aver); (分数:40.00)_正确答案:()解析:float fun (float *a, int n) float av=0.0; int i; for(i=0;i