User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,912 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 1,817 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: Programming Forums
Views: 506 | Replies: 6
Reply
Join Date: Apr 2008
Posts: 18
Reputation: rob_xx17 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rob_xx17 rob_xx17 is offline Offline
Newbie Poster

shortcut to a large number of nested if-else statements

  #1  
Jul 24th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 1,769
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: shortcut to a large number of nested if-else statements

  #2  
Jul 24th, 2008
Originally Posted by rob_xx17 View Post
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.
Reply With Quote  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: shortcut to a large number of nested if-else statements

  #3  
Jul 24th, 2008
Originally Posted by rob_xx17 View Post
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 18
Reputation: rob_xx17 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rob_xx17 rob_xx17 is offline Offline
Newbie Poster

Re: shortcut to a large number of nested if-else statements

  #4  
Jul 24th, 2008
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
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: shortcut to a large number of nested if-else statements

  #5  
Jul 25th, 2008
Don't waste the time, you live in C World now:
  1. ++array[indexNum];
Reply With Quote  
Join Date: May 2006
Posts: 2,723
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 222
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: shortcut to a large number of nested if-else statements

  #6  
Jul 25th, 2008
Originally Posted by ArkM View Post
Don't waste the time, you live in C World now:
  1. ++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
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: shortcut to a large number of nested if-else statements

  #7  
Jul 26th, 2008
I like both increment forms. It's interesting that postfix ++ is potentially more expensive operator (in a full expression context, not here).
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 8:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC