hello,

I have a situation where I'm going to be generating a whole bunch of numbers. These numbers will be in the range between 0 and 1. What I will have to do is to separate these numbers into 100 different groups depending if the number is between 0 and 0.01 (first group), 0.01 and 0.02 (second group), 0.02 and 0.03 (third group) etc. all the way up to 0.99 and 1.0 (the 100th group). I also have to count how many numbers I have in each group. Is there an easier way to do it than writing 100 if-else nested statements?


Thanks.

r.

hello,

I have a situation where I'm going to be generating a whole bunch of numbers. These numbers will be in the range between 0 and 1. What I will have to do is to separate these numbers into 100 different groups depending if the number is between 0 and 0.01 (first group), 0.01 and 0.02 (second group), 0.02 and 0.03 (third group) etc. all the way up to 0.99 and 1.0 (the 100th group). I also have to count how many numbers I have in each group. Is there an easier way to do it than writing 100 if-else nested statements?


Thanks.

r.

Definitely don't write 100 if statements. If you have a 2-D array of doubles, like, say:

double groups[100][MAX_NUM_ELEMENTS_IN_GROUP];

where 100 is the number of groups, as in your case, and you have a double with a value of 0.045, you want to add that to the groups[4] array, so you want to calculate the array index 4 from 0.045. Do that by multiplying 0.045 times 100 and casting it to an integer:

int indexNum;
double value;
value = 0.045;
indexNum = (int)(value * 100.0);  // result is 4
// add number to groups[indexNum] array

That's far better than:

if (value < 0.01)
    indexNum = 0;
else if (value < 0.02)
    indexNum = 1;
// etc. for 100 if statements

The 100 groups may not be arrays of doubles. They could be linked lists of doubles or whatever. You still need to get 4 from 0.045 and the first way is far better than the second.

Comments
Goog approch :)

I want to heartily second Vernon's post - not only is it a great trick for your problem, but it can be used for many others, as well. It's usually called Distributed Counting or Bucketsort, but you don't hear much about it, for some unknown reason.

It has several advantages: 1) It's screamingly fast - far faster than any sorting routine, for instance, and 2) The code is extremely short.

Vernon's description was quite good, but also concise (I guess we know why he likes C, now). :) If you have questions about it, don't hesitate to ask, the technique is sublime and the essence of smart coding, in certain cases.

thanks a million, that worked like a champ. the only thing that I added at the end was:

array[indexNum]=array[indexNum]+1;

to keep track of the values being added.

thanks

Don't waste the time, you live in C World now:

++array[indexNum];

I've always preferred the post-inc format array[indexNum]++; for some reason. It's programmer's choice since they both ultimately do the same thing.

And just for completeness, you can also do array[indexNum ] += 1; Not extremely useful for increments of 1, but great if you need to add other than one (integer and floating)

I like both increment forms. It's interesting that postfix ++ is potentially more expensive operator (in a full expression context, not here).

This article has been dead for over six months. Start a new discussion instead.