Sunday, August 2, 2009

C++ Boolean question..Will you help me =( ?

Yah as you can guess its a homework...i tried alot and i have no idea ... it must be done today ..





Any solves or hints will be so appreciated,here's the question:


--------------------------------------...


De Morgan’s Laws can sometimes make it more convenient for us to express a logical expression.


These laws state that the expression !(condition1 %26amp;%26amp; condition2) is logically equivalent to the


expression (!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically


equivalent to the expression (!condition1 %26amp;%26amp; !condition2). Use De Morgan’s Laws to write


equivalent expressions for each of the following, then write a program to show that both the


original expression and the new expression in each case are equivalent:


A. !( x %26lt; 5 ) %26amp;%26amp; !( y %26gt;= 7 )


B. !( a == b ) || !( g != 5 )


C. !( ( x %26lt;= 8 ) %26amp;%26amp; ( y %26gt; 4 ) )


D. !( ( i %26gt; 4 ) || ( j %26lt;= 6 ) )


--------------------------------------...


Thanks .

C++ Boolean question..Will you help me =( ?
A. !( x %26lt; 5 ) %26amp;%26amp; !( y %26gt;= 7 )





This is of the form (!condition1 %26amp;%26amp; !condition2), which according to the second law you state, is the same as !( condition1 || condition2 ), or in this case, !( x %26lt; 5 || y %26gt;= 7 ).





Your c++ code could look something like this:





int x,y;


x = 1; //pick any two numbers


y = 2;





bool result1, result2;


result1 = !( x %26lt; 5 ) %26amp;%26amp; !( y %26gt;= 7 );


result2 = !( x %26lt; 5 || y %26gt;= 7 );





cout %26lt;%26lt; "!( x %26lt; 5 ) %26amp;%26amp; !( y %26gt;= 7 ) is" %26lt;%26lt; result1 %26lt;%26lt; endl;


cout %26lt;%26lt; "!( x %26lt; 5 || y %26gt;= 7 ) is" %26lt;%26lt; result2 %26lt;%26lt; endl;











B. !( a == b ) || !( g != 5 )


Looks like (!cond1 || !cond2), which is equivalent to !(cond1 %26amp;%26amp; cond2).





C. !( ( x %26lt;= 8 ) %26amp;%26amp; ( y %26gt; 4 ) )


!(cond1 %26amp;%26amp; cond2) -%26gt; (!cond1 || !cond2)





D. !( ( i %26gt; 4 ) || ( j %26lt;= 6 ) )


!(cond1 || cond2) -%26gt; (!cond1 %26amp;%26amp; !cond2)








Hopefully that gives you enough of a start to fill in the rest of it.
Reply:These are all REALLY easy applications of DeMorgan's law. They're stated exactly in the format that the law is stated in. For example, if we have this expression:





!(x %26gt;= 10) %26amp;%26amp; !(y %26lt; 2)





we can use the second form of DeMorgan's law you've given in your question to turn this into:





!((x %26gt;= 10) || (y %26lt; 2))





If we have the opposite situation, that is, we're given an expression with a "not" operator outside the entire expression, we just "not" the terms and change the operator. So:





!((x %26gt; 6) %26amp;%26amp; (y %26lt;= 10))





becomes





!(x %26gt; 6) || !(y %26lt;= 10)





That will solve all four of the problems in about 30 seconds.





A program to test these expressions is a little tougher but the idea would be to set x and y to values that alternately make the terms in the expressions true or false then verify that "if" statements containing both your form and the original form of the expression evaluate the same. So for example:





int x = 1;


int y = 11;





if ((!((x %26gt; 6) %26amp;%26amp; (y %26lt;= 10))) != (!(x %26gt; 6) || !(y %26lt;= 10)))


printf("Failed with x = %d and y = %d\n", x, y);





x = 7;





if ((!((x %26gt; 6) %26amp;%26amp; (y %26lt;= 10))) != (!(x %26gt; 6) || !(y %26lt;= 10)))


printf("Failed with x = %d and y = %d\n", x, y);





x = 1;


y = 9;





if ((!((x %26gt; 6) %26amp;%26amp; (y %26lt;= 10))) != (!(x %26gt; 6) || !(y %26lt;= 10)))


printf("Failed with x = %d and y = %d\n", x, y);





x = 7;





if ((!((x %26gt; 6) %26amp;%26amp; (y %26lt;= 10))) != (!(x %26gt; 6) || !(y %26lt;= 10)))


printf("Failed with x = %d and y = %d\n", x, y);








As you can see we've chosen values of x and y in each case to test every combination of the terms being true and false. If the program runs without generating any error messages, your expressions are equivalent.





Good luck.

floral

No comments:

Post a Comment