文档详情

【计算机等级考试】二级上机改错题100题.doc

发布:2017-04-19约6.31万字共118页下载文档
文本预览下载声明
改 错 题 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; id; i++) t[i] = s[i]; for (i = 0; id; 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; istrlen(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;i3;i++)
显示全部
相似文档