打印直方图的题目,怎么都打印不出来,求帮助啊,先谢过啦
#include <stdio.h>#include <stdlib.h>
typedef struct {int nb;double min;double max;double taille;int *data;}hist;
int main(void){
int k,x,j,i;
double *sample;
unsigned size;
hist histo;
printf("please input the size of the sample:\n");
scanf("%d",&x);
sample=malloc(x*sizeof(double));
printf("please input %d numbers as the sample:\n",x);
for(j=0;j<x;j++)scanf("%lf",&sample[j]);
printf("give the min:\n");
scanf("%lf",&histo.min);
printf("give the max:\n");
scanf("%lf",&histo.max);
printf("give how many cases you want:\n");
scanf("%d",&size);
histo.taille=(histo.max-histo.min)/size;
printf("the taille of the histogram is %lf\n",histo.taille);
printf("max=%lf\nmin=%lf\n",histo.max,histo.min);
histo.data=malloc(size*sizeof(int));
histogramme(sample,size,histo);
for (k=0;k<size;k++)
{printf("\nfrom%lf to %lf:",histo.min+k*histo.taille,histo.min+(k+1)*histo.taille);
for (i=0;i<histo.data[k];i++)printf("*");}
return 0;
}
unsigned histogramme(double const *sample,unsigned size,hist histo){
int k,x,j;
for(k=0;k<size;k++)histo.data[k]=0;
for(j=0;j<x;j++)
for(k=0;k<size;k++)
{if (histo.min+k*histo.taille<=sample[j]&&sample[j<histo.min(k+1)*histo.taille)histo.data[k]+=1;}
}
理想结果应该是能打印出直方图的
比如:
please input the size of the sample:
3
please input %d numbers as the sample:
2
1
3
give the min:
1
give the max:
3
give how many cases you want:
3
the taille of the histogram is:0.666667
min=1.000000
max=3.000000
到这里都没有问题就是在打印直方图的时候本应该为
from 1.000000 to 1.666667:*
from 1.666667 to 2.333333:*
from 2.333333 to 3.000000:*
但是我编写的程序什么都打印不出来
只有这样而已:
from 1.000000 to 1.666667:
from 1.666667 to 2.333333:
from 2.333333 to 3.000000:
我打印出来的完全没有星号,不知道是什么原因,我把程序直接改成printf试了,发现histo.data中所有元素还是0,所以感觉问题应该出在histogramme 这个函数的循环身上,但是不知道哪里有问题,真苦恼,求高人指教,先谢过啦,谢谢谢谢哦