2022年二级C语言机试试题 .pdf





《2022年二级C语言机试试题 .pdf》由会员分享,可在线阅读,更多相关《2022年二级C语言机试试题 .pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、请补充 fun函数,该函数的功能:按 0到 9统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能使用字符串库函数。例如,输入 g1ters4543123564879fgfd,结果为 1=2,3=2,5=2,7=1,9=1。请勿改动主函数main与其他函数中的任何内容,仅在 fun函数的横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#include#define N 500 void fun(char*t,int count)int i,j;int a10;char*p=t;for(i=0;i=0&*p=9)_2_;p+;for(i=1
2、,j=0;i10;i=i+2,j+)_3_;main()char sN;int count10,i;printf(nPlease enter a char string:);gets(s);printf(nThe original stringn);puts(s);fun(s,count);printf(nThe countber of letern);for(i=0;i5;i+)printf(n);printf(%d=%d,2*i+1,counti);printf(n);【答案】(1)*p(2)a*p-0+(3)countj=ai下列给定程序中,函数fun的功能是从字符串 s中删除所有小写字
3、母 c。请修改程序中的错误,使它能计算出正确的结果。注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构。#include void fun(char*str)int i,j;for(i=j=0;stri!=0;i+)/*error*/if(stri!=c)strj=stri;/*error*/stri=0;main()char str100;printf(Enter a string with c:);gets(str);printf(The original string:);puts(str);fun(str);printf(The string after deleted
4、:);puts(str);printf(nn);【答案】(1)strj+=stri;(2)strj=0;M名学生的成绩已在主函数中放入一个带头节点的链表结构中,a指向链表的头节点。请编写函数 fun,它的功能是求出平均分,由函数值返回。例如,若学生的成绩是69、85、91、72、64,则平均分应当是76.200。请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include#define M 5 struct list double s;struct list*next;typedef struct lis
5、t SCORE;double fun(SCORE*a)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 10 页 -SCORE*creat(double*stu)SCORE*a,*p,*q;int i=0;a=p=(SCORE*)malloc(sizeof(SCORE);p-s=0;while(is=stui;i+;p-next=q;p=q;p-next=0;return a;outlist(SCORE*a)SCORE*p;p=a-next;printf(a);do printf(-%4.1f,p-s);p=p-next;while(p!=0);printf(nn);main()d
6、ouble stuM=69,85,91,72,64,ave;SCORE*a;FILE*out;a=creat(stu);outlist(a);ave=fun(a);printf(ave=%6.3fn,ave);out=fopen(outfile.dat,w);fprintf(out,%6.3f,ave);fclose(out);【答案】double aver=0.0;/初始化平均值while(a!=NULL)/如果头指针不指向链表尾,就累加链表中成绩 aver+=a-s;/累加成绩a=a-next;/指向下一个节点 aver/=M;/求平均分return aver;请补充 fun函数,该函数的
7、功能是逐个比较s1、s2两个字符串对应位置中的字符,把ASCII 值小或相等的字符依次存放到s数组中,形成一个新的字符串。例如,若 s1中的字符串为 aBCDeFgH,s2中的字符串为 ABcd,则s中的字符串应为ABCD。请勿改动主函数main与其他函数中的任何内容,仅在 fun函数的横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#include void fun(char*a,char*b,char*s)int i=_1_;while(*a _2_*b)if(*a*b)si=*b;else si=*a;if(*a)a+;if(*b)b+;i+;main()ch
8、ar s110=aBCDeFgH,s210=ABcd,s80=0;fun(s1,s2,s);printf(The string s1:);puts(s1);printf(The string s2:);puts(s2);printf(The result s:);puts(s);【答案】(1)0(2)|下列给定程序中,函数fun的功能是根据整型参数 n,计算如下公式的值:1nn23121x11x,x11x,x11x1,x名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 10 页 -例如,若 n=4,则应输出 0.600000。请修改程序中的错误,得出正确的结果。注意:不要改动 ma
9、in函数,不能增行或删行,也不能更改程序的结构。#include#include /*error*/fun(int n)double x=1;int i;/*error*/for(i=2;in;i+)x=1.0/(1+x);return x;main()int n;printf(nPlease enter n:);scanf(%d,&n);printf(x%d=%lfn,n,fun(n);【答案】(1)double fun(int n)(2)for(i=2;i=n;i+)请编写函数 fun,其功能是计算并输出给定10个数的方差:1011012101)(101kkkky,yyyF其中例如,给定的
10、 10个数为 76.0、65.0、72.0、85.0、57.0、67.0、45.0、92.0、44.0、58.0,则输出为 F=14.982990。请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include double fun(double y10)main()double f,y10=76.0,65.0,72.0,85.0,57.0,67.0,45.0,92.0,44.0,58.0;int i;FILE*out;printf(nThe original data is:n);for(i=0;i10;
11、i+)printf(%6.1f,yi);printf(nn);f=fun(y);printf(f=%fnn,f);out=fopen(outfile.dat,w);fprintf(out,%f,f);fclose(out);【答案】int i,j;double f=0.0,f1=0.0;/初始化变量for(i=0;i10;i+)/求公式中y的值f1+=yi;f1/=10;for(j=0;js2。请勿改动主函数main与其他函数中的任何内容,仅在 fun函数的横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#include#define N 80 char*fun(c
12、har*s1,char*s2)char*t1=s1,*t2=s2;while(*t1&*t2)if(_1_)return;t1+;t2+;if(*t1=*t2)return=;if(*t1=_3_)return;main()char s1N,s2N;printf(Please input s1:n);gets(s1);printf(Please input s2:n);gets(s2);printf(The result isn);printf(s1%s s2,fun(s1,s2);【答案】(1)*t1*t2(3)0下列给定的程序中,函数fun的功能是应用递归算法求某数b的平方根。求平方根的迭
13、代公式如下:)00(1xbx21x例如,2的平方根值为1.414214。请修改程序中的错误,得出正确的结果。注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构。#include#include /*error*/fun(double b,double x0)double x1,y;x1=(x0+b/x0)/2.0;/*error*/if(abs(x1-x0)=1e-6)y=fun(b,x1);else y=x1;return y;main()double n;printf(Enter n:);scanf(%lf,&n);printf(The square root of%lf
14、is%lfn,n,fun(n,1.0);【答案】(1)double fun(double b,double x0)(2)if(fabs(x1-x0)=1e-6)程序定义了 M M的二维数组,并在主函数中自动赋值。请编写函数fun(int M,int n),该函数的功能是使数组左下半三角元素中的值乘以 n。例如,若 n的值为 0,matrix 数组中的值为:288490471matrix,则返回主程序后,matrix 数组中的值应为:000400470请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#inclu
15、de#include#define M 3 void fun(int matrixM,int n)main()int matrixMM,n,i,j;FILE*out;printf(The array n);for(i=0;iM;i+)for(j=0;jM;j+)matrixij=rand()%10;printf(%4d,matrixij);printf(n);n=rand()%4;printf(n=%4dn,n);fun(matrix,n);printf(THE RESULTn);for(i=0;iM;i+)for(j=0;jM;j+)printf(%4d,matrixij);printf(n
16、);out=fopen(outfile.dat,w);for(i=0;iM;i+)for(j=0;jM;j+)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 10 页 -matrixij=i*j+1;fun(matrix,9);for(i=0;iM;i+)for(j=0;jM;j+)fprintf(out,%4d,matrixij);fprintf(out,n);fclose(out);【答案】int i,j;for(i=0;iM;i+)for(j=0;j=i;j+)matrixij=matrixij*n;请补充 fun函数,该函数的功能是寻找两个整数之间的所有素数(包括这两个整
17、数),把结果保存在数组a中,函数返回素数的个数。例如,输入 3和18,则输出为 357111317。请勿改动主函数main与其他函数中的任何内容,仅在 fun函数的横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#include#define N 500 int fun(int n,int m,int aN)int i,j,k=0,flag;for(j=n;j=m;j+)_1_;for(i=2;ij;i+)if(_2_)flag=0;break;if(_3_)ak+=j;return k;main()int n=0,m=0,i,k;int aN;printf(Inp
18、ut nn);scanf(%d,&n);printf(Input mn);scanf(%d,&m);for(i=0;im-n;i+)ai=0;k=fun(n,m,a);for(i=0;ik;i+)printf(%4d,ai);【答案】(1)flag=1(2)j%i=0(3)flag=1 下列给定程序中函数fun的功能是:传入一个整数 n,计算如下公式的值:n121s311例如,若输入 5,则应输出-0.283333。请修改函数 fun中的错误或在横线处填上适当的内容并把横线删除,使它能计算出正确的结果。注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构。#include#inc
19、lude double fun(int n)double s=1.0;int i;/*error*/for(i=2;i=n;i+)s=1.0-1/i;/*error*/_填 空_ main()int n;printf(nPlease enter 1 integer numbers:n);scanf(%d,&n);printf(nnThe result is%lfn,fun(n);【答案】(1)s=s-1.0/i;/s-=1.0/i;/s-=(double)1/i(2)return s;请编写函数 fun,其功能是将两个两位数的正整数 a、b合并成一个整数放在c中。合并的方式:将a的十位和个位依
20、次放在c的百位和个位上,b的十位和个位依次放在c的千位和十位上。例如,当 a=45,b=12,调用该函数后,名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 10 页 -c=1425。请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include void fun(int a,int b,long*c)main()int a,b;long c;FILE*out;printf(Input a,b:);scanf(%d%d,&a,&b);fun(a,b,&c);printf(The result i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年二级C语言机试试题 2022 二级 语言 试试

限制150内