Hi, can someone give me a hint on how a histogram's pseudo code would look like?
+1
A:
Well, you'd probably have a bunch of printf statements at the top for your headers to give some meaning to the data. Then maybe a line of dashes or equals or something to work as a separator.
Then below that, loop through an array with the values you wish to plot. One on each line.
Kyle Walsh
2009-09-11 23:00:52
+4
A:
How to structure and fill a histogram?
Trivial case is just a count per bin:
/* needs error checking, badly */
int *buildHist(int bins, double min, double max, int n, double *data){
double *hist=malloc(bins*sizeof(int));
if (hist == NULL) return hist;
for (int i=0; i<n; ++i){
int bin=int( (data[i]-min)/((max-min)/(bins)) );
if ( (bin>=0) && (bin<n) ) hist[bin]++;
}
return hist;
}
For a weighted histogram, the array must be of floating point type.
With more data (over- and under-flow counts, accumulated statistics...or even to keep the limits in the same place as the count), use a structure that includes the array.
Incremental filling is often desired, but should be obvious from here.
Output depends a great deal on what display technology you have at hand.
dmckee
2009-09-11 23:07:45
a friend of mine helped me get started, but I don't know what he intended to do... here's what I have void computeHistogram(int data[], int data_size, int histo[], int histo_size) { int min = 99999999; int max = -99999999; int i = 0; while(i<data_size){ if(data[i] < min){ min = data[i];} if(data[i]>max){ max = data[i]; } } printf("min of data is %d", min);}
2009-09-11 23:29:27
It looks like he intends to set the limits dynamically. Make one pass through the data to find the limiting values, then make *another* pass through the data to fill the histogram. Your firend gave you the limit finding pass, and the filling pass will look like the one I exhibit above.
dmckee
2009-09-11 23:33:24
thank you dmckee, but I was instructed not to use pointers.... is there an alternative? thanks!
2009-09-12 00:16:35
Substitute arrays like your friend did, and allocate both arrays on the stack in the calling routine. And ditch the `malloc`.
dmckee
2009-09-12 00:24:03