最新C语言程序改错题.doc
精品资料C语言程序改错题.第12次上机 程序改错第1题【程序改错】-功能:求二分之一的圆面积,函数通过形参得到圆的半径,函数 返回二分之一的圆面积。例如:输入圆的半径值:19.527 输出为:s = 598.950017。-*/#include <stdio.h>#include <conio.h>/*FOUND*/double fun( r) double fun(double r) double s; /*FOUND*/ s=1/2*3.14159* r * r; s=1.0/2*3.14159*r*r; /*FOUND*/ return r; return s;main() float x; printf ( "Enter x: "); scanf ( "%f", &x ); printf (" s = %fn ", fun ( x ) );第2题 【程序改错】功能:先将在字符串s中的字符按逆序存放到t串中,然后把s中的 字符按正序连接到t串的后面。例如:当s中的字符串为:“ABCDE”时,则t中的字符串应为: “EDCBAABCDE”。-*/#include <conio.h>#include <stdio.h>#include <string.h> void fun (char *s, char *t) /*FOUND*/ int i; int sl,i; sl = strlen(s); for (i=0; i<sl; i+) /*FOUND*/ ti = ssl-i; ti=ssl; for (i=0; i<sl; i+) tsl+i = si; tsl+i=ssl-i-1; /*FOUND*/ t2*sl = "0" t2*sl =0 main() char s100, t100; printf("nPlease enter string s:"); scanf("%s", s); fun(s, t); printf("The result is: %sn", t);第3题 【程序改错】功能:求出以下分数序列的前n项之和。和值通过函数值返回main 函数。 2/1+3/2+5/3+8/5+13/8+21/13 例如:若n = 5,则应输出:8.391667。-*/#include <conio.h>#include <stdio.h> /*FOUND*/fun ( int n ) double fun(int n) int a, b, c, k; double s; s = 0.0; a = 2; b = 1; for ( k = 1; k <= n; k+ ) /*FOUND*/ s = (double)a / b; s=s+(double)a / b; c = a; a = a + b; b = c; /*FOUND*/ return c; return s;main( ) int n = 5; printf( "nThe value of function is: %lfn", fun ( n ) );第4题 【程序改错】-功能:以下程序能求出1*1+2*2+.+n*n<=1000中满足条件的 最大的n。-*/#include <stdio.h>#include "string.h"main() int n,s; /*FOUND*/ s=n=0; s=0,n=0; /*FOUND*/ while(s>1000) while(s<=1000) +n; s+=n*n; /*FOUND*/ printf("n=%dn",&n-1); printf("n=%dn",n-1); 第5题 【程序改错】-功能:利用二维数组输出如图所示的图形。 * * * * * * *-*/#include <stdio.h>#include <conio.h>/*FOUND*/#define N= 7 #define N 7 main() char aNN; int i,j,z; for(i=0;i<N;i+) for(j=0;j<N;j+) /*FOUND*/ aij=; aij= ; z=0; for(i=0;i<(N+1)/2;i+) for(j=z;j<N-z;j+) aij='*' z=z+1; /*FOUND*/ z=0; for(i=(N+1)/2;i<N;i+) z=z-1; z-; for(j=z;j<N-z;j+) aij='*' for(i=0;i<N;i+) for(j=0;j<N;j+) /*FOUND*/ printf("%d",aij); printf("%c",aij); printf("n"); 第6题 【程序改错】-功能:求出a所指数组中最大数和次最大数(规定最大数和次最大 数不在a0和a1中),依次和a0、a1中的数对调。例如:数组中原有的数:7、10、12、0、3、6、9、11、5、8, 输出的结果为:12、11、7、0、3、6、9、10、5、8。-*/#include <conio.h>#include <stdio.h>#define N 20 void fun ( int * a, int n ) int k,m1,m2,max1,max2,t; max1=max2= -32768; m1=m2=0; for ( k = 0; k < n; k+ ) if ( ak>max1 ) max2 = max1; m2 = m1; max1 = ak; m1 = k; /*FOUND*/ else if( ak>max1 ) else if( ak>max2) max2 = ak; m2 = k; /*FOUND*/ t = a0; am1=a0; a0=am1; am1 = t; /*FOUND*/ t = a1; am2=a1; a1=am2 am2 = t;main( ) int bN=7,10,12,0,3,6,9,11,5,8, n=10, i; for ( i = 0; i<n; i+) printf("%d ",bi); printf("n"); fun (b, n); for ( i=0; i<n; i+ ) printf("%d ",bi); printf("n");第7题【程序改错】-功能:根据以下公式求值,并作为函数值返回。例如:给指定精度的变量eps输入0.0005时,应当输出Pi=3.140578。 1 1 2 1 2 3 1 2 3 4 = 1 + + x + x x + x x x + .2 3 3 5 3 5 7 3 5 7 9-*/#include <stdio.h>double fun(double eps) double s,t; int n=1; s=0.0; t=1; /*FOUND*/ while(t<=eps) s+=t; /*FOUND*/ t=n/(2*n+1)*t; n+; /*FOUND*/ return s;main() double x; scanf("%lf",&x); printf("neps=%lf,Pi=%lfnn",x,fun(x);第8题 【程序改错】-功能:读入一个整数k(2k10000),打印它的所有质因子(即 所有为素数的因子)。例如:若输入整数:2310,则应输出:2、3、5、7、11。 请改正程序中的语法错误,使程序能得出正确的结果。-*/#include "conio.h"#include <stdio.h>/*FOUND*/isPrime(integer n ) int isPrime(int n ) int i, m; m = 1; for ( i = 2; i < n; i+ ) /*FOUND*/ if ( n%i ) if ( !(n%i) ) m = 0; break; /*FOUND*/ return n return n; main( ) int j, k; printf( "nPlease enter an integer number between 2 and 10000: " ); scanf( "%d", &k ); printf( "nnThe prime factor(s) of %d is( are ):", k ); for( j = 2; j <= k; j+ ) if(!( k%j)&&( IsPrime(j) printf( "n %4d", j ); printf("n");第9题 【程序改错】-功能:从m个学生的成绩中统计出高于和等于平均分的学生人数, 此人数由函数值返回。平均分通过形参传回,输入学生成 绩时,用-1结束输入,由程序自动统计学生人数。例如:若输入 8 名学生的成绩,输入形式如下: 80.5 60 72 90.5 98 51.5 88 64 -1 结果为: The number of students :4 Ave = 75.56。-*/#include <conio.h>#include <stdio.h>#define N 20 int fun ( float *s, int n, float *aver ) float av, t ; int count , i; count = 0; t=0.0; for ( i = 0; i < n; i+ ) t += s i ; av = t / n; printf( "ave =%fn",av ); for ( i = 0; i < n; i+ ) /*FOUND*/ if ( s i < av ) count+; if ( s i > av ) count+; /*FOUND*/ aver = av; *aver = av /*FOUND*/ return count return count;main() float a, s30, aver; int m = 0; printf ( "nPlease enter marks ( -1 to end):n "); scanf("%f",&a ); while( a>0 ) sm = a; m+; scanf ( "%f", &a ); printf( "nThe number of students : %dn" , fun ( s, m, &aver ); printf( "Ave = %6.2fn",aver );第10题 【程序改错】-功能:实现两个字符串的连接。例如:输入dfdfqe和12345时,则输出dfdfqe12345.-*/#include <stdio.h>main() char s180,s280; void scat(char s1,char s2); gets(s1); gets(s2); scat(s1,s2); puts(s1);void scat (char s1,char s2) int i=0,j=0; /*FOUND*/ while(s1i= ='0') while(s1i!='0') i+; /*FOUND*/ while(s2j= ='0') while(s2j!='0') /*FOUND*/ s2j=s1i; s1j=s2i i+; j+; /*FOUND*/ s2j='0' s1j='0'