1、二级 C 语言-358 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是:把一个整数插入一个已经按从小到大排序的数组中。插入后,数组仍然有序。 例如,在数组 bbM=10 20 30 40 50 60 70 80 85 95中插入 75,结果为 bbM=10 20 30 40 50 60 70 75 80 85 95 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdli
2、b.h #includestdio.h #define M 10 void main() int i,j; int n; int bbM+1=10,20,30,40,50,60,70,80,85,95; system(“CLS“); printf(“/nlnput n/n“); scanf(“%d“, printf(“/nn=%d“,n); printf(“/n*original list*/n“); for(i=0;iM;i+) printf(“%4d“,bbi); for(i=0;iM;i+) if(n=bbi) for(j=M; 1;j-) 2; bbj=n; 3; if(i=M) bb
3、i=n; printf(“/n*new list*/n“); for(i=0;iM+1;i+) printf(“%4d“,bbi); (分数:30.00)二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:用冒泡法对 6 个字符串按由小到大的顺序进行排序。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includestring.h #includeconio.h #includestdio.h #define MAX 20 /*fo
4、und* proc(char*pstr6) int i,j; char*p; for(i=0;i5;i+) for(j=i+1;j6;j+) /*found* if(strcmp(*(pstr+i),pstr+j)0) p=*(pstr+i); /*found* *(pstr+i)=pstr+j; *(pstr+j)=p; void main() int i; char * pstr6,str6MAX; system(“CLS“); for(i=0;i6;i+)pstri=stri; printf(“/nEnter 6 string(1 string at eachline):/n“); fo
5、r(i=0;i6;i+)scanf(“%s“,pstri); proc(pstr); printf(“The strings after sorting:/n“); for(i=0;i6;i+)printf(“%s/n“,pstri); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.下列程序定义了 MM 的二维数组,并在主函数中自动赋值。请编写函数 proc(int aM),该函数的功能是:使数组左下半三角元素中的值全部置成 0。例如,a 数组中的值为: a=1 7 4 0 9 4 8 8 2 4 5 5 1 7 1 1 5 2 7 6 1 4 2 3 2 则返回主程序
6、后 a 数组中的值应为: 0 7 4 0 9 0 0 8 2 4 0 0 0 7 1 0 0 0 0 6 0 0 0 0 0 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h #includetime.h #includestdlib.h #define M 5 void proc(int aM) void main() int arrMM,i,j; srand(unsigned)time(NULL); system(“CLS“); p
7、rintf(“*The array*/n“); for(i=0;iM;i+) /产生一个随机的 5*5 矩阵 for(j=0;jM;j+) arrij=rand()%10; printf(“%4d“,arrij); printf(“/n“); proc(arr); printf(“THE RESULT/n“); for(i=0;iM;i+) for(j=0;jM;j+) printf(“%4d“,arrij); printf(“/n“); (分数:40.00)_二级 C 语言-358 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请
8、补充 main()函数,该函数的功能是:把一个整数插入一个已经按从小到大排序的数组中。插入后,数组仍然有序。 例如,在数组 bbM=10 20 30 40 50 60 70 80 85 95中插入 75,结果为 bbM=10 20 30 40 50 60 70 75 80 85 95 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #define M 10 void main() int i,j; int n; int bbM
9、+1=10,20,30,40,50,60,70,80,85,95; system(“CLS“); printf(“/nlnput n/n“); scanf(“%d“, printf(“/nn=%d“,n); printf(“/n*original list*/n“); for(i=0;iM;i+) printf(“%4d“,bbi); for(i=0;iM;i+) if(n=bbi) for(j=M; 1;j-) 2; bbj=n; 3; if(i=M) bbi=n; printf(“/n*new list*/n“); for(i=0;iM+1;i+) printf(“%4d“,bbi); (
10、分数:30.00)解析:ji bbj=bbj-1 break解析 要把一个整数插入一个已经按从小到大排序的数组中,插入后,数组仍然有序。首先要找到应该插入的位置,将该位置的元素以及其后的元素向后移动一个位。因此,第一处填“ji”;第二处填“bbj=bbj-1”。然后将指定元素插入,结束循环,因此,第三处填“break”。二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:用冒泡法对 6 个字符串按由小到大的顺序进行排序。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #
11、includestdlib.h #includestring.h #includeconio.h #includestdio.h #define MAX 20 /*found* proc(char*pstr6) int i,j; char*p; for(i=0;i5;i+) for(j=i+1;j6;j+) /*found* if(strcmp(*(pstr+i),pstr+j)0) p=*(pstr+i); /*found* *(pstr+i)=pstr+j; *(pstr+j)=p; void main() int i; char * pstr6,str6MAX; system(“CLS“
12、); for(i=0;i6;i+)pstri=stri; printf(“/nEnter 6 string(1 string at eachline):/n“); for(i=0;i6;i+)scanf(“%s“,pstri); proc(pstr); printf(“The strings after sorting:/n“); for(i=0;i6;i+)printf(“%s/n“,pstri); (分数:30.00)解析:错误:proc(char*pstr6) 正确:void proc(char*pstr6) 错误:if(strcmp(*(pstr+i),pstr+j)0) 正确:if(
13、strcmp(*(pstr+i),*(pstr+j)0) 错误:*(pstr+i)=pstr+j; 正确:*(pstr+i)=*(pstr+j); 解析 由主函数的调用可知,函数 proc()没有返回值。因此,“proc(char*pstr6)”应改为“void proc(char*pstr6)”。题目中要求按字符串由小到大的顺序进行排序,需要比较每一个字符串的大小,因此,“if(strcmp(*(pstr+i),pstr+j)0)”应改为“if(strcrop(*(pstr+i),*(pstr+j)0)”。顺序不对的 2 个字符串要互换其指针所指向的地址,赋值的两个变量其类型应该相同,因此,
14、“*(pstr+i)=pstr+j;”应改为“*(pstr+i)=*(pstr+j);”。三、程序设计题(总题数:1,分数:40.00)3.下列程序定义了 MM 的二维数组,并在主函数中自动赋值。请编写函数 proc(int aM),该函数的功能是:使数组左下半三角元素中的值全部置成 0。例如,a 数组中的值为: a=1 7 4 0 9 4 8 8 2 4 5 5 1 7 1 1 5 2 7 6 1 4 2 3 2 则返回主程序后 a 数组中的值应为: 0 7 4 0 9 0 0 8 2 4 0 0 0 7 1 0 0 0 0 6 0 0 0 0 0 注意:部分源程序给出如下。 请勿改动 ma
15、in()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h #includetime.h #includestdlib.h #define M 5 void proc(int aM) void main() int arrMM,i,j; srand(unsigned)time(NULL); system(“CLS“); printf(“*The array*/n“); for(i=0;iM;i+) /产生一个随机的 5*5 矩阵 for(j=0;jM;j+) arrij=rand()%1
16、0; printf(“%4d“,arrij); printf(“/n“); proc(arr); printf(“THE RESULT/n“); for(i=0;iM;i+) for(j=0;jM;j+) printf(“%4d“,arrij); printf(“/n“); (分数:40.00)_正确答案:()解析:void proc(int aM) int i,j; ror(i=0;iM;i+)/i 表示行下标 for(j=0;j=j;j+) /j 表示列下标 aij=0; /数组左下半三角元素中的值全部置成 0 解析 要使数组左下半三角元素中的值全部置成 0,先要找出数组的下半三角元素,再将其值全部赋值为 0。二维数组中下半三角元素的特点为,其列下标小于其行下标。根据这个特点,将二维数组的下半三角元素都赋值为 0。