float_histogram.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <math.h>
00003 
00004 #define MAX_VALUE 2000.0
00005 #define STEP 0.1
00006 
00007 int main(int argc, char *argv[]) {
00008 
00009   char *histogram_filename = argv[1];
00010   FILE *histogram_file, *infile;
00011   int num_entries = MAX_VALUE/STEP;
00012   double *hist;
00013   float f;
00014   double data;
00015   int i;
00016 
00017 
00018   hist = (double *)calloc(num_entries, sizeof(double));
00019 
00020   if ((histogram_file = fopen(histogram_filename, "rb")) != NULL) {
00021     fread(hist, sizeof(double), num_entries, histogram_file);
00022     fclose(histogram_file);
00023   }
00024   infile = stdin;
00025 
00026   while(fscanf(infile, "%f", &f) != EOF) {
00027     data = (double)f;
00028     if (data >= MAX_VALUE) {
00029       fprintf(stderr, "Value %f greater than max. allowed in histogram (%f)\n", data, MAX_VALUE);
00030       data = 0.9999*MAX_VALUE;
00031     }
00032     i = (int)floor(data/STEP);
00033     hist[i]++;
00034   }
00035 
00036   histogram_file = fopen(histogram_filename, "wb");
00037   fwrite(hist, sizeof(double), num_entries, histogram_file);
00038   fclose(histogram_file);
00039 }

Generated on Tue Jan 6 00:30:37 2009 for Gift by  doxygen 1.5.6