#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define n 30 // hst.length = n
#define N 10000
int main(void)
{
double hst[n]={0};
double dx = (double) 1/n; // 1/n devolve int!!!
double x;
int i,j;
// for (j=0; j<n; j++)
// hst[j]=0;
for (i=0; i<N; i++) {
x = sqrt((double) rand()/RAND_MAX); // rand()/RAND_MAX devolve int!!!
// printf("%lf\n",x);
for (j=0; j<n; j++) {
if (x>=j*dx && x<(j+1)*dx) {
hst[j]++;
}
}
}
// printf("\n");
for (j=0; j<n; j++) {
printf("%lf \n",hst[j]);
}
// achando o máximo da distribuição:
double hmax = log(0); // pra ser menos infinito
for (i=0; i<n; i++) {
if (hst[i]>hmax) hmax = hst[i];
}
// normalizando a distribuição (sendo o maior valor 100):
for (i=0; i<n; i++) {
hst[i] = hst[i]/hmax*100;
}
// graficando a distribuição:
for (i=0; i<n; i++) {
for (j=0; j<hst[i]; j++) {
printf("o");
}
printf("\n");
}
return 0;
}
Comments