改错题
1、给定程序中fun函数的功能是:根据整型形参m的值,计算如下公式的值:
例如,若m中的值为5,。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <>
double fun ( int m )
{ double y = ;
int i ;
/**************found**************/
for(i = 2 ; i < m ; i++) 改为:for(i = 2 ; i <= m ; i++)
/**************found**************/
y -= 1 /(i * i) ; 改为:y -= /(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 <>
#include <>
/************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位中)。
例如:字符串中的数据为:DDEEFF,则应输出ABBCDDEFF。
#include <>
#include <>
#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,。
#include <>
double fun ( int n )
{ double result = ;
/************found************/
if n = = 0 改为:if (n == 0)
return ;
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列的矩阵的各个元素
计算机国家二级---改 错 题 来自淘豆网www.taodocs.com转载请标明出处.