•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,068 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 323 | Replies: 6
![]() |
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
•
•
Join Date: Jan 2008
Posts: 1,405
Reputation:
Rep Power: 6
Solved Threads: 179
•
•
•
•
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 statementsThe 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.
•
•
Join Date: Jun 2008
Posts: 78
Reputation:
Rep Power: 1
Solved Threads: 6
•
•
•
•
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.
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. •
•
Join Date: Apr 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 0
thanks a million, that worked like a champ. the only thing that I added at the end was:
to keep track of the values being added.
thanks
array[indexNum]=array[indexNum]+1;
thanks
•
•
Join Date: Jul 2008
Posts: 304
Reputation:
Rep Power: 2
Solved Threads: 44
Don't waste the time, you live in C World now:
c Syntax (Toggle Plain Text)
++array[indexNum];
•
•
•
•
Don't waste the time, you live in C World now:
c Syntax (Toggle Plain Text)
++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)
Age is unimportant -- except in cheese
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Other Threads in the C Forum
- Previous Thread: error C2447: '{' : missing function header (old-style formal list?)
- Next Thread: ARP keep sending wrong IP address



Linear Mode