944,075 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1150
  • C++ RSS
Sep 17th, 2006
1

Algorithm doubt

Expand Post »
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;:-|
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Karthi Msc is offline Offline
2 posts
since Sep 2006
Sep 17th, 2006
0

Re: Algorithm doubt

Simple. Just use nested for loops.

C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Sep 17th, 2006
1

Re: Algorithm doubt

Maybe something like this which will implement a basic AND logic will help you understand the thing better:

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Sep 17th, 2006
2

Re: Algorithm doubt

for ( i = 0 ; i < 1<<N ; i++ )
Just print out the binary representation of i to N bits of precision.
Easy money.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with the execution of this program
Next Thread in C++ Forum Timeline: Determine if a certain ivalue is in a string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC