统计各年龄段的人数。N个年龄通过调用随机函数获得,并放在主函数的age数组中;要求函数把0至9岁年龄段的人数放在d[0]中,吧10~19段的人数 放在d[1]中,……,把100岁(含100岁)以上年龄段人数都放在d[10]中。结果在主函数中输出。
void fun(int *a ,int *b )
{
int i;
for (i=0 ;i<10 ;i++)
b[i]=0 ;
for (i=0 ;i<50 ;i++)
{
if (a[i]>=0 &&a[i]<=9 ) b[0 ]++;
else if (a[i]>=10 &&a[i]<=19 ) b[1 ]++;
else if (a[i]>=20 &&a[i]<=29 ) b[2 ]++;
else if (a[i]>=30 &&a[i]<=39 ) b[3 ]++;
else if (a[i]>=40 &&a[i]<=49 ) b[4 ]++;
else if (a[i]>=50 &&a[i]<=59 ) b[5 ]++;
else if (a[i]>=60 &&a[i]<=69 ) b[6 ]++;
else if (a[i]>=70 &&a[i]<=79 ) b[7 ]++;
else if (a[i]>=80 &&a[i]<=89 ) b[8 ]++;
else if (a[i]>=90 &&a[i]<=99 ) b[9 ]++;
else if (a[i]>=100 ) b[10 ]++;
}
}
double rnd()
{
static int t=29 ,c=217 ,m =1024 ,r=0 ;
r=(r*t +c)%m ;
return ((double)r/m );
}
int main()
{
int age[N],i,d[M];
for (i=0 ;i<N;i++) age[i]=(int )(115 *rnd ());
printf ("the original data:\n" );
for (i=0 ;i<N;i++)
printf ("%4d :%4d \n" ,(i+1 )%51 ,age[i]);
printf ("\n\n" );
fun(age,d);
for (i=0 ;i<10 ;i++) printf ("%4d ---%4d :%4d \n" ,i*10 ,i*10 +9 ,d[i]);
printf ("over 100:%4d \n" ,d[10 ]);
return 0 ;
}