例7.1
# include
int main()
{
void print-star();
void print-message();
print-star();
print-message();
print-star();
return 0;
}
void print-star()
{
printf("*********************\n");
}
void print-message()
{
printf("How do you do !\n");
}
例7.2
int max(int x,int y)
{
int z;
z=x>y? x:y;
return(z);
}
# include
int main()
{
int max(int x,int y);
int a,b,c;
printf("please enter two interger numbers:");
scanf("%d,%d",&a,&b);
c=max(a,b)
printf("max is %d\n",c);
return 0;
}
例7.3
# include
int main()
{
int max(float x,float y);
float a,b;
int c;
scanf("%f,%f",&a,&b);
c=max(a,b);
printf("max is %d\n",c);
return 0;
}
int max(float a,float y)
{
float z;
z=x>y? x:y;
return (z);
}
例7.4 输入两个实数,用一个函数求它们的和。
# include
int main()
{
float add(float x,float y);
float a,b,c;
printf("Please enter a and b:");
scanf("%f,%f",&a,&b);
c=add(a,b);
printf("sum is %f\n",c);
return 0;
}
float add(float x,float y)
{
float z;
z=x+y;
return(z);
}
例7.5输入四个数,找出其中最大的数
# include
int main()
{
int max4(int a,int b,int c,int d);
int a,b,c,d,max;
printf("Please enter 4 interger numbers:");
scanf("%f %f %f %f",&a,&b,&c,&d);
max=max4(a,b,c,d);
printf("max=%d\n",max);
return 0;
}
int max4(int a,int b,int c,int d)
{
int max2(int a,int b);
int m;
m=max2(a,b);
m=max2(m,c);
m=max2(m,d);
return(m);
}
int max2(int a,int b)
{
if(a>=b)
return a;
else
return b;
}
例7-6 y用递归方法求n!
# include
int main()
{
int fac(int n);
int n;
int y;
printf("input an interger number:");
scanf("%d",&n);
y=fac(n);
printf("%d!=%d\n",n,y);
return 0;
}
int fac(int n)
{
int f;
if(n<0)
printf("n<0,date error!");
else if(n==0||n==1)
f=1;
else
f=fac(n-1)*n;
return(f);
}
例7.7分别用两个函数实现两个操作
# include
int main()
{
void hanoi(int n,char one, char two,char three);
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to move %d diskes:\n",m);
hanoi(m,'A','B','C');
}
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,two,three);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c->%c\n",x,y);
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20190221091829518.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU3NjI0MQ==,size_16,color_FFFFFF,t_70)
## 例7.8
```c
# include
int main()
{
int max(int x,int y);
int a[10],m,n,i;
printf("enter 10 interger numbers:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(i=1,m=a[0],n=0;i<10;i++)
{
if (max(m,a[i])>m)
{
m=max(m,a[i]);
n=i;
}
}
printf("The largest number is %d\nit is the %dth number.\n",m,n+1);
}
int max(int x,int y)
{
return(x>y? x:y);
}
例7.8
# include
int main()
{
float average(float array[10]);
float score[10],aver;
int i;
printf("input 10 scores:\n");
for(i=0;i<10;i++)
scanf("%f",&score[i]);
printf("\n");
aver=average(sorce);
printf("average score is %5.2f\n",aver);
return 0;
}
float average(flaot array[10])
{
int i;
float aver,sum=array[0];
for(i=1;i<10;i++)
sum=sum+array[i];
aver=sum/10;
return(aver);
}
例7.9
# include
int main()
{
float average(float array[ ],int n);
float score1[5]={98,5,97,91,5,60,55};
float score2[10]={67,5,89,5,99,69,5,77,89,5,76,5,54,60,99,5};
printf("The average of class A is %6.2f\n",average(score1,5));
return 0;
}
float average(float array[ ],int n)
{
int i;
float aver,sum=array[0];
for(i=1;i<n;i++)
sum=sum+array[i];
aver=sum/n;
return (aver);
}
例7.9
# include
int main()
{
int max_value(int array[ ][4]);
int a[3][4]={{1,3,5,7},{2,4,6,8},{15,17,34,12}};
printf("Max value is %d\n",max_value(a));
return 0;
}
int max_value(int array[ ][4])
{
int i,j,max;
max=array[0][0];
for(i=0;i<3;i++)
for(j=0;j<4;j++)
if(array[i][y]>max)
max=array[i][y];
return(max);
}
例7.10
# include
int a=3,b=5;
int main()
{
int max(int a,int b);
int a=8;
printf("max=%d\n",max(a,b));
return 0;
}
int max(int a,int b)
{
int c;
c=a>b? a:b;
return(c);
}
例7.11
# include
int main()
{
int f(int);
int a,i;
for(i=0;i<3;i++)
printf("%d\n",f(a));
return 0;
}
int f(int a)
{
auto int b=0;
static c=3;
b=b+1;
c=c+1;
return (a+b+c)
}
例7.12
# include
int main()
{
int fac(int n);
int i;
for(i=1;i<=5;i++)
printf("%d!=%d\n",i,fac(i));
return 0;
}
int fac(int n)
{
static int f=1;
f=f*n;
return(f);
}
例7.13
# include
int main()
{
int max();
entern int A,B,C;
printf("Please enter three interger numbers:");
scanf("%d %d %d",&A,&B,&C);
printf("max is %d\n",max());
return 0;
}
int A,B,C;
int max()
{
int m;
m=A>B?A:B;
if(C>m)
m=C;
return(m);
}