/***********************************************
* ./hist <N>
* This program prints out a histogram for nums
* in the range (0,1). The histogram uses N
* buckets, where N is the program's one-and-only
* command-line argument. We need dynamic memory
* allocation here because the size of B, the
* array of buckets is not known til run-time.
***********************************************/
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
// Get N, number of buckets, from command-line
int N = atoi(argv[1]);
// Allocate N ints and init to zero. NOTE: this
// would have been easier using calloc!
int *B = (int*)malloc(N*sizeof(int));
int j;
for(j = 0; j < N; ++j)
B[j] = 0;
// Read values and increment bucket counts
int i;
double x;
while(scanf("%lf",&x) == 1)
{
i = N*x;
B[i]++;
}
// Write histogram and free memory
for(j = 0; j < N; ++j)
printf("%i ",B[j]);
printf("\n");
free(B);
return 0;
}