您好,欢迎访问三七文档
试卷编号:10766所属语言:C语言试卷方案:TC_150604083824试卷总分:440分共有题型:1种一、程序改错共44题(共计440分)第1题(10.0分)题号:360难度:中第2章/*------------------------------------------------------【程序改错】--------------------------------------------------------功能:计算正整数num的各位上的数字之积。例如:若输入:252,则输出应该是:20。若输入:202,则输出应该是:0。------------------------------------------------------*/#includestdio.h#includeconio.hlongfun(longnum){/**********FOUND**********/longk=1do{k*=num%10;/**********FOUND**********/num\=10;/**********FOUND**********/}while(!num);return(k);}main(){longn;printf(Pleaseenteranumber:);scanf(%ld,&n);printf(\n%ld\n,fun(n));}答案:=======(答案1)=======longk=1;=======(答案2)=======num/=10;=======(答案3)=======}while(num);=========或=========}while(num!=0);第2题(10.0分)题号:554难度:中第2章/*-------------------------------------------------------【程序改错】---------------------------------------------------------题目:以下程序输入一个整数n,计算累加和(1+2+3+…+n)并输出。请找出错误并改正。如输入5↙输出:Thesumfrom1to5is15-------------------------------------------------------*/#includestdio.hmain(){/***********FOUND***********/inti,n,sum;/***********FOUND***********/scanf(%d,n);for(i=0;i=n;i++);/***********FOUND***********/sum+=n;printf(%d,sum);}答案:=======(答案1)=======inti,n,sum=0;=======(答案2)=======scanf(%d,&n);=======(答案3)=======sum+=i;第3题(10.0分)题号:551难度:中第1章/*-------------------------------------------------------【程序改错】---------------------------------------------------------题目:下列程序从键盘输入一个数n,求1~n之和并输出。请纠正程序中存在错误,使程序实现其功能。如输入5↙输出:Thesumfrom1to5is15-------------------------------------------------------*/#includestdio.hmain(){inti=1,n;/***********FOUND***********/floatsum;/***********FOUND***********/scanf(%d,n);/***********FOUND***********/for(;in;i++)sum+=i;printf(Thesumfrom1to%dis%f\n,n,sum);}答案:=======(答案1)=======floatsum=0;=======(答案2)=======scanf(%d,&n);=======(答案3)=======for(;i=n;i++)第4题(10.0分)题号:22难度:易第1章/*------------------------------------------------------【程序改错】--------------------------------------------------------功能:计算数组元素中值为正数的平均值(不包括0)。例如:数组中元素的值依次为39,-47,21,2,-8,15,0,则程序的运行结果为19.250000。------------------------------------------------------*/#includestdio.hdoublefun(ints[]){/**********FOUND**********/intsum=0.0;intc=0,i=0;/**********FOUND**********/while(s[i]=0){if(s[i]0){sum+=s[i];c++;}i++;}/**********FOUND**********/sum\=c;/**********FOUND**********/returnc;}main(){intx[1000];inti=0;do{scanf(%d,&x[i]);}while(x[i++]!=0);printf(%f\n,fun(x));}答案:=======(答案1)=======doublesum=0.0;=========或=========doublesum=0;=======(答案2)=======while(s[i]!=0)=========或=========while(!s[i]==0)=========或=========while(!0==s[i])=========或=========while(s[i])=======(答案3)=======sum/=c;=========或=========sum=sum/c;=======(答案4)=======returnsum;=========或=========return(sum);第5题(10.0分)题号:11难度:中第1章/*------------------------------------------------------【程序改错】--------------------------------------------------------功能:用下面的和式求圆周率的近似值。直到最后一项的绝对值小于等于0.0001。π111—=1-—+—-—+...4357------------------------------------------------------*/#includestdio.h/**********FOUND**********/#includestdlib.hfun(){inti=1;/**********FOUND**********/ints=0,t=1,p=1;/**********FOUND**********/while(fabs(t)=1e-4){s=s+t;p=-p;i=i+2;t=p/i;}/**********FOUND**********/printf(pi=%d\n,s*4);}main(){fun();}答案:=======(答案1)=======#includemath.h=========或=========#includemath.h=======(答案2)=======floats=0,t=1,p=1;=========或=========floats=0,p=1,t=1;=========或=========floatp=1,s=0,t=1;=========或=========floatp=1,t=1,s=0;=========或=========floatt=1,p=1,s=0;=========或=========floatt=1,s=0,p=1;=======(答案3)=======while(fabs(t)1e-4)=========或=========while(0.0001fabs(t))=========或=========while(1e-4fabs(t))=========或=========while(fabs(t)0.0001)=======(答案4)=======printf(pi=%f\n,s*4);第6题(10.0分)题号:13难度:中第1章/*------------------------------------------------------【程序改错】--------------------------------------------------------功能:求1到20的阶乘的和。------------------------------------------------------*/#includestdio.hfun(){intn,j;floats=0.0,t=1.0;for(n=1;n=20;n++){/**********FOUND**********/s=1;for(j=1;j=n;j++)/**********FOUND**********/t=t*n;/**********FOUND**********/s+t=s;}/**********FOUND**********/printf(jiecheng=%d\n,s);}main(){fun();}答案:=======(答案1)=======t=1;=========或=========t=1.0;=======(答案2)=======t=t*j;=========或=========t*=j;=========或=========t=j*t;=======(答案3)=======s=s+t;=========或=========s+=t;=========或=========s=t+s;=======(答案4)=======printf(jiecheng=%f\n,s);第7题(10.0分)题号:444难度:易第2章/*------------------------------------------------------【程序改错】--------------------------------------------------------功能:编写函数fun计算下列分段函数的值:x*20x0且x≠-3f(x)=sin(x)0≤x10且x≠2及x≠3x*x+x-1其它------------------------------------------------------*/#includemath.h#includestdio.hdoublefun(doublex){/**********FOUND**********/doubley/**********FOUND**********/if(x0||x!=-3.0)y=x*20;elseif(x=0&&x10.0&&x!=2.0&&x!=3.0)y=sin(x);elsey=x*x+x-1;/**********FOUND**********/returnx;}main(){doublex,f;printf(inputx=);scanf(%f,&x);f=fun(x);printf(x=%f,f(x)=%f\n,x,f);}答案:=======(答案1)====
本文标题:C++程序改错
链接地址:https://www.777doc.com/doc-2901713 .html