二、改错题
Cmody191.c
修改程序,用以计算1~100之间所有的偶数之和。#include
#include
void main( )
{
int i=1;
/**/ int sum=2 /**/;
while(i++,i<=100)
{ if(/**/ i==(i/2)*2 /**/)
continue;
sum+=i;
}
printf("Sum is %d\n",sum);
getch();
}
Cmody192.c
修改程序,将二维数组表示的方阵左下半三角(不含对角线)各元素加4,右上半三角(含对角线)各元素乘2。
#include
#define N 5
void main()
{
int a[N][N],i,j;
for(i=0;i for(j=0;j a[i][j]=i*5+j+11; printf("\nArray a is:\n"); for(i=0;i {for(j=0;j printf("%3d ",a[i][j]); printf("\n"); } for(i=0;i /**/ for(j=0;j<=i;j++)/**/ a[i][j]+=4; for(i=0;i /**/ for(j=i+1 ;j a[i][j]*=2; printf("\nArray a is turned:\n"); for(i=0;i {for(j=0;j printf("%3d ",a[i][j]); printf("\n"); } getch(); } Cmody201.c 修改程序,计算满足以下条件的整数n的最大值。22+42+62+82+……+n2<1000 #include #include void main() { int n=0, sum=0; /**/ while(sum<1000); /**/ { /**/ sum+=n^2 /**/; n++,n++; } printf("n=%d\n",n-2); getch(); } Cmody202.c 修改程序,实现从键盘输入一串字符,并在字符串中从第m个字符串开始截取n个字符。 例如:输入的字符串为”welcome”,m=2, n=3, 则截取的字串为”elc” #include #include void main() { char str[100],sub[100]; int m,n,len,i; printf("Enter string:"); gets(str); printf("\nEnter m n:"); scanf("%d%d",&m,&n); len=strlen(str); if( m-1+n>len ) printf("Can't run with %d and %d!\n",m,n); else { i=0; /**/ while(i<=n) /**/ { /**/ sub[i]=str[m+i]; /**/ i++; } /**/ sub[i]='\n'; /**/ printf("sub string is:%s\n",sub); } getch(); } Cmody211.c 修改程序,实现输入一整型二维数组,计算其中数组元素的最大值与最小值的差。 #include #include #define ROW 3 #define COL 4 void main() { int a[ROW][COL],max,min,i,j,result; clrscr(); printf("Enter array a:\n"); for(i=0;i for(j=0;j scanf("%d",&a[i][j]); /**/ max=min=0; /**/ for(i=0;i for(j=0;j {if(/**/ min min=a[i][j]; if(a[i][j]>max) /**/ max=a[i][j] /**/ } printf("Result= %d\n",max-min); getch(); } Cmody212.c 修改程序cmody212.c,实现从键盘输入一串字符和一个字串,输出该子串在原字符串中出现的次数。#include #include void main() { int i,j,k,count; char s1[100],s2[100]; printf("Enter main String:"); gets(s1); printf("Enter Sub String:"); gets(s2); count=0; /**/ for(i=0;*s1!='\0';i++) /**/ { /**/ for(k=0; (s1[j]==s2[k])&&(s1[j]!='\0'); j++,k++ ); /**/ if(s2[k]=='\0') count++; } printf("\nCount=%d\n",count); getch(); } Cmody221.c 修改程序,输出存储在结构体数组中8位学生成绩的平均分以及最高学生的姓名 #include /**/ structure student /**/ { char name[10]; float score; }; void main( ) { struct student stu[8]={{"Mary",76},{"John",85}, {"Tom",81},{"Susa",87},{"Wilu",79},{"Yili",65},{"S onmu",73},{"Lichar",70}}; int i=0,mrk; float total=0,aver,max; max=stu[0].score; mrk=0; while(i<8) { /**/ total=total+stu[i]; /**/ if(stu[i].score>max) {max=stu[i].score; mrk=i; } /**/ i++ /**/ } aver=total/8; printf("\naver=%.2f \n Best is %s\n",aver,stu[mrk].name); getch(); } Cmody222.c 修改程序,完成其中n个字符串的升序排列 #include #include #define ROW 5 #define COL 30 void fun(char s[][COL],int n) { char temp[COL]; int i,j; for(i=0; i /**/ for (j=i+1; j if(strcmp(s[i],s[j])>0) { strcpy(temp,s[i]); strcpy(s[i],s[j]); strcpy(s[j],temp); } } void main() { int i; char str[][COL]={"DEF","BAEELCS","FHIAME","CBAD F","APMAE"}; /**/ fun(str,COL); /**/ clrscr(); for(i=0;i printf("%s\n",str[i]); getch(); } Cmody231.c 修改程序cmody231.c,实现将输入的十进制正整数转换成十六进制数,且用字符串存放该十六进制数#include #include #include void main() { int x,b,i,j; char s[5]; printf("Input a number(Dec): "); scanf("%d",&x); /**/ i=1; /**/ while(x>0) {b=x%16; if(b>9) s[i]=b-10+'A'; else /**/ s[i]=b /**/; x=x/16; i++; } printf("\nHex number is: "); for(j=i-1;j>=0;j--) putchar(/**/ s[i] /**/); getch(); } Cmody232.c 修改程序cmody232.c,输出下列9行数字金字塔1 121 12321 1234321 ……………………… 12345678987654321 #include void main() { int i,j,k; for(k=1;k<=9;k++) { /**/ for(i=0;i<=k;i++) /**/ printf(" "); for(i=1;i<=k;i++) printf("%d",i); /**/ for(j=k-1;j>1;j--) /**/ printf("%d",j); printf("\n"); } getch( ); } 三、填空题 Ccon191.c 程序ccon191.c,从键盘输入3个实型值,若它们能构成一个三角形(即任意两边之和大于第三边),则根据以下公式求该三角形面积;否则,提示相应信息。 #include #include void main() { float a,b,c,d,area; printf("Please input 3 numbers:\n"); scanf("%f,%f,%f",&a,&b,&c); if( a+b>c /**/ /**/) {d = (a+b+c)/2; area = /**/ /**/; printf("area is %.2f\n",area); } else printf("Not Triangle!\n"); getch(); } Ccon192.c 程序ccon192.c,计算:已知大货车限载8吨、中型货车限载3吨、微型货车限载1吨,需50辆车运输100吨货物时,应配备的大、中、微型车分别多少辆?共有多少种解决方案? #include #include int /**/ /**/( ) { int big,median,small,n=0; printf(" big median small \n"); for(big=0;big<=13;/**/ /**/) for(median=0;median<=33;median++) { small=100-8*big-3*median; if(/**/ /**/==50) { n++; printf("%d-->%2d %2d %2d\n", n,big,median,small); } } return n; } void main() { int num; num = fun(); printf("\n There are %d solutions.\n",num); getch(); } Ccon201.c 程序ccon201.c,按照以下袋装情况求一堆苹果的总数,总数在100~300之间。若按每袋装8个苹果则多出5个,若按每袋装7个苹果,则多出4个,若按每袋装5个苹果,则多出2个。 #include void main() { int n; for( n=300; /**/ /**/;n-- ) {if( n%8 == 5) if( !((n-4)%7) /**/ /**/) printf("n=%d\n", n); } getch(); } Ccon202.c 程序ccon202.c中的swap函数,通过指针变量实现两个变量值的交换。 #include void swap(/**/ /**/) { int sum; sum = *p1+*p2; *p1 =/**/ /**/; *p2 = sum-*p1; } void main() { 2 ) )( )( ( c b a d c d b d a d d area + + = -- - = 其中: int x,y; printf("please input 2 numbers:\n"); scanf("%d,%d",&x,&y); printf("\nOriginal: x=%d y=%d\n",x,y); swap(&x,&y); printf("\nNow: x=%d y=%d\n",x,y); getch(); } Ccon211.c 程序ccon211.c,按每行最多5个数方式输出100~400之间能同时被7和4整除的所有数及它们之和。 #include void main() { int i,n; int sum=0; /**/ /**/ for(i=101;i<=399;i++) { if(/**/ /**/ && !(i%4) ) {printf("%d ",i); sum+=/**/ /**/; n++; if(n%5==0) printf("\n"); } } printf("\n sum=%d\n",sum); getch(); } Ccon212.c 程序ccon212.c中的cmob函数用以输出等式a*b*c+a*c*c=560的所有a,b,c的组合(其中a,b,c 只取1~9之间的一位整数)。如a=7, b=2, c=8时,7*2*8+7*8*8=560 #include void comb(/**/ /**/) {int a,b,c; for( a=1;a<10;a++ ) for( b=1;b<10;b++ ) for( c=1;c<10;c++ ) { if(/**/ /**/ ==m) printf("a=%d,b=%d,c=%d\n",a,b,c); } } void main() { int result=560; comb(result); getch(); } Ccon221.c 程序ccon221.c,输出如下图形 % % % % % % % & & & & & & % % % % % & & & & % % % & & % #include #include void main() { int row,col; for (row=1;row<=7;row++) { printf("\t"); for(col=1;col<=/**/ /**/ ;col++) if(row%2) printf("%c",/**/ /**/); else printf("%c",/**/ /**/); printf("\n"); } getch(); } Ccon222.c 程序ccon222.c中的pnt函数,根据参数n(2≤n≤10),输出类似如下所示的方针(n=5). 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25 #include void pnt(int a[M][M],int n) {int i,/**/ /**/; for(i=0;i a[i][j]=/**/ /**/; else a[i][j]=i*n+n-j; } printf("array a(n=%d) is:\n",n); for(i=0;i printf("%3d",a[i][j]); printf("\n"); } } void main() { int a[M][M]; int n; printf("Input n(2<=n And n<=10):"); scanf("%d",&n); pnt(/**/ /**/,n); getch(); } Ccon231.c 程序ccon231.c, 计算以下分数序列的前18项之和 #include int i; float sum,a=2,b=1,t; /**/ /**/; for (i=1;i<=18;i++) { sum+=/**/ /**/; t=a; a=a+b; b=/**/ /**/; } printf("sum=%f\n",sum); getch(); } Ccon232.c 程序ccon232.c 中的inv 函数,通过指针变量将字符串str 的内容”asdfjk ”,调用inv 函数后,变成:”kjfdsa ”。 #include j=/**/ /**/; for(i=0;i *(str+i)=*(str+j); *(str+j)=/**/ /**/; } } void main( ) { char str[]="asdfjk"; printf("\n Original is: %s\n",str); inv(str); printf("\n Now is: %s\n",str); getch(); } 四、编程题 cprog191.c 打开程序cprog191.c,完成其中的fun 函数:根据输入的汇款数额,求应交的汇费。设应交汇费的计算公式如下: ,8 13 ,58,35,23,12??? ? ?? ? >≤<≤<≤-=50005000200020005005004444)2000(008.001.05)(x x x x x x fun #include /**/ } void main() { float x; double y; clrscr(); printf("Please input a number:\n"); scanf("%f",&x); y = fun(x); printf("f(%.2f)=%.2f\n",x,y); getch(); } cprog192.c 打开程序cprog192.c ,完成其中的fun 函数:对4×4矩阵从顶行开始各行按从左到右顺序查找,找出各行中0元素之后的所有负数,并按元素出现的先后顺序存放到数组b 中,并返回这些负数之和。如矩阵为: 则调用函数fun( )后,数组b 各元素依次为-3,-1,-4,-3,-12,-13,并返回-36. #include int fun(int a[][COL],int b[]) { /**/ /**/ } void main( ) { int arra[ROW][COL]={{-2,0,-3,-1},{-8,2,0,-4}, {0,3,-3,-12},{21,0,-13,3}}; int arrb[LEN],i,result; for(i=0;i result=fun(arra,arrb); printf("array arrb is:\n"); for(i=0;i if(arrb[i]==0) printf(" "); else printf("%4d",arrb[i]); printf("\nresult=%d",result); getch( ); } cprog201.c 打开cprog201.c ,完成其中的函数fun ,该函数的数学表达式是 #include /**/ } void main( ) ? ? ??? ? ??????-------313021 12330402813 2?? ? ? ?≤+>-=-10 )2sin()3(10) 1/(1)(x x x x e x fun x { float x; double y; printf("Please input a number:\n"); scanf("%f",&x); y = fun(x); printf("f(%.3f)=%.3f\n",x,y); getch(); } cprog202.c 打开程序cprog202.c ,完成其中的fun 函数: 函数fun(double x, int n),用以下公式近似计算cos(x)的值: #include double x; int n; double fun(double x,int n); printf("Please enter x,n:"); scanf("%lf%d",&x,&n); printf("cos(%lf)=%lf\n",x,fun(x,n)); getch(); } double fun(double x,int n) { /**/ /**/ } cprog211.c 打开cprog211.c ,完成其中的函数fun ,该函数的数学表达式是 #include /**/ } void main( ) { float x; double y; clrscr(); printf("Please input a number:\n"); scanf("%f",&x); y = fun(x); printf("f(%.2f)=%.2f\n",x,y); getch( ); } cprog212.c 打开程序cprog212.c ,完成其中的fun 函数:对两个字符串中小写英文字母的个数进行比较,并返回比较的结果。(注:第1个字符串比第2个字符串的小写英文字母个数多,则返回正数,相等返回0,个数少则返回负数) 例如:字符串str1为“abc123”,字符串str2为”abcd ”,则函数fun 的返回值应小于0。 #include int fun(char *str1, char *str2) { /**/ /**/ } ?? ? ??<<≤-≥=1 1011 210)3ln()(x x x x x x x fun )! 2()1(!6!4!21)cos(2642n x x x x x n n -++-+-= void main( ) { int rela; char *str1,*str2; clrscr(); printf("Please input string NO.1:"); gets(str1); printf("Please input string NO.2:"); gets(str2); rela=fun(str1,str2); if(rela>0) printf("Numbers of Lower char in %s > Numbers of Lower char in %s",str1,str2); if(rela==0) printf("Numbers of Lower char in %s = Numbers of Lower char in %s",str1,str2); if(rela<0) printf("Numbers of Lower char in %s < Numbers of Lower char in %s",str1,str2); getch(); } cprog221.c 打开程序cporg221.c,完成其中的fun函数:根据输入的x和y的值,按如下公式计算: 将计算结果的整数部分存入数组元素arr[0],并将该计算结果的小数部分的前两位存入arr[1]中。 #include #include void fun(int arr[],double x,int y) { /**/ /**/ } void main() { int y; double x; int arra[2]; printf("Please enter x(1 scanf("%lf%d",&x,&y); if((x>1.0 &&x<2.0)&&(y>0 && y<=6)) { fun(arra,x,y); printf("\npart1=%d,part2=%d",arra[0],arra[1]); } else printf("x or y is out of range!"); getch(); } cprog222.c 打开程序cprog222.c,完成其中的fun函数:将一个升序数组a和一个降序数组b中的所有元素按降序存入数组c中。 例如:数组a为{5,10,15,27,46},数组b为{50,45,42,29,15,8,5,2},则数组c就应为{50,46,45,42,29,27,15,15,10,8,5,5,2}。#include #define N 5 #define M 8 void fun(int a[],int b[],int c[]) { /**/ /**/ } void main() { int arra[N]={5,10,15,27,46},arrb[M]={50,45,42,29,15,8,5 ,2}; int arrc[N+M],k; fun(arra,arrb,arrc); printf("array arra is:"); for(k=0;k printf("%d ",arra[k]); printf("\narray arrb is:"); 6 0,0.2 0.1 )2 ( ) , (≤ < < < + =y x x y y x fun y for(k=0;k printf("%d ",arrb[k]); printf("\nresult arrc is:"); for(k=0;k cprog231.c 打开程序cprog231.c ,完成其中的函数fun ,该函数的递归公式是: #include double x; int n; double fun(double x,int n); printf("Please enter x,n:\n"); scanf("%lf%d",&x,&n); printf("fun=%lf\n",fun(x,n)); getch(); } double fun(double x,int n) { /**/ /**/ } cprog232.c 打开程序cprog232.c,完成其中的fun 函数:返回一维数组中的次大数(即仅小于最大数的数),设数组a 中没有重复的元素。 例如:数组元素为{45,58,33,24,40,20,30,28,31},函数fun 的返回值为45. #include /**/ /**/ } void main() { int arra[ROW]={45,58,33,24,40,20,30,28,31}; int max_pre,i; max_pre=fun(arra); printf("array arra is:"); for(i=0;i printf("%d ",arra[i]); printf("\nmax_pre is %d",max_pre); getch(); } ?? ? >-+==1 )1,()1(1),(n n x f x n x n x f 参考答案 二、改错题 cmody191.c /**/ int sum=2 /**/;→int sum=0; /**/ int sum=2 /**/;→if(i!=(i/2)*2) cmody192.c /**/ for(j=0;j<=i;j++)/**/→for(j=0; j /**/ for(j=i+1 ;j cmody201.c /**/ while(sum<1000); /**/→while(sum<1000) /**/ sum+=n^2 /**/;/**/ →sum+=n*n cmody202.c /**/ while(i<=n) /**/→while(i /**/ sub[i]=str[m+i]; /**/→sub[i]=str[m-1+i]; /**/ sub[i]='\n'; /**/→sub[i]= '\0' cmody211.c /**/ max=min=0; /**/→max=min=a[0][0]; /**/ mina[i][j] /**/ max=a[i][j] /**/→max=a[i][j]; cmody212.c /**/ for(i=0;*s1!='\0';i++) /**/ →for(i=0;*(s1+i)!=’\0’;i++) /**/ for(k=0; (s1[j]==s2[k])&&(s1[j]!='\0'); j++,k++ ); /**/→ for(k=0,j=i; (s1[j]==s2[k]&&(s2[k]!=’\0’); j++, k++); cmody221.c /**/ structure st udent /**/→struct student /**/ total=total+stu[i]; /**/→ total=total+stu[i].score /**/ i++ /**/→i++; cmody222.c /**/ for (j=i+1; j Cmody231.c /**/ i=1; /**/→i=0; /**/ s[i]=b /**/→s[i]=b+’0’;Cmody232.c /**/ for(i=0;i<=k;i++) /**/→for(i=0; i<9-k; i++) /**/ for(j=k-1;j>1;j--) /**/→for (j=k-1; j>=1; j--) 三、填空题 Ccon191.c if( a+b>c /**/&&b+c>a&&c+a>b/**/) area = /**/sqrt(d*(d-a)*(d-b)*(d-c)) /**/ Ccon192.c int /**/ fun /**/( ) for(big=0;big<=13;/**/big++ /**/) if(/**/ big+median+small /**/==50) Ccon201.c for( n=300; /**/ n>=100 /**/;n-- ) if( !((n-4)%7) /**/ &&(n-2)%5= =0 /**/) Ccon202.c void swap(/**/int *p1, int *p2 /**/) *p1 =/**/ *p2 /**/; Ccon211.c /**/ n=0 /**/ if(/**/!(i%7) /**/ && !(i%4) ) sum+=/**/i /**/; Ccon212.c void comb(/**/ int m /**/) if(/**/ a*b*c+a*c*c /**/ ==m) Ccon221.c for(col=1;col<=/**/ row /**/ ;col++) printf("%c",/**/ ‘%’ /**/); print f("%c",/**/ ‘&’ /**/); Ccon222.c int i,/**/ j /**/; a[i][j]=/**/i*n+j+1 /**/; pnt(/**/ a /**/,n); Ccon231.c /**/sum=0 /**/; sum+=/**/ a/b /**/; b=/**/ t /**/; Ccon232.c j=/**/strlen(str)-1 /**/; *(str+j)=/**/ k /**/; 四、编程题 cprog191.c double fun(float x) { /**/ double y; if(x<=500) y=5; else if(x<=2000) y=0.01; else if(x<=5000) y=0.008*(x-2000); else y=44; return y; /**/ } cprog192.c int fun(int a[][COL],int b[]) { /**/ int i, j, k, t=0, sum=0; for(i=0; i for(j=0; j if(a[i][j]==0) { for(k=j+1; k if(a[i][k]<0) { b[t]=a[i][k]; sum+=b[t]; t++;} } return sum; /**/ } cprog201.c double fun(float x) { /**/ double y; if(x>10) y=1/(1-exp(-x)); else y=(x+3)*sin(2*x); return y; /**/ } cprog202.c double fun(double x,int n) { /**/ int i; double t=1, sum=0; for(i=1; i<=n; i+=) { sum+=t; t=t*(-x)*x/(2*i)/(2*i-1); } return sum; /**/ } cprog211.c #include #include double fun(float x) { /**/ double y; if(x>=10) y=log(3*x); else if(x>=1) y=sqrt(2*x-1); else y=fabs(x); return y; /**/} cprog212.c #include int fun(char *str1, char *str2) { /**/ int n=0, m=0; char *p1, *p2; for(p1=str1; *p1!=’\0’; p1++) if(*p1>=’a’&&*p1<=’z’) n++; for(p2=st r2; *p2!=’\0’; p2++) if(*p2>=’a’&&*p2<=’z’) m++; return(n-m); /**/ } #include #include void fun(int arr[],double x,int y) { /**/ double z; z=(y+2)*pow(x,y) arr[0]=(int)z; arr[1]=(int)(z*100)%100; /**/ } cprog222.c void fun(int a[],int b[],int c[]) { /**/int i, j, t; for(i=0; i for(j=0; j for(i=0; i for(j=0; j if(c[j] {t=c[j]; c[j]=c[j+1]; c[j+1]=t;} /**/ } cprog231.c double fun(double x,int n) { /**/ double y; if(n==1) y=x; else if(n>1) y=(x+1)*fun(x, n-1); return y; /**/ } cprog232.c #include #define ROW 9 int fun(int a[ ]) {/**/(方法一)(假设法) int i, j, max1, max2; max1=a[0]>a[1]?a[0]:a[1]; max2=a[0] for(i=2; i if(a[i]>max1) { max2=max1; max1=a[i];} else if(a[i]>max2) a[i]=max2; } return max2; } /**/ (方法二) (先排序,后返回次最大值)/**/ int i, j, t; for(i=0; i for(j=O; j if(a[j] {t=a[j]; a[j]=a[j+1]; a[j+1]=t;} return a[1]; /**/ (方法三) int i, j, k, t; for(i=0; i<2; i++) {k=i; for(j=i+1; j if(a[j]>a[k]) k=j; if(k!=i) {t=a[k]; a[k]=a[i]; a[i]=t;} } return a[1]; /**/ C语言改错题库 目录 C语言改错题库 (1) 从低位开始取出长整型变量s中奇数位上的数,依次构成一个新数放在t中。 (3) 2.求一维数组a中的值为偶数的元素之和。 (3) 3.求二维数组a中的最大值。 (4) 5.猴子第一天摘下若干桃子 (5) 6. 将长整型数中每一位上为偶数的数依次取出,构成一个新数放在t中。高位仍在高位,低位仍在低位。 (5) 7.求出a所指数组中最大数(规定最大数不在a[0]中),最大值和a[0]中的数对调。 (6) 8.输出200~300之间的所有素数,要求每行输出8个素数。 (6) 9.打印九九乘法表 (6) 10.输出100~200之间既不能被3整除也不能被7整除的整数并统计这些整数的个数,要求每行输出8个数。 (7) 10.输出100~200之间既不能被3整除也不能被7整除的整数并统计这些整数的个数,要求每行输出8个数。 (7) 11.打印输出以下图形 (8) 12.求:1!+2!+3!+ ...... + 20! (8) 13.输入一个百分制成绩,打印出五级记分成绩。 (8) 14.求一维数组a中值为奇数的元素的平均值。 (9) 15.将[m,n]之间的所有素数存放到一维数组a中,并输出。 (9) 16.求一维数组a中所有元素的平均值。 (10) 17.将[2,100]之间的所有素数存放到一维数组a中,并输出这些素数。 (10) 18.求一维数组a中所有元素的平均值,结果保留两位小数。 (11) 19. 当M为5时,如果从键盘上输入的5个数 (11) 20. 在第一个循环中给a数组的前10个数组元素 (12) 21. 把数组a(大小为M)中前M-1个元素中的最大值放入a的最后一个元素中。 (12) 22.求排列组合的值 (12) 23. 计算正整数num的各位上的数字之和。 (13) 24.从键盘上输入一个正整数n,计算并输出n的阶乘。 (13) 25.从键盘上输入一个数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数. 例如:从键盘上输入2345, (14) 26. 求一维数组a中素数的个数。 (14) 27.输出Fabonacci数列:1,1,2,3,5,8,......的前20项的值,要求每行输出5个数。 .. (15) 28.求一维数组a中非素数之和。 (15) 29.求两个正整数m,n之间的非素数之和(包含m和n)。 (16) 30.将无符号八进制数字构成的字符串转换为十进制整数。 (16) 31.计算1到M(M为100)之间的奇数之和及偶数之和。 (16) 32.实现在N行M列的二维数组中,找出每一行上的最大值,且原数组值不变。 (17) 1、以下程序的功能是求如下表达式: 1 1 1 S = 1 + ——+ ———+ ...... + ——————— 1+2 1+2+3 1+2+3+......+n 『答案』:1 scanf("%d",&n); 2 float fun(int n) 3 for(i=1;i<=n;i++) 4 s+=1.0/t 或s= s + 1.0 /t; 2、题目:函数fun的功能是:根据整型形参m的值,计算如下公式的值。 1 1 1 t = 1- ——- ——- ... - —— 2x2 3x3 mxm 例如,若m=5,则应输出:0.536389 『答案』:1 for(i=2;i<=m;i++)或for(i=2;i C语言改错题常见错误归纳 一、书写错误 1:mian→(main)见例一 2:print→(printf) 3:include 五、赋值错误 1:未赋初值。如求和时一般sum=0,求阶乘时一般n=1。见例一 2:注意题中赋值是否正确。如究竟i是赋1还是0。见例五 六、大小关系错误 1:大于号和小于号与所求程序不符。见例一 2:是否携带等号<=,>= 七、返回值错误 1:在子函数中,return (i)i 是要返回到主函数的值。 2:在子函数中,return(1)返回主函数是执行后yes 的结果,return(0) 返回执行后no的结果。见例二 3:一般在主函数中很少涉及返回值的错误 八、其他错误 1、参数未定义:程序中出现的任何变量i或常量j=1,都要定义他们的类型。 2、等号与赋值弄混好:==为等号,=为赋值号。见例二 3、与scanf搭配未加地址符, 也不可加\n换行符在d 后面,scanf(“%d”,&i); 4、行末未加分号,每行后面需要分号。 5、子函数中形参为定义好。见例六 经典例题 第12次上机程序改错第1题 【程序改错】 -------------------------------------------------------- 功能:求二分之一的圆面积,函数通过形参得到圆的半径,函数返回二分之一的圆面积。 例如:输入圆的半径值:19.527 输出为:s = 598.950017。 ------------------------------------------------------*/ #include printf ( "Enter x: "); scanf ( "%f", &x ); printf (" s = %f\n ", fun ( x ) ); } 第2题【程序改错】 功能:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。 例如:当s中的字符串为:“ABCDE”时,则t中的字符串应为:“EDCBAABCDE”。 ------------------------------------------------------*/ #include C语言改错题库
C语言程序改错2
C语言常见改错
C语言程序改错题
齐齐哈尔大学C语言题库 程序改错(部分)