计算机二级C上机练习题汇总
第一套
(1) 填空题
给定程序中,函数fun的功能是:在带有头结点的单向链表中,查找数据域中值为ch的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为ch的结点,函数返回0值。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANK1.C中。 不得增行或删行,也不得更改程序的结构!
#include
struct list *next; } SLIST;
SLIST *creatlist(char *); void outlist(SLIST *);
int fun( SLIST *h, char ch) { SLIST *p; int n=0; p=h->next;
/**********found**********/ while(p!=___1___) { n++;
/**********found**********/
if(p->data==ch) return___2___; else p=p->next; }
return 0; }
main()
{ SLIST *head; int k; char ch; char a[N]={'m','p','g','a','w','x','r','d'}; head=creatlist(a); outlist(head);
printf(\ scanf(\
/**********found**********/ k=fun(___3___);
if (k==0) printf(\
else printf(\ %d\\n\
}
SLIST *creatlist(char *a)
{ SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i { q=(SLIST *)malloc(sizeof(SLIST)); q->data=a[i]; p->next=q; p=q; } p->next=0; return h; } void outlist(SLIST *h) { SLIST *p; p=h->next; if (p==NULL) printf(\ else { printf(\ do { printf(\ p=p->next; } while(p!=NULL); printf(\ } } 第1 处内容:while(p!=0) 第2 处内容:if (p->data==ch) return n; 第3 处内容:k=fun(head,ch); (2) 改错题 给定程序modi.c中,函数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 #include /************found************/ int j,c=0;float xa=0.0; for(j=0;j printf(\ for(j=0;j /************found************/ if (x[j]=>xa) c++; return c; } main() {float x[100]={193.199f,195.673f,195.757f,196.051f,196.092f,196.596f,196.579f,196.763f}; system(\ printf(\} 第1 处:int j,c=0;float xa=0.0; 应改为 { int j,c=0;float xa=0.0; 第2 处:if(x[j]=>xa) 应改为if (x[j]>=xa) (3) 程序设计 请编写一个函数fun,它的功能是:找出一维整型数组元素中最大的值和它所在的下标,最大的值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。主函数中x是数组名,n是x中的数据个数,max存放最大值,index存放最大值所在元素的下标。 注意:部分源程序存在文件prog.c中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 /******本答案只作参考。******/ #include void fun(int a[], int n , int *max, int *d ) { int i,base,subscript; base=a[0]; subscript=0; for(i=1;i if(a[i]>base) { subscript=i; base=a[i]; } } *max=base; *d=subscript; } main() { int i, x[20], max , index, n = 10; int randomize() ; for (i=0;i < n;i++) {x[i] = rand()P; printf(\ printf(\ fun( x, n , &max, &index); printf(\ Index =M\\n\} 第二套 (1) 填空题 给定程序中,函数fun的功能是:统计出带有头结点的单向链表中结点的个数,存放在形参n所指的存储单元中。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANK1.C中。 不得增行或删行,也不得更改程序的结构! #include struct list *next; } SLIST; SLIST *creatlist(int *a); void outlist(SLIST *); void fun( SLIST *h, int *n) { SLIST *p; /**********found**********/ ___1___=0; p=h->next; while(p) { (*n)++; /**********found**********/ p=p->___2___; } } main() { SLIST *head; int a[N]={12,87,45,32,91,16,20,48}, num; head=creatlist(a); outlist(head); /**********found**********/ fun(___3___, &num); printf(\} SLIST *creatlist(int a[]) { SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i { q=(SLIST *)malloc(sizeof(SLIST)); q->data=a[i]; p->next=q; p=q; } p->next=0; return h; } void outlist(SLIST *h) { SLIST *p; p=h->next; if (p==NULL) printf(\ else { printf(\ do { printf(\ p=p->next; } while(p!=NULL); printf(\ } } 第1 处:(*n)=0; 第2 处:p=p->next; 第3 处:fun(head, &num); (2) 改错题 给定程序modi.c中,fun函数的功能是:将n个无序整数从小到大排序。请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构! #include #include for (j=0;j /************found************/ for (i=j+1;i /************found************/ t=i;