求数组中的最值、求和、求平均值
#include<stdio.h>
int max(int a[],int i,int j)
{
int t1,t2;
if(i==j) return a[i];
else
{
t1=a[i];
t2=max(a,i+1,j);
return t1>t2?t1:t2;
}
}
float aver(int a[],int i,int j)
{
if(i==j) return a[i];
else
{
return (a[i]+aver(a,i+1,j)*(j-i))/(j-i+1);
}
}
int sum(int a[],int i,int j)
{
if(i==j) return a[i];
else
{
return a[i]+sum(a,i+1,j);
}
}
void main()
{
int a[8]={1,2,3,4,45,6,7,8},m,s;
float average;
m=max(a,0,7);
average=aver(a,0,7);
s=sum(a,0,7);
printf("max=%d ,average=%.2f ,sum=%d ",m,average,s);
}