求助!
/*随机数产生年龄,统计各个年龄段人数,大于100岁的人数放在b,0-9放在b,10-19放在b,类推*/#include <stdio.h>
# define N50
# define M11
void fun(int *a,int *b)
{
int c,i;
for(i=0;i<N;i++)
{
if( 0 <= a <= 9 ) (c)++; //年龄大于0小于9,c++
if( 10 <= a <= 19 ) (c)++; //同上
if( 20 <= a <= 29 ) (c)++;
if( 30 <= a <= 39 ) (c)++;
if( 40 <= a <= 49 ) (c)++;
if( 50 <= a <= 59 ) (c)++;
if( 60 <= a <= 69 ) (c)++;
if( 70 <= a <= 79 ) (c)++;
if( 80 <= a <= 89 ) (c)++;
if( 90 <= a <= 99 ) (c)++;
if( 100 <= a ) (c)++;
}
for(i=0;i<M;i++)
{
b = c;
}
}
doublernd()
{
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, i,d;
int b={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i++)
age=(int)(115*rnd()); /*产生一个随机的年龄数组*/
printf("The original data :\n");
for(i=0; i<N; i++)
printf((i+1)%10==0? "%4d\n":"%4d",age); /*每行输出10个数*/
printf("\n\n");
fun(age,d);
for(i=0; i<10; i++)
printf("%4d---%4d:%4d\n", i*10, i*10+9,d);
printf("Over 100:%4d\n",d);
/******************************/
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);
fprintf(wf,"Over 100:%4d",d);
fclose(wf);
/*****************************/
}
运行结果:
我不知道怎么贴图。。没办法放结果了 结果是年龄成功生成了,但是统计人数返回错了,不知道什么原因,求助大佬们 本帖最后由 永恒的蓝色梦想 于 2020-4-13 15:22 编辑
问题出在12-21行/*随机数产生年龄,统计各个年龄段人数,大于100岁的人数放在b,0-9放在b,10-19放在b,类推*/
#include <stdio.h>
# define N50
# define M11
void fun(int *a,int *b)
{
int c,i;
for(i=0;i<N;i++)
{
if( 0 <= a && a <= 9 ) (c)++; //年龄大于0小于9,c++
if( 10 <= a && a <= 19 ) (c)++; //同上
if( 20 <= a && a <= 29 ) (c)++;
if( 30 <= a && a <= 39 ) (c)++;
if( 40 <= a && a <= 49 ) (c)++;
if( 50 <= a && a <= 59 ) (c)++;
if( 60 <= a && a <= 69 ) (c)++;
if( 70 <= a && a <= 79 ) (c)++;
if( 80 <= a && a <= 89 ) (c)++;
if( 90 <= a && a <= 99 ) (c)++;
if( 100 <= a ) (c)++;
}
for(i=0;i<M;i++)
{
b = c;
}
}
doublernd()
{
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, i,d;
int b={32,45,15,12,86,49,97,3,44,52,17,95,63};
for(i=0; i<N; i++)
age=(int)(115*rnd()); /*产生一个随机的年龄数组*/
printf("The original data :\n");
for(i=0; i<N; i++)
printf((i+1)%10==0? "%4d\n":"%4d",age); /*每行输出10个数*/
printf("\n\n");
fun(age,d);
for(i=0; i<10; i++)
printf("%4d---%4d:%4d\n", i*10, i*10+9,d);
printf("Over 100:%4d\n",d);
/******************************/
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);
fprintf(wf,"Over 100:%4d",d);
fclose(wf);
/*****************************/
}又一个Python写法受害者 给你解释一下:
比如表达式0 <= a <= 9首先会计算0<=a假设这个数是16,结果为0原表达式变为0<=9结果为1于是本来不想执行的代码就执行了 满意请选最佳~{:10_327:} 感谢感谢!!
页:
[1]