PAGE 16
第1题 【程序改错】
功能:先将在字符串s中的字符按逆序存放到t串中,然后把s中的
字符按正序连接到t串的后面。
例如:当s中的字符串为:“ABCDE”时,则t中的字符串应为:
“EDCBAABCDE”。
*/
#include
#include
#include
void fun (char *s, char *t)
{
/**********FOUND**********/
int i;
sl = strlen(s);
for (i=0; i
/**********FOUND**********/
t[i] = s[sl-i];
for (i=0; i
t[sl+i] = s[i];
/**********FOUND**********/
t[2*sl] = "0";
}
main()
{
char s[100], t[100];
printf("\nPlease enter string s:"); scanf("%s", s);
fun(s, t);
printf("The result is: %s\n", t);
}
答案:
1). int i,sl;
2). t[i] = s[sl-i-1];
3). t[2*sl] = '\0'; 或 t[2*sl] = 0;
第2题 【程序改错】
功能:求出以下分数序列的前n项之和。和值通过函数值返回main
函数。
2/1+3/2+5/3+8/5+13/8+21/13 ……
例如:若n = 5,则应输出:8.391667。
*/
#include
#include
/**********FOUND**********/
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;
c = a;
a = a + b;
b = c;
}
/**********FOUND**********/
return c;
}
main( )
{
int n = 5;
printf( "\nThe value of function is: %lf\n", fun ( n ) );
}
答案:
1). double fun(int n)
2). s = s + (double)a / b; 或 s += (double)a / b; 或 s += a /(double)b; 或 s=s+a/(double)b;
3). return s;
第3题 【程序改错】
功能:读入一个整数m( 5≤m≤20 ),函数getarr调用函数rnd获
得m个随机整数,函数sortpb将这m个随机整数从小到大排序。
例如:若输入整数7,则应输出:3 10 17 28 32 36 47。
*/
#include "conio.h"
#include
sortpb ( int n, int *a )
{
/**********FOUND**********/
int i, j, p, t
for ( j = 0; j < n-1 ; j++ )
{
p = j;
for ( i = j + 1; i < n ; i ++ )
/**********FOUND**********/
if ( a[p] > a[j] ) p = i;
/**********FOUND**********/
if ( p == j )
{
t = a[j];
a[j] = a[p];
a[p] = t;
}
}
}
double rnd ( )
{
static t = 29, c = 217, m = 1024, r = 0;
r =( r*t + c )%m; return( ( double )r/m );
}
getarr( int n, int *x )
{
i