40、假定整数数列中的数不重复,并存放在数组中。给定程序中fun函数的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。
#include <stdio.h>
#define N 20
fun(int *a,int n,int x)
{ int p=0,i;
a[n]=x;
while( x!=a[p] )
p=p 1;
/**********found**********/
if(P==n) return -1; 改为:if(p==n) return -1;
else
{ for(i=p;i<n;i )
/**********found**********/
a[i 1]=a[i]; 改为:a[i]=a [i 1];
return n-1; }
}
main( )
{ int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;
n=10;
printf("The original data :\n");
for(i=0;i<n;i ) printf("%5d",w[i]);
printf("\nInput x (to delete): "); scanf("%d",&x);
printf("Delete : %d\n",x);
n=fun(w,n,x);
if ( n==-1 ) printf("***Not be found!***\n\n");
else
{ printf("The data after deleted:\n");
for(i=0;i<n;i ) printf("%5d",w[i]);printf("\n\n"); }
}
41、给定程序中fun函数的功能是:计算并输出下列级数的前N项之和直到大于q为止,q的值通过形参传入。
例如,若q的值为50.0,则函数值为49.394948。
#include <stdio.h>
double fun( double q )
{ int n; double s,t;
n = 2;
s = 2.0;
while (s<=q)
{ t=s;
/************found************/
s=s (n 1)/n; 改为:s=s (double)(n 1)/n;
n ; }
printf("n=%d\n",n);
/************found************/
return s; 改为:return t;
}
main ( )
{ printf("%f\n", fun(50)); }
42、给定程序中fun函数的功能是:根据输入的三个边长(整型值),判断能束构成三角形,构成的是等边三解形还是等腰三角形。若能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成一般三角形函数返回1,若不能构成三角形函数返回0。
#include <stdio.h>
#include <math.h>
/**************found**************/
void fun(int a,int b,int c) 改为:int fun(int a,int b,int c)
{ if(a b>c && b c>a && a c>b) {
if(a==b && b==c)
/**************found**************/
return 1; 改为:return 3;
else if(a==b||b==c||a==c)
return 2;
/**************found**************/
else retrun 1 改为:else return 1;
}
else return 0;
}
main()
{ int a,b,c,shape;
printf("\nInput a,b,c: "); scanf("%d%d%d",&a,&b,&c);
printf("\na=%d, b=%d, c=%d\n",a,b,c);
shape =fun(a,b,c);
printf("\n\nThe shape : %d\n",shape); }
43、给定程序中fun函数的功能是:计算n的5次方的值(规定n的值大于2小于8),通过形参指针传回主函数,并计算该值的个位、十位、百位上数字之和作为函数值返回。例如,7的5次方是16807,其低3位数的和值是15。
#include <stdio.h>
#include <math.h>
int fun( int n ,int *value )
{ int d,s,i;
/**************found**************/
d=0; s=0; 改为:d=1; s=0;
for(i=1; i<=5; i ) d=d*n;
*value=d;
for(i=1; i<=3; i )
{ s=s d%10;
/**************found**************/
s=s\10; 改为:d=d/10;
}
return s;
}
main( )
{ int n, sum, v;
do
{ printf("\nEnter n( 2<n<8): ");scanf("%d",&n); }
while(n<=2||n>=8);
sum=fun( n,&v );
printf("\n\nThe result:\n value=%d sum=%d\n\n",v,sum); }
44、给定程序中fun函数的功能是:判断一个整数是否是素数,若是返回1,否则返回0。在main( )函数中,若fun返回1输出YES,若返回0则输出NO!。
#include <stdio.h>
int fun ( int m )
{ int k = 2;
while ( k <= m && (m%k))
/************found************/
k 改为:k ;
/************found************/
if (m = k ) 改为:if (m == k )
return 1;
else return 0;
}
main( )
{ int n;
printf( "\nPlease enter n: " ); scanf( "%d", &n );
if ( fun ( n ) ) printf( "YES\n" );
else printf( "NO!\n" ); }
45、给定程序中fun函数的功能是:判断ch中的字符是否与str所指串中的某个字符相同;若相同,什么也不做,若不同,则将其插在串的最后。
#include <stdio.h>
#include <string.h>
/**********found**********/
void fun(char str, char ch ) 改为:void fun(char * str, char ch )
{ while ( *str && *str != ch ) str ;
/**********found**********/
if ( *str == ch ) 改为:if ( *str == ‘\0’ )
{ str [ 0 ] = ch;
/**********found**********/
str[1] = '0'; 改为:str[1] = '\0';
}
}
main( )
{ char s[81], c ;
printf( "\nPlease enter a string:\n" ); gets ( s );
printf ("\n Please enter the character to search : " );
c = getchar(); fun(s, c) ;
printf( "\nThe result is %s\n", s); }
46、给定程序中fun函数的功能是:按顺序给s所指数组中的元素赋予从2开始的偶数,然后再按顺序对每五个元素求一个平均值,并针这些值依次存放在w所指的数组中。若s所指数组中元素的个数不是5的倍数,多余部分忽略不计。例如,s所指数组在14个元素,则只对前10个元素进行处理,不对最后的4个元素求平均值。
#include <stdio.h>
#define SIZE 20
fun(double *s, double *w)
{ int k,i; double sum;
for(k=2,i=0;i<SIZE;i )
{ s[i]=k; k =2; }
/**********found**********/
sun=0.0; 改为:sum=0.0;
for(k=0,i=0;i<SIZE;i )
{ sum =s[i];
/**********found**********/
if(i 1%5==0) 改为:if((i 1)%5==0)
{ w[k]=sum/5; sum=0; k ; }
}
return k;
}
main( )
{ double a[SIZE],b[SIZE/5];
int i, k;
k = fun(a,b);
printf("The original data:\n");
for(i=0; i<SIZE; i )
{ if(i%5==0) printf("\n");
printf("%4.0f", a[i]); }
printf("\n\nThe result :\n");
for(i=0; i<k; i ) printf("%6.2f ",b[i]);
printf("\n\n"); }
47、给定程序中fun函数的功能是:按以下递归公式求函数值。
例如,当给n输入5时,函数值为18;当给n输入3时,函数值为14。
#include <stdio.h>
/************found************/
fun ( n ) 改为:int fun ( int n )
{ int c;
/************found************/
if(n=1) 改为:if(n==1)
c = 10 ;
else c= fun(n-1) 2;
return(c);
}
main( )
{ int n;
printf("Enter n : "); scanf("%d",&n);
printf("The result : %d\n\n", fun(n)); }
48、给定程序中fun函数的功能是:用二分法求方程的一个根,并要求绝对误差不超过0.001。例如,若给m输入-100,给n输入90,则函数求得一个根值为2.000。
#include <stdio.h>
#include <math.h>
double funx(double x)
{ return(2*x*x*x-4*x*x 3*x-6); }
double fun( double m, double n)
{
/************found************/
int r; 改为:double r;
r=(m n)/2;
/************found************/
while(fabs(n-m)<0.001) 改为:while(fabs(n-m)>0.001)
{ if(funx(r)*funx(n)<0) m=r;
else n=r;
r=(m n)/2; }
return r;
}
main( )
{ double m,n, root;
printf("Enter m n : \n"); scanf("%lf%lf",&m,&n);
root=fun( m,n );
printf("root = %6.3f\n",root); }


