can someone give me a hint on how a histogram's pseudo code would look like?
Asked
Active
Viewed 8,685 times
3
-
3This question needs much more detail. – Noldorin Sep 11 '09 at 23:09
-
Why, because the question is short? How many ways of doing a standard histogram are there? – Dervin Thunk Sep 11 '09 at 23:44
-
Because he doesn't say what he wants or where he is stuck. Naming the kind of data he wants to stick in it would be good too. I had to guess as did others. – dmckee --- ex-moderator kitten Sep 11 '09 at 23:50
-
2Continued by http://stackoverflow.com/questions/1413725/console-doesnt-display-histogram and http://stackoverflow.com/questions/1415348/design-a-frequncy-histogram-without-pointers-in-c. – Steve Melnikoff Sep 12 '09 at 16:09
2 Answers
8
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 --- ex-moderator kitten
- 98,632
- 24
- 142
- 234
-
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
max){ max = data[i]; } } printf("min of data is %d", min); } – user133466 Sep 11 '09 at 23:29 -
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 --- ex-moderator kitten Sep 11 '09 at 23:33
-
thank you dmckee, but I was instructed not to use pointers.... is there an alternative? thanks! – user133466 Sep 12 '09 at 00:16
-
Substitute arrays like your friend did, and allocate both arrays on the stack in the calling routine. And ditch the `malloc`. – dmckee --- ex-moderator kitten Sep 12 '09 at 00:24
0
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
- 2,774
- 4
- 27
- 25