UMBC CMSC 201
Spring '09

UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help

Boolean Logic Problems

Use DeMorgan's law to negate each of the following conditions.

  1. x == y && y == z

  2. a != b && b == c

  3. x < y || x == z

  4. a == x && b == y && c == z

  5. x <= 5 || c != z

    Write the code for each of the problems below, then click on the problem name to see the answer. The code is well-commented so you can understand what each segment does.

  6. Write a do-while loop that will ask the user to enter an integer within the range of 1 to 10, rejecting values not in that range, and continues to prompt the user for new values until a value in the range is entered.

  7. Write a while loop that will ask the user to enter positive integers to be summed. The loop should end when the user enters the sentinel, -1, or when 10 values have been entered. So the loop should not accept more than 10 values. Remember that in this class the break; statement cannot be used to break out of a loop, so a combination of both continuation conditions needs to be in the condition of the while loop. Use DeMorgan's law to help you negate the termination conditions stated in the problem.


Boolean Logic Problem Solutions

  1. !(x == y && y == z) is equivalent to x != y || y != z

    Explain

  2. !(a != b && b == c) is equivalent to a == b || b != c

    Explain


  3. !(x < y || x == z) is equivalent to x >= y && x != z

    Explain


  4. !(a == x && b == y && c == z) is equivalent to a != x || b != y || c != z

    Explain


  5. !(x <= 5 || c != z) is equivalent to x > 5 && c == z

    Explain


  6. the do-while loop implementation of values in a range: #include <stdio.h> int main() { int value; /* Give explanation to user */ printf("This program accepts a value within the range\n"); printf("of 1 to 10 and will reject all other values,\n"); printf("running until a good value is entered.\n"); /* Get a valid value */ do { printf("Enter a value between 1 and 10, inclusive: "); scanf ("%d", &value); /* If the value isn't within the range * print an error message */ if(value < 1 || value > 10) { printf("Sorry, %d is not in the range of 1 - 10\n", value); } }while (value < 1 || value > 10); /* Print the value */ printf("The valid value you entered is %d\n", value); return 0; }
    Explain

  7. the while loop implementation of a limited size list: #include <stdio.h> int main() { int value, total, count; /* Give directions to user */ printf("This program sums not more than 10 numbers.\n"); printf("Signal the end of the list with a -1.\n"); /* Initialize total and count */ count = total = 0; /* Get the first integer from the user */ printf("Enter an integer, -1 to end : "); scanf("%d", &value); /* Continue to get integers from the user until * a -1 is entered or the maximum number of values * has been reached, accumulating into total */ while (value != -1 && count != 9) { count++; total += value; printf("Enter an integer, -1 to end : "); scanf("%d", &value); } /* Print the total */ printf("The total is %d\n", total); return 0; }
    Explain


Boolean Logic Explanations

  1. !(x == y && y == z)
    Using DeMorgan's law to negate an entire boolean expression involves
    breaking that expression down into its component parts and negating
    each part individually.
    The expression: x == y && y == z is made up of three parts
    1. x == y
    2. &&
    3. y == z
    Negating x == y yields    x != y
    Negating && yields    ||
    Negating y == z yields    y != z
    So the answer is :
    !(x == y && y == z) is equivalent to x != y || y != z


  2. !(a != b && b == c)
    Using DeMorgan's law to negate an entire boolean expression involves
    breaking that expression down into its component parts and negating
    each part individually.
    The expression: a != b && b == c is made up of three parts
    1. a != b
    2. &&
    3. b == c
    Negating a != b yields    a == b
    Negating && yields    ||
    Negating b == c yields    b != c
    So the answer is :
    !(a != b && b == c) is equivalent to a == b || b != c


  3. !(x < y || x == z)
    Using DeMorgan's law to negate an entire boolean expression involves
    breaking that expression down into its component parts and negating
    each part individually.
    The expression: x < y || x == z is made up of three parts
    1. x < y
    2. ||
    3. y == z
    Negating x < y yields    x >= y
    Negating || yields    &&
    Negating y == z yields    y != z
    So the answer is :
    !(x < y || x == z) is equivalent to x >= y && x != z


  4. !(a == x && b == y && c == z)
    Using DeMorgan's law to negate an entire boolean expression involves
    breaking that expression down into its component parts and negating
    each part individually.
    The expression: a == x && b == y && c == z is made up of five parts
    1. a == x
    2. &&
    3. b == y
    4. &&
    5. c == z
    Negating a == x yields    a != x
    Negating && yields    ||
    Negating b == y yields    b != y Negating && yields    ||
    Negating c == z yields    c != z
    So the answer is :
    !(a == x && b == y && c == z) is equivalent to a != x || b != y || c != z

  5. !(x <= 5 || c != z) is equivalent to x > 5 && c == z
    Using DeMorgan's law to negate an entire boolean expression involves
    breaking that expression down into its component parts and negating
    each part individually.
    The expression: x <= 5 || c != z is made up of three parts
    1. x <= 5
    2. ||
    3. c != z
    Negating x <= 5 yields    x > 5
    Negating || yields    &&
    Negating c != z yields    c == z
    So the answer is :
    !(x <= 5 || c != z) is equivalent to x > 5 && c == z

  6. the do-while loop implementation of values in a range:
    Prompting of the user to enter a value in the range of 1 to 10 is being performed within the loop
    so that it can be repeated as many times as necessary if the user continuously enters invalid values.
    It's easy to think of the termination condition : value >= 1 && value <= 10
    but not always easy to figure out the continuation condition.
    We can just use DeMorgan's law to get value < 1 || value > 10.

  7. the while loop implementation of a limited size list:
    Since this is a while loop, we need to use a priming read to get the first value.
    Since the value entered might be the sentinel if the user decides not to run the program after reading what it's about, we shouldn't increment count until we're sure we got a good value. So we have to wait until the loop has been entered.
    Why is the condition of the while loop (value != -1 && count != 9) ?
    Since the value of count is 0 on the first pass through the loop and we don't want to allow more than 10 values,
    those 10 values could be thought of as values related to a count of 0 through 9 for a total of 10 values.


UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help

Thursday, 22-Jan-2009 11:40:57 EST