1、C语言题库(一)程序改错=题号:866/*- 【程序改错】-功能:用下面的和式求圆周率的近似值。直到最后一项的绝对值 小于等于0.0001。 1 1 1 - = 1 - - + - - - + . 4 3 5 7-*/#include /*ERROR*/#include fun() int i=1; /*ERROR*/ int s=0,t=1,p=1; /*ERROR*/ while(fabs(t)=1e-4) s=s+t; p=-p; i=i+2; t=p/i; /*ERROR*/ printf(pi=%dn,s*4);main() fun();答案:位置 1:#include math.h
2、 【或】 #include 位置 2:float s=0,t=1,p=1; 【或】 float s=0,p=1,t=1; 【或】 float p=1,s=0,t=1; 【或】 float p=1,t=1,s=0; 【或】 float t=1,p=1,s=0; 【或】 float t=1,s=0,p=1;位置 3:while(fabs(t)1e-4) 【或】 while(0.0001fabs(t) 【或】 while(1e-40.0001)位置 4:printf(pi=%fn,s*4); 【或】 printf(pi=%fn,4*s);题号:13282/*- 【程序改错】-题目:以下函数功能,完成
3、字符串的复制。-注意:不可以增加或删除程序行,也不可以更改程序的结构。-*/#include stdio.h#include string.hvoid f(char *p) char str10,*p1; strcpy(str,string); /*ERROR*/ p1=*str; for(;*p+=*p1+;);main( ) /*ERROR*/ char a20,p=a; f(p); puts(p);答案:位置 1:p1=str;位置 2:char a20,*p=a;题号:8026/*- 【程序改错】-功能:将整型数组中所有小于0的元素放到所有大于0的元素的前 面(要求只能扫描数组一次)。
4、-*/#include stdio.h#define Max 100void fun(int a,int n) /*ERROR*/ int i=5;j=n-1,temp; while(ij) while(ai=0) /*ERROR*/ j+; /*ERROR*/ if(ij) temp=ai; ai=aj; aj=temp; void main() static a=1,-3,-1,3,2,4,-4,5,-5,-2,n=10,i; fun(a,n); for(i=0;i10;i+) printf(%d ,ai);答案:位置 1:int i=0,j=n-1,temp; 【或】 int i=0;
5、int j=n-1,temp;位置 2:j-;位置 3:if(ij)题号:4067/*- 【程序改错】-功能:求1到20的阶乘的和。-*/#include stdio.hvoid fun() int n,j; float s=0.0,t=1.0; for(n=1;n=20;n+) /*ERROR*/ s=1; for(j=1;j=n;j+) /*ERROR*/ t=t*n; /*ERROR*/ s+t=s; /*ERROR*/ printf(jiecheng=%dn,s);main() fun();答案:位置 1:t=1; 【或】 t=1.0; ?位置 2:t=t*j; 【或】 t*=j; 【
6、或】 t=j*t;位置 3:s=s+t; 【或】 s+=t; 【或】 s=t+s;位置 4:printf(jiecheng=%fn,s);题号:185/*- 【程序改错】-功能:编写一个程序计算某年某月有几天。(注意要区分闰年)-*/#include stdio.hmain() int yy,mm,len; printf(year,month=); scanf(%d%d,&yy,&mm); /*ERROR*/ switch(yy) case 1: case 3: case 5: case 7: case 8: case 10: case 12: len=31; /*ERROR*/ break
7、case 4: case 6: case 9: case 11: len=30; break; case 2: if (yy%4=0 & yy%100!=0 | yy%400=0) len=29; else len=28; break; /*ERROR*/ default printf(input error!n); break; printf(The length of %d %d id %dn,yy,mm,len);答案:位置 1:switch(mm)位置 2:break;位置 3:default :题号:13112/*- 【程序改错】-功能:求出NM整型数组的最小元素及其所在的行坐标及列
8、坐标 (如果最小元素不唯一,选择位置在最前面的一个)。例如:输入的数组为: 9 2 3 4 15 6 12 1 9 10 11 2 求出的最小数为1,行坐标为2,列坐标为1。-*/#define N 4#define M 3#include stdio.hint Row,Col;main() int aNM,i,j,min,row,col; printf(input a array:); for(i=0;iN;i+) for(j=0;jM;j+) scanf(%d,&aij); for(i=0;iN;i+) for(j=0;jM;j+) printf(%d,aij); printf(n); m
9、in=fun(a); printf(max=%d,row=%d,col=%d,min,Row,Col);int fun(int arrayNM) int min,i,j; min=array 00; Row=0; Col=0; for(i=0;iN;i+) /*ERROR*/ for(j=i;jM;j+) /*ERROR*/ if(min array ij) min=array ij; Row=i; /*ERROR*/ Col=i; return(min);答案:位置 1:for(j=0;jarray ij)位置 3:Col=j;题号:13258/*- 【程序改错】-题目:-注意:不可以增加或删除程序行,也不可以更改程序的结构。-
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1