1.给定程序功能是计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…+f(n)的值。
float f( double x)
{
if (x == 0.0 || x == 2.0)
return ___1___;
else if (x < 0.0)
return (x -1)/(x-2);
else
return (x +1)/(x-2);
}
double fun( int n )
{int i; double s=0.0, y;
for (i= -n; i<=___2___; i++)
{ y=f(1.0*i); s += y; }
return ___3___;
}
main ( )
{
printf("%f\n", fun(5) );
}
2.给定程序的功能是求二分之一的圆面积,函数通过形参得到圆的半径,函数返回二分之一的圆面积。
#include
float fun ( float ___1___ )
{
return 3.14159 * ___2___ /2.0;
}
main ( )
{float x;
printf ( "Enter x: ");
scanf ( "%f", ___3___ );
printf (" s = %f\n ", fun ( x ) );
}
3.求K!(K<13),并通过函数名传回主函数。
#include <stdio.h>
long fun ( int k)
{
/************found************/
if (k __1__0)
/************found************/
return (k*fun(__2__));
/************found************/
else if ( k __3__ 0 )
return 1L;
}
main()
{ int k = 10 ;
printf("%d!=%ld\n", k, fun ( k )) ;
}
4.给定程序的功能是计算score中m个人的平均成绩aver,将低于aver的成绩放在below中,通过函数名返回人数。
例如,当score={10,20,30,40,50,60,70,80,90},m=9时,函数返回的人数应该是4,below={10,20,30,40}。
#include <stdio.h>
#include <string.h>
int fun(int score[], int m, int below[])
{
int i, j = 0 ;
float aver = 0.0 ;
for(i = 0 ; i < m ; i++) aver += score[i] ;
aver /= (float) m ;
for(i = 0 ; i < m ; i++)
/**************found**************/
if(score[i] < aver) below[j++] = __1__;
return j ;
}
main()
{ int i, n, below[9] ;
int score[9] = {10, 20, 30, 40, 50, 60, 70, 80, 90} ;
/**************found**************/
n = fun(score, 9, __2__) ;
printf( "\nBelow the average score are: " ) ;
/**************found**************/
for (i = 0 ; i < n ; i++) printf("%d ", __3__) ;
}
5. 下列程序的功能是:找出所有100以内(含100)满足i,i+4,i+10都是素数的整数i(i+10也在100以内)的个数cnt以及这些i之和sum。
#include <stdio.h>
int cnt,sum;
int isPrime(int number)
{
int i,tag=1;
if(number==1) return 0;
for(i=2;tag&&i<=number/2;i++)
/***************found***************/
if(__1__) tag=0;
return tag;
}
void countValue()
{ int I,count=0,xx[30];
int j,k,m;
cnt=0;
sum=0;
/***************found***************/
for(I=1;__2__;I++)
if(isPrime(I)) {xx[count]=I;count++;}
for(I=0;I<count;I++)
if (isPrime(xx[I]+4)&&isPrime(xx[I]+10))
/***************found***************/
{__3__;}
}
void main()
{
cnt=sum=0;
countValue();
printf("cnt=%d\n",cnt);
printf("sum=%d\n",sum);
}
6.用二分法求方程的一个根,并要求绝对误差不超过0.001。
函数fIm和funx的功能是:用二分法求方程
的一个根,并要求绝对误差不超过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************/
{ double r;
r=(m+n)/2;
/************found************/
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);
}
7.下列给定程序中,函数fun的功能是:按以下递归公式求函数的值。
例如,当给n输入5时,函数值为18;当给n输入3时,函数值为14。请改正程序中的错误,使它能得出正确的结果。
#include <stdio.h>
/************found************/
int fun ( n )
{ int c;
/************found************/
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));
}
8.给定程序MODll.C中函数fun的功能是:根据输入的三个边长(整型值),判断能否构成三角形:构成的是等边三角形,还是等腰三角形。若能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成一般三角形函数返回1,若不能构成三角形函数返回0。 请改正函数fun中指定部位的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <math.h>
/**************found**************/
int fun(int a,int b,int c)
{ if(a+b>c && b+c>a && a+c>b) {
if(a==b && b==c)
return 3;
else if(a==b||b==c||a==c)
return 2;
/**************found**************/
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);
}
9.给定程序MODI1.C中函数fun的功能是:计算函数(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>
/************FOUND************/
#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);
}
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);
}
10. 判断两个整数m和n是否互质(即是否有公共的因子)(m≠1,n≠1)。方法是:用2到t(t取m和n中较小的那个数)之间的数分别去除m和n,若m和n能同时被某个数除尽,则m和n不互质;否则它们互质。例如:若输入187和85,则应输出No(表示它们不互质,它们有公因子17)。若输入89和187,则应输出Yes(表示它们互质)。
#include <conio.h>
#include <stdio.h>
int IsThat( int m, int n )
{
int k, t, mk=1;
t=m;
if (m>n) t=n;
/************FOUND**********/
for( k=2; k<=t; k++ )
if( m%k==0 && n%k==0 ){ mk=0; break; }
return( mk );
}
main( )
{
int m, n;
printf( "\nPlease enter 2 numbers:\n" );
/************FOUND**********/
scanf( "%d %d", &m, &n );
if( IsThat( m, n ) )
printf( "Yes\n" );
else
printf( "No\n" );
}