Algorithm doubt

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 2
Reputation: Karthi Msc is an unknown quantity at this point 
Solved Threads: 0
Karthi Msc Karthi Msc is offline Offline
Newbie Poster

Algorithm doubt

 
1
  #1
Sep 17th, 2006
Hi can anyone help me in doing the following

Given n Boolean variables x1,x2,…..xn, we wish to print all possible combinations of truth values they can assume. For instance, if n=2, there are four possibilities : true, true; false, false; false, true; true, false;:-|
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Algorithm doubt

 
0
  #2
Sep 17th, 2006
Simple. Just use nested for loops.

  1. bool x1[2] = {true, false}
  2. bool x2[2] = {true, false}
  3. for ( i = 0 ; i < 1; i++ )
  4. for ( j = 0 ; j < 1; j++ )
  5. print x1[i], x2[j];
will print the answer for two variables. Expand it for n variables.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Algorithm doubt

 
1
  #3
Sep 17th, 2006
Maybe something like this which will implement a basic AND logic will help you understand the thing better:

  1. int main ()
  2. {
  3. bool first [] = {true, false} ;
  4. bool second [] = {true, false} ;
  5.  
  6. for (int i = 0; i < 2; ++i) {
  7. for (int j = 0; j < 2; ++j) {
  8. cout << boolalpha << first [i] << " AND " << second [j] << " => " << (first[i] && second [j]) ;
  9. cout << endl ;
  10. }
  11. }
  12. return 0 ;
  13. }

Hope it helped, bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Algorithm doubt

 
2
  #4
Sep 17th, 2006
for ( i = 0 ; i < 1<<N ; i++ )
Just print out the binary representation of i to N bits of precision.
Easy money.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC