1

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;:-|

4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by Salem
0

Simple. Just use nested for loops.

bool x1[2] = {true, false}
bool x2[2] = {true, false}
for ( i = 0 ; i < 1; i++ )
          for ( j = 0 ; j < 1; j++ )
                  print x1[i], x2[j];

will print the answer for two variables. Expand it for n variables.

1

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

int main ()
{
    bool first [] = {true, false} ;
    bool second [] = {true, false} ;

    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            cout << boolalpha << first [i] << "  AND  " << second [j] << "  =>  " << (first[i] && second [j]) ;
            cout << endl ;
        }
    }
    return 0 ;
}

Hope it helped, bye.

2

for ( i = 0 ; i < 1<<N ; i++ ) Just print out the binary representation of i to N bits of precision.
Easy money.

Votes + Comments
nice (andor)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.