Better way to do this (C)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
seladore
Posts: 586
Joined: Thu Mar 20, 2008 8:17 pm UTC
Location: Tumbolia

Better way to do this (C)

Postby seladore » Wed Jun 03, 2009 8:38 pm UTC

Hi,

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.

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: Better way to do this (C)

Postby stephentyrone » Wed Jun 03, 2009 8:43 pm UTC

First off, you have a bug: if x == bin_2, it won't go into any bin at all =)

Second, there are a number of "nicer" ways to do this, but the "right" way to do it will rather depend on the specifics of your bins (and possibly even the data type). Are your bins uniform in size? Floating-point or integer data? More details will get you a better answer.
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

User avatar
seladore
Posts: 586
Joined: Thu Mar 20, 2008 8:17 pm UTC
Location: Tumbolia

Re: Better way to do this (C)

Postby seladore » Wed Jun 03, 2009 8:50 pm UTC

My pseudocode was rough, sorry.

Bins are uniform in size, and using floats. I have a list of a few hundred floating point numbers roughly between 5 and 10 (more precisely a list of the log of numbers between 10^5 and 10^10), and would like to put them into bins of width 0.1. So, looking at order ~50 bins, which would take a while to code using my method.

User avatar
jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: Better way to do this (C)

Postby jaap » Wed Jun 03, 2009 8:54 pm UTC

seladore wrote:My pseudocode was rough, sorry.

Bins are uniform in size, and using floats. I have a list of a few hundred floating point numbers roughly between 5 and 10 (more precisely a list of the log of numbers between 10^5 and 10^10), and would like to put them into bins of width 0.1. So, looking at order ~50 bins, which would take a while to code using my method.


Given a floating point number f between 5 and 10.
First subtract 5, so that it is in a range starting at 0, namely between 0 and 5.
Now divide by the width of the bins, 0.1.
This gives a floating point number between 0 and 50, which when rounded down is the index number of its bin.

Code: Select all

int binIndex = (int)( (f-5)/0.1 );


Also, learn about arrays instead of having 50 variables a,b,c,... for the bin counts.

Code: Select all

int binCount[50];
...
if( binIndex>=0 && binIndex<50 ){
   binCount[ binIndex ]++
}

User avatar
seladore
Posts: 586
Joined: Thu Mar 20, 2008 8:17 pm UTC
Location: Tumbolia

Re: Better way to do this (C)

Postby seladore » Wed Jun 03, 2009 9:05 pm UTC

Great, thanks. That's a really clever solution :)

I'm off a'coding...


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests