I'm trying to write a programme that essentially has a histogram-type effect.

Say I have a list of n numbers, and I want to sort them into bins based on their size - say m bins. The only way I can see how to do it (I'm using C) is to do something like

Code: Select all

` read number from file -> x`

if(x > bin_1 and x < bin_2)

{

a++

}

if(x > bin_2 and x < bin_3)

{

b++

}

if(x > bin_3 and x < bin_4)

{

c++

}

print(a,b,c...)

And do each number from the file in turn (and my values of a,b,c etc are the numbers in each respective bin).

This seems like a horribly inefficient way to do this, and becomes impossible when the number of bins gets large.

Is there an easier way to do this?

P.s. in C would be best. I really like C.