(De Morgan’s Laws) In this chapter, we discussed the logical operators &&, || and !. De Morgan’s laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !( condition1 && condition2 ) is logically equivalent to the expression ( !condition1 || !condition2 ). Also, the expression !( condition1 || condition2 ) is logically equiv- alent to the expression ( !condition1 && !condition2 ). Use De Morgan’s laws to write equivalent expressions for each of the following, then write a program to show that the original expression and the new expression in each case are equivalent:

a) !( x < 5 ) && !( y >= 7 )
b) !( a == b ) || !( g != 5 )
c) !( ( x <= 8 ) && ( y > 4 ) )
d) !( ( i > 4 ) || ( j <= 6 ) )


```
// Prove DeMorgan’s law.
#include
#include
using namespace std;

int main()
{
// Prove part (a)
// condition1: !(x < 5), condition2: !(y >= 7)
int x = 6; // make condition1 true first time through loop
int y; // variable for condition2

cout << boolalpha << "PART A" << endl << endl;

// loop twice for condition1 true, then false
do
{
x--; // make condition1 false second time through loop
y = 5; // make condition2 true first time through loop

// loop twice for condition2 true, then false
do
{
y++; // make condition2 false second time through loop

// display current assignments
cout << "!( x < 5 ): " << !( x < 5 ) << endl;
cout << "!( y >= 7 ): " << !( y >= 7 ) << endl;

// test for validity
if ( ( !( x < 5 ) && !( y >= 7 ) ) ==
( !( ( x < 5 ) || ( y >= 7 ) ) ) )
cout << "!(x < 5) && !(y >= 7) is equivalent to"
<< " !((x < 5) || (y >= 7))" << endl;
else
cout << "!(x < 5) && !(y >= 7) is not equivalent to"
<< " !((x < 5) || (y >= 7))" << endl;

cout << endl;
} while ( !( y >= 7 ) ); // end do...while

} while ( !( x < 5 ) ); // end do...while

cout << endl << endl;

// Prove part (b)
// condition1: !(a == b), condition2: !(g != 5)
int a = 3; // make condition1 true first time through loop
int b = 5; // leave b at 5.
int g; // variable for condition2

cout << "PART B" << endl << endl;

// loop twice for condition1 true, then false
do
{
a++; // make condition1 false second time through loop
g = 4; // initially make condition2 true

// loop twice for condition2 true, then false
do
{
g++; // make condition2 false second time through loop

// display current assignments
cout << "!( a == b): " << !( a == b ) << endl;
cout << "!( g != 5): " << !( g != 5 ) << endl;

// test for validity
if ( ( !( a == b ) || !( g != 5 ) ) ==
( !( ( a == b ) && ( g != 5 ) ) ) )
cout << "!(a == b) || !(g != 5) is equivalent to"
<< " !((a == b) && (g != 5))" << endl;
else
cout << "!(a == b) || !(g != 5) is not equivalent to"
<< " !((a == b) && (g != 5))" << endl;

cout << endl;
} while ( !( g != 5 ) ); // end do...while

} while ( !( a == b ) ); // end do...while

cout << endl << endl;

// Prove part (c)
// condition1: (x <= 8), condition2: (y > 4)
x = 7; // make condition1 true first time through loop

cout << "PART C" << endl << endl;

// loop twice for condition1 true, then false
do
{
x++; // make condition1 false second time through loop
y = 6; // initially make condition2 true

// loop twice for condition2 true, then false
do
{
y--; // make condition2 false second time through loop

// display current assignments
cout << "( x <= 8 ): " << ( x <= 8 ) << endl;
cout << "( y > 4 ): " << ( y > 4 ) << endl;

// test for validity
if ( !( ( x <= 8 ) && ( y > 4 ) ) ==
( !( x <= 8 ) || !( y > 4 ) ) )
cout << "!((x <= 8) && (y > 4)) is equivalent to"
<< " !(x <= 8) || !(y > 4)" << endl;
else
cout << "!((x <= 8) && (y > 4)) is not equivalent to"
<< " !(x <= 8) || !(y > 4)" << endl;

cout << endl;
} while ( ( y > 4 ) ); // end do...while

} while ( ( x <= 8 ) ); // end do...while

cout << endl << endl;

// Prove part (d)
// condition1: (i > 4), condition2: (j <= 6)
int i = 6; // make condition1 true first time through loop
int j; // variable for condition2

cout << "PART D" << endl << endl;

// loop twice for condition1 true, then false
do
{
i--; // make condition1 false second time through loop
j = 5; // initially make condition2 true

// loop twice for condition2 true, then false
do
{
j++; // make condition2 false second time through loop

// display current assignments
cout << "( i > 4 ): " << ( i > 4 ) << endl;
cout << "( j <= 6 ): " << ( j <= 6 ) << endl;

// test for validity
if ( !( ( i > 4 ) || ( j <= 6 ) ) ==
( !( i > 4 ) && !( j <= 6 ) ) )
cout << "!((i > 4) || (j <= 6)) is equivalent to"
<< " !(i > 4) && !(j <= 6)" << endl;
else
cout << "!((i > 4) || (j <= 6)) is not equivalent to"
<< " !(i > 4) && !(j <= 6)" << endl;
cout << endl;
} while ( ( j <= 6 ) ); // end do...while

} while ( ( i > 4 ) ); // end do...while
} // end main
```
PART A
```
!( x < 5 ): true
!( y >= 7 ): true
!(x < 5) && !(y >= 7) is equivalent to !((x < 5) || (y >= 7))
!( x < 5 ): true
!( y >= 7 ): false
!(x < 5) && !(y >= 7) is equivalent to !((x < 5) || (y >= 7))
!( x < 5 ): false
!( y >= 7 ): true
!(x < 5) && !(y >= 7) is equivalent to !((x < 5) || (y >= 7))
!( x < 5 ): false
!( y >= 7 ): false
!(x < 5) && !(y >= 7) is equivalent to !((x < 5) || (y >= 7))
```
PART B
```
!( a == b): true
!( g != 5): true
!(a == b) || !(g != 5) is equivalent to !((a == b) && (g != 5))
!( a == b): true
!( g != 5): false
!(a == b) || !(g != 5) is equivalent to !((a == b) && (g != 5))
!( a == b): false
!( g != 5): true
!(a == b) || !(g != 5) is equivalent to !((a == b) && (g != 5))
!( a == b): false
!( g != 5): false
!(a == b) || !(g != 5) is equivalent to !((a == b) && (g != 5))
```
PART C
```
( x <= 8 ): true
( y > 4 ): true
!((x <= 8) && (y > 4)) is equivalent to !(x <= 8) || !(y > 4)
( x <= 8 ): true
( y > 4 ): false
!((x <= 8) && (y > 4)) is equivalent to !(x <= 8) || !(y > 4)
( x <= 8 ): false
( y > 4 ): true
!((x <= 8) && (y > 4)) is equivalent to !(x <= 8) || !(y > 4)
( x <= 8 ): false
( y > 4 ): false
!((x <= 8) && (y > 4)) is equivalent to !(x <= 8) || !(y > 4)
```
PART D
```
( i > 4 ): true
( j <= 6 ): true
!((i > 4) || (j <= 6)) is equivalent to !(i > 4) && !(j <= 6)
( i > 4 ): true
( j <= 6 ): false
!((i > 4) || (j <= 6)) is equivalent to !(i > 4) && !(j <= 6)
( i > 4 ): false
( j <= 6 ): true
!((i > 4) || (j <= 6)) is equivalent to !(i > 4) && !(j <= 6)
( i > 4 ): false
( j <= 6 ): false
!((i > 4) || (j <= 6)) is equivalent to !(i > 4) && !(j <= 6)
```

Computer Science & Information Technology

You might also like to view...

The executeQuery method ____________ .

a) retrieves information from a database b) modifies information in a database c) establishes a connection to a database d) closes a connection to a database

Computer Science & Information Technology

The width of a Calc column will automatically increase as the user types more data

Indicate whether the statement is true or false

Computer Science & Information Technology

Least privilege is a concept that denies all traffic to a resource unless the user is specifically granted access to that resource

Indicate whether the statement is true or false

Computer Science & Information Technology

You're creating a network for an office. You need to connect two office buildings that are approximately 900 meters apart. You have been asked to provide a fast network backbone. Which of the following types of cable will you choose to connect those buildings?

A) STP B) MMF C) SMF D) UTP

Computer Science & Information Technology