没有合适的资源?快使用搜索试试~ 我知道了~
C语言程序改错题.doc
1 下载量 15 浏览量
2023-07-05
13:05:49
上传
评论
收藏 48KB DOC 举报
温馨提示
试读
16页
C语言程序改错题.doc
资源推荐
资源详情
资源评论
.
实用文档.
第 1 题 【程序改错】
功能:先将在字符串 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;
sl = strlen(s);
for (i=0; i<sl; i++)
/**********FOUND**********/
t[i] = s[sl-i];
for (i=0; i<sl; i++)
t[sl+i] = s[i];
/**********FOUND**********/
t[2*sl] = "0";
}
main()
{
char s[100], t[100];
printf("Please enter string s:"); scanf("%s", s);
fun(s, t);
printf("The result is: %s", t);
}
答案:
1). int i,sl;
2). t[i] = s[sl-i-1];
3). t[2*sl] = ' '; 或 t[2*sl] = 0;
第 2 题 【程序改错】
功能:求出以下分数序列的前 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 )
{
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 <stdio.h>
.
实用文档.
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 )
{
int i;
for( i = 1; i <= n; i++, x++ ) *x = ( int )( 50*rnd() );
}
putarr( int n, int *z )
{
int i;
for( i = 1; i <= n; i++, z++ )
{
printf( "%4d", *z );
if ( !( i%10 ) ) printf( "\n" );
}
printf("\n");
}
main()
剩余15页未读,继续阅读
资源评论
yyyyyyhhh222
- 粉丝: 403
- 资源: 6万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功