改 错 题
1、给定程序中fun函数的功能是:根据整型形参m的值,计算如下公式的值:
例如,若m中的值为5,则应输出0.536389。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
double fun ( int m )
{ double y = 1.0 ;
int i ;
/**************found**************/
for(i = 2 ; i < m ; i ) 改为:for(i = 2 ; i <= m ; i )
/**************found**************/
y -= 1 /(i * i) ; 改为:y -= 1.0 /(i * i) ;
return( y ) ;
}
main( )
{ int n = 5 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ;
}
2、给定程序中fun函数的功能是:将s所指字符串的正序和反序进行连接,形成一个新串放在t所指的数组中。
例如,当s所指字符串为“ABCD”时,则t所指字符串的内容应为“ABCDDCBA”
#include <stdio.h>
#include <string.h>
/************found************/
void fun (char s, char t) 改为:void fun (char * s, char * t)
{ int i, d;
d = strlen(s);
for (i = 0; i<d; i ) t[i] = s[i];
for (i = 0; i<d; i ) t[d i] = s[d-1-i];
/************found************/
t[2*d-1] = '\0'; 改为:t[2*d] = '\0';
}
main()
{ char s[100], t[100];
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t); }
3、给定程序中fun函数的功能是:将s所指字符串中位于奇数位置的字符或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。
例如:字符串中的数据为:AABBCCDDEEFF,则应输出ABBCDDEFF。
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *s, char t[])
{ int i, j=0;
for(i=0; i<strlen(s); i )
/***********found**********/
if(i%2 && s[i]%2==0) 改为:if(i%2= =0 || s[i]%2= =0)
t[j ]=s[i];
/***********found**********/
t[i]='\0'; 改为:t[j]='\0';
}
main()
{ char s[N], t[N];
printf("\nPlease enter string s : "); gets(s);
fun(s, t);
printf("\nThe result is : %s\n",t);
}
4、给定程序中fun函数的功能是:计算n!。例如,给n输入5,则输出120.000000。
#include <stdio.h>
double fun ( int n )
{ double result = 1.0 ;
/************found************/
if n = = 0 改为:if (n == 0)
return 1.0 ;
while( n >1 && n < 170 )
/************found************/
result *= n-- 改为:result *= n--;
return result ;
}
main ( )
{ int n ;
printf("Input N:") ;
scanf("%d", &n) ;
printf("\n\n%d! =%lf\n\n", n, fun(n)) ;
}
5、给定程序中fun函数的功能是:先从键盘上输入一个3行3列的矩阵的各个元素的值,然后输出主对角线元素之和。
#include <stdio.h>
int fun()
{ int a[3][3],sum;
int i,j;
/*********found**********/
______; 改为:sum=0
for (i=0;i<3;i )
{ for (j=0;j<3;j )
/*********found**********/
scanf("%d",a[i][j]); 改为:scanf("%d",&a[i][j]);
}
for (i=0;i<3;i )
sum=sum a[i][i];
printf("Sum=%d\n",sum);
}
main()
{ fun(); }
6、给定程序中fun函数的功能是:求,(此处aa…aa表示n个a,a和n的值在1至9之间)。例如,a=3,n=6,则以上表达式为:
s=333333-33333-3333-333-33-3,其值是:296298。a和n是fun函数的形参。
#include <stdio.h>
long fun (int a, int n)
{ int j ;
/**************found**************/
long s = 0, t = 1 ; 改为:long s = 0, t = 0 ;
/**************found**************/
for ( j = 0 ; j <=n ; j ) 改为:for ( j = 0 ; j <n ; j )
t = t * 10 a ;
s = t ;
for ( j = 1 ; j < n ; j ) {
/**************found**************/
t = t % 10 ; 改为:t = t / 10 ;
s = s - t ; }
return(s) ;
}
main( )
{ int a, n ;
printf( "\nPlease enter a and n:") ;
scanf( "%d%d", &a, &n ) ;
printf( "The value of function is: %ld\n", fun ( a, n ) );
}
7、给定程序中fun函数的功能是:求k!(k<13),所求阶乘的值作为函数值返回。例如:若k=10,则应输出3628800。
#include <stdio.h>
long fun ( int k)
{
/************found************/
if k > 0 改为:if(k > 0)
return (k*fun(k-1));
/************found************/
else if ( k=0 ) 改为:else if ( k==0 )
return 1L;
}
main()
{ int k = 10 ;
printf("%d!=%ld\n", k, fun ( k )) ; }
8、给定程序中fun函数的功能是:由形参给定n个实数,输出平均值,并统计在平均值以上(含平均值)的实数个数。
例如,n=8时,输入:193.99,195.673,195.757,196.051,196.092,196.596,196.579,196.763,所得平均值为:195.838745,在平均值以上的实数个数应为:5。
#include <stdio.h>
int fun(float x[], int n)
{
/************found************/
int j, c=0, float xa=0.0; 改为:int j, c=0; float xa=0.0;
for (j=0; j<n; j )
xa = x[j]/n;
printf("ave =%f\n",xa);
for (j=0; j<n; j )
/************found************/
if (x[j] => xa) 改为:if (x[j] >= xa)
c ;
return c;
}
main ( )
{ float x[100] = { 193.199, 195.673, 195.757, 196.051,
196.092, 196.596, 196.579, 196.763 };
printf("%d\n", fun (x, 8)); }
9、给定程序中fun函数的功能是:将tt所指字符串中的小写字母都改为对应的大写字母,其它字符不变。例如:若输入“Ab,cD”,则输出“AB,CD”
#include <stdio.h>
#include <string.h>
char* fun( char tt[] )
{ int i;
for( i = 0; tt[i]; i )
/**********found***********/
if(( 'a' <= tt[i] )||( tt[i] <= 'z' ) ) 改为:if((tt[i] >= 'a')&&( tt[i] <= 'z' ) )
/**********found***********/
tt[i] = 32; 改为:tt[i] - = 32;
return( tt );
}
main( )
{ char tt[81];
printf( "\nPlease enter a string: " );
gets( tt );
printf( "\nThe result string is:\n%s", fun( tt ) ); }
10、给定程序中fun函数的功能是:在p所指字符串中找出ASCII码值最大的字符,将其放在第一个位置上;并将该字符前的原字符向后顺序移动。例如,调用fun函数这前给字符串输入:ABCDeFGH,调用后字符串中的内容为:eABCDFGH。
#include <stdio.h>
/**********found**********/
fun( char *p ) 改为:void fun( char *p )
{ char max,*q; int i=0;
max=p[i];
while( p[i]!=0 )
{ if( max<p[i] )
{ max=p[i];
/**********found**********/
q=p I 改为:q=p i;
}
i ; }
/**********found**********/
wihle( q>p ) 改为:while(q>p )
{ *q=*(q-1);
q--; }
p[0]=max;
}
main()
{ char str[80];
printf("Enter a string: "); gets(str);
printf("\nThe original string: "); puts(str);
fun(str);
printf("\nThe string after moving: "); puts(str); printf("\n\n"); }


