|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/*随机数产生年龄,统计各个年龄段人数,大于100岁的人数放在b[10],0-9放在b[0],10-19放在b[1],类推*/
#include <stdio.h>
# define N 50
# define M 11
void fun(int *a, int *b)
{
int c[M],i;
for(i=0;i<N;i++)
{
if( 0 <= a[i] <= 9 ) (c[0])++; //年龄大于0小于9,c[0]++
if( 10 <= a[i] <= 19 ) (c[1])++; //同上
if( 20 <= a[i] <= 29 ) (c[2])++;
if( 30 <= a[i] <= 39 ) (c[3])++;
if( 40 <= a[i] <= 49 ) (c[4])++;
if( 50 <= a[i] <= 59 ) (c[5])++;
if( 60 <= a[i] <= 69 ) (c[6])++;
if( 70 <= a[i] <= 79 ) (c[7])++;
if( 80 <= a[i] <= 89 ) (c[8])++;
if( 90 <= a[i] <= 99 ) (c[9])++;
if( 100 <= a[i] ) (c[10])++;
}
for(i=0;i<M;i++)
{
b[i] = c[i];
}
}
double rnd()
{
static int t=29,c=217,m=1024,r=0;
r=(r*t+c)%m;
return((double)r/m);
}
void main()
{
FILE *wf;
int age[N], i,d[M];
int b[N]={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i++)
age[i]=(int)(115*rnd()); /*产生一个随机的年龄数组*/
printf("The original data :\n");
for(i=0; i<N; i++)
printf((i+1)%10==0? "%4d\n":"%4d",age[i]); /*每行输出10个数*/
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]);
/******************************/
wf=fopen("out.dat","w");
fun(b,d);
for(i=0; i<10; i++)
fprintf(wf,"%4d---%4d :%4d\n", i*10, i*10+9,d[i]);
fprintf(wf,"Over 100 : %4d",d[10]);
fclose(wf);
/*****************************/
}
运行结果:
本帖最后由 永恒的蓝色梦想 于 2020-4-13 15:22 编辑
问题出在12-21行 /*随机数产生年龄,统计各个年龄段人数,大于100岁的人数放在b[10],0-9放在b[0],10-19放在b[1],类推*/
#include <stdio.h>
# define N 50
# define M 11
void fun(int *a, int *b)
{
int c[M],i;
for(i=0;i<N;i++)
{
if( 0 <= a[i] && a[i] <= 9 ) (c[0])++; //年龄大于0小于9,c[0]++
if( 10 <= a[i] && a[i] <= 19 ) (c[1])++; //同上
if( 20 <= a[i] && a[i] <= 29 ) (c[2])++;
if( 30 <= a[i] && a[i] <= 39 ) (c[3])++;
if( 40 <= a[i] && a[i] <= 49 ) (c[4])++;
if( 50 <= a[i] && a[i] <= 59 ) (c[5])++;
if( 60 <= a[i] && a[i] <= 69 ) (c[6])++;
if( 70 <= a[i] && a[i] <= 79 ) (c[7])++;
if( 80 <= a[i] && a[i] <= 89 ) (c[8])++;
if( 90 <= a[i] && a[i] <= 99 ) (c[9])++;
if( 100 <= a[i] ) (c[10])++;
}
for(i=0;i<M;i++)
{
b[i] = c[i];
}
}
double rnd()
{
static int t=29,c=217,m=1024,r=0;
r=(r*t+c)%m;
return((double)r/m);
}
void main()
{
FILE *wf;
int age[N], i,d[M];
int b[N]={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i++)
age[i]=(int)(115*rnd()); /*产生一个随机的年龄数组*/
printf("The original data :\n");
for(i=0; i<N; i++)
printf((i+1)%10==0? "%4d\n":"%4d",age[i]); /*每行输出10个数*/
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]);
/******************************/
wf=fopen("out.dat","w");
fun(b,d);
for(i=0; i<10; i++)
fprintf(wf,"%4d---%4d :%4d\n", i*10, i*10+9,d[i]);
fprintf(wf,"Over 100 : %4d",d[10]);
fclose(wf);
/*****************************/
}
又一个Python写法受害者
|
|