49、给定程序中fun函数的功能:用选择法对数组中的n个元素按从小到大的顺序进行排序。
#include <stdio.h>
#define N 20
void fun(int a[], int n)
{ int i, j, t, p;
for (j = 0 ;j < n-1 ;j ) {
/************found************/
p = j 改为:p = j;
for (i = j;i < n; i )
if(a[i] < a[p])
/************found************/
p = j; 改为:p = i;
t = a[p] ; a[p] = a[j] ; a[j] = t; }
}
main( )
{ int a[N]={9,6,8,3,-1},i, m = 5;
printf("排序前的数据:") ;
for(i = 0;i < m;i ) printf("%d ",a[i]); printf("\n");
fun(a,m);
printf("排序后的数据:") ;
for(i = 0;i < m;i ) printf("%d ",a[i]); printf("\n"); }
50、给定程序中fun函数的功能是:计算函数F(x,y,z)=(x y)/(x-y) (z y)/(z-y)的值。其中x和y的值不等,z和y的值不等。例如,当x的值为9、y的值为11、z的值为15时,函数值为-3.50。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/************found************/
#define FU(m,n) (m/n) 改为:#define FU(m,n) (m)/(n)
float fun(float a,float b,float c)
{ float value;
value=FU(a b,a-b) FU(c b,c-b);
/************found************/
Return(Value); 改为:return(value);
}
main( )
{ float x,y,z,sum;
printf("Input x y z: ");
scanf("%f%f%f",&x,&y,&z);
printf("x=%f,y=%f,z=%f\n",x,y,z);
if (x==y||y==z){printf("Data error!\n");exit(0);}
sum=fun(x,y,z);
printf("The result is : %5.2f\n",sum); }
51、给定程序中fun函数的功能是:根据形参m的值,计算如下公式的值:
例如,若输入5,则应输出2.283333。
#include <stdio.h>
double fun( int m )
{ double t = 1.0;
int i;
for( i = 2; i <= m; i )
/**********found**********/
t = 1.0/k; 改为:t = 1.0/i;
/**********found**********/
________ 改为:return t;
}
main( )
{ int m;
printf( "\nPlease enter 1 integer number:" );
scanf( "%d", &m );
printf( "\nThe result is %lf\n", fun( m ) ); }
52、给定程序中fun函数的功能是:用递归算法计算裴波拉契数列中第n项的值,从第1项起,裴波拉契数列为:1、1、2、3、5、8、13、21……
例如,若给n输入7,则裴波拉契数值为13。
#include <stdio.h>
long fun(int g)
{
/**********found**********/
switch(g); 改为:switch(g):
{ case 0: return 0;
/**********found**********/
case 1 ;case 2 : return 1 ; 改为:case 1 :case 2 : return 1 ;
}
return( fun(g-1) fun(g-2) );
}
main( )
{ long fib; int n;
printf("Input n: "); scanf("%d",&n); printf("n = %d\n",n);
fib=fun(n);
printf("fib = %d\n\n",fib); }
53、给定程序中fun函数的功能是:用冒泡法对6个字符串按由小到大顺序进行排列。
#include <stdio.h>
#include <string.h>
#define MAXLINE 20
/**************found**************/
fun ( char *pstr[6]) 改为:void fun ( char *pstr[6])
{ int i, j ;
char *p ;
for (i = 0 ; i < 5 ; i ) {
/**************found**************/
for (j = i 1, j < 6, j ) 改为:for (j = i 1;j < 6;j )
{
/**************found**************/
if(strcmp(*(pstr i), (pstr j)) > 0) 改为:if(strcmp(*(pstr i), *(pstr j)) > 0)
{ p = *(pstr i) ;
/**************found**************/
*(pstr i) = pstr j ; 改为:*(pstr i) = *(pstr j) ;
*(pstr j) = p ; }
}
}
}
main( )
{ int i ;
char *pstr[6], str[6][MAXLINE] ;
for(i = 0; i < 6 ; i ) pstr[i] = str[i] ;
printf( "\nEnter 6 string(1 string at each line): \n" ) ;
for(i = 0 ; i < 6 ; i ) scanf("%s", pstr[i]) ;
fun(pstr) ;
printf("The strings after sorting:\n") ;
for(i = 0 ; i < 6 ; i ) printf("%s\n", pstr[i]) ; }
54、给定程序中fun函数的功能是:将m(1<=m<=10)个字符串连接起来,组成一个新串,放入pt所指存储区中。例如,把3个串“abc”,“CD”,“EF”连接起来,结果是“abcCDEF”。
#include <stdio.h>
#include <string.h>
/************found************/
int fun ( char str[][10], int m, char *pt ) 改为:void fun ( char str[][10], int m,char *pt )
{
/************found************/
Int k, q, i ; 改为:int k, q, i ;
for ( k = 0; k < m; k )
{ q = strlen ( str [k] );
for (i=0; i<q; i )
/************found************/
pt[i] = str[k,i] ; 改为:pt[i] = str[k][i] ;
pt = q ; pt[0] = 0 ; }
}
main( )
{ int m, h ;
char s[10][10], p[120] ;
printf( "\nPlease enter m:" ) ;
scanf("%d", &m) ; gets(s[0]) ;
printf( "\nPlease enter %d string:\n", m ) ;
for ( h = 0; h < m; h ) gets( s[h]) ;
fun(s, m, p) ;
printf( "\nThe result is : %s\n", p) ; }
55、已知一个数列从第0项开始的前三项分别为0、0、1,以后各项都是其相邻前三项之和。给定程序中fun函数的功能是:计算并输出该数列前n项的平方根之和,n的值通过形参传入。例如,当n=10时,输出结果为23.197745。
#include <stdio.h>
#include <math.h>
/************found************/
fun(int n) 改为:double fun(int n)
{ double sum, s0, s1, s2, s; int k;
sum = 1.0;
if (n <= 2) sum = 0.0;
s0 = 0.0; s1 = 0.0; s2 = 1.0;
for (k = 4; k <= n; k )
{ s = s0 s1 s2;
sum = sqrt(s);
s0 = s1; s1 = s2; s2 = s; }
/************found************/
return sum 改为:return sum;
}
main ( )
{ int n;
printf("Input N=");
scanf("%d", &n);
printf("%f\n", fun(n) ); }
56、给定程序中fun函数的功能是:求两个非零正整数的最大公约数,并作为函数值返回。例如,num1和num2分别输入49和21,则输出最大公约数为7;若给num1和num2分别输入27和81,则输出最大公约数为27。
#include <stdio.h>
int fun(int a,int b)
{ int r,t;
if(a<b) {
/************found************/
t=a; b=a; a=t; 改为:t=a; a=b; a=t;
}
r=a%b;
while(r!=0)
{ a=b; b=r; r=a%b; }
/************found************/
return(a); 改为:return(b);
}
main( )
{ int num1, num2,a;
printf("Input num1 num2: "); scanf("%d%d",&num1,&num2);
printf("num1= %d num2= %d\n\n",num1,num2);
a=fun(num1,num2);
printf("The maximun common divisor is %d\n\n",a); }
57、给定程序中creatlink函数的功能是:创建带头节点的单向链表,并为各节点数值域赋0到m-1的值。
#include <stdio.h>
#include <stdlib.h>
typedef struct aa
{ int data;
struct aa *next;
} NODE;
NODE *Creatlink(int n, int m)
{ NODE *h=NULL, *p, *s;
int i;
/**********found***********/
p=(NODE )malloc(sizeof(NODE)); 改为:p=(NODE *)malloc(sizeof(NODE));
h=p;
/**********found***********/
p->next=NULL; 改为:p=s;
for(i=1; i<=n; i )
{ s=(NODE *)malloc(sizeof(NODE));
/**********found***********/
s->data=rand()%m; 改为:s->data=rand()%(m-1);
s->next=p->next;
p->next=s;
p=p->next;
}
/**********found***********/
return p; 改为:return h;
}
outlink(NODE *h)
{ NODE *p;
p=h->next;
printf("\n\nTHE LIST :\n\n HEAD ");
while(p)
{ printf("->%d ",p->data);
p=p->next; }
printf("\n");
}
main( )
{ NODE *head;
head=Creatlink(8,22);
outlink(head); }
58、给定程序中fun函数的功能是:计算整数n的阶乘。
#include <stdio.h>
double fun(int n)
{ double result=1.0;
while (n>1 && n<170)
/*********found*********/
result*=--n; 改为:result*=n--;
/*********found*********/
return _____; 改为:return result;
}
main( )
{ int n;
printf("Enter an integer: ");
scanf("%d",&n);
printf("\n\n%d!=%lg\n\n",n,fun(n)); }
59、给定程序中fun函数的功能是:删除p所指字符串中的所有空白字符(包括Tab、回车符及换行符)。输入字符串时用‘#’结束输入。
#include <string.h>
#include <stdio.h>
#include <ctype.h>
fun ( char *p)
{ int i,t; char c[80];
/************found************/
For (i = 0,t = 0; p[i] ; i ) 改为:for (i = 0,t = 0; p[i] ; i )
if(!isspace(*(p i))) c[t ]=p[i];
/************found************/
c[t]="\0"; 改为:c[t]=’\0’;
strcpy(p,c);
}
main( )
{ char c,s[80];
int i=0;
printf("Input a string:"); c=getchar();
while(c!='#')
{ s[i]=c;i ;c=getchar(); }
s[i]='\0'; fun(s); puts(s); }