CS 110 Spring 2012
Homework 04: Booleans and Digital Logic
Due: Monday, 20 February in class
In Homework 4 you will explore logical notation and boolean algebra,
practice if statements, and even work with a program that
simulates digital circuits.
In this writeup, we will use the 'v' symbol for OR and the '^' symbol for AND and the ¬ symbol for NOT. Precedence rules for boolean expressions are (1) negation is always evaluated first, (2) AND is evaluated next, and (3) OR is evaluated last. Parenthesis can change this order.
Part 1 - Simplify simplify
Simplify the following boolean expressions (labeled as Q) as much as possible. That is, your task is to find the smallest expression that is equivalent to the stated expression. You may want to write out a truth table to check your solutions. The first two solutions are given, along with explanations, but you are not required to provide explanations for your answers. For parts 1 through 3 in this lab, submit your solutions as a text file called logic.rtf.
1. Q = A v false
Solution: Q = A (if A is true, Q is true, and if A is false, Q is false, and thus A and Q are the same)
2. Q = A ^ false
Solution:
Q = false (regardless of whether A is true or false, Q is false)
3. Q = A v true
Solution: Q =
4. Q = A ^ true
Solution: Q =
5. Q = A^ ¬A
Solution: Q =
6. Q = A v ¬A
Solution: Q =
7. Q = ¬(¬A)
Solution: Q =
8. Q = (A v B) ^ (A v ¬B)
Solution: Q =
9. Q = ¬(¬A v ¬B)
Solution: Q =
10. Q = ¬(¬A ^ ¬B)
Solution: Q =
Part 2 - Are these the same?
Indicate which of the following equations are correct for all truth values of A, B and C, and which are not. Justify your answer. To be clear, a justification that two expressions are different simply requires you to specify truth values for the variables such that the two expressions disagree. A justification that they are the same requires you to create a complete truth table in which all entries for the two expressions match.
1. (A v B) ^ ¬(A ^ B) = (A ^ ¬B) v (¬A ^ B)
2. (A v B) ^ C = A ^ (B v C)
Part 3 - Find the expression
For both problems, generate a boolean expression that matches the specified output. Your solutions should be added to logic.rtf.
1.
A |
B |
? |
T |
T |
T |
T |
F |
T |
F |
T |
F |
F |
F |
T |
2.
A |
B |
C |
? |
T |
T |
T |
F |
T |
T |
F |
F |
T |
F |
T |
T |
T |
F |
F |
T |
F |
T |
T |
F |
F |
T |
F |
F |
F |
F |
T |
T |
F |
F |
F |
F |
Part 5 - Practice with ifs
In Python, create three programs that perform the following tasks, with the specified name.
1. (GradeReport.py) Write a full Python program (main() function definition with main() invocation) with the following functionality; prompt the user for their test grade. If their grade is 90 or above, print "You got an A!" If the grade is between 80 and 89, print "You got a B!" If the grade is between 70 and 79, print "You got a C." And if the grade is below 70, print "Uh oh... you should probably see me." One and only one output should be printed for any particular value.
2. (Divisible.py) Write a full Python program that prompts the user to enter an integer. Then report to the user whether that number is divisible by 2, 3 and 5. For example, if the user enters 15, the output should be "15 is not divisible by 2, is divisible by 3, and is divisible by 5." If the user enters 64, the output should be "15 is divisible by 2, is not divisible by 3, and is not divisible by 5." Use only 3 if-statements.
3. (RoomSize.py) Write a full Python program that prompts the user to enter an integer room width and length in feet. If the area of the room is less than 100, print "The room is too small." If the area is greater than 900, print "The room is too big." If the area is in between, but either the width is more than three times the height or the height is more than three times the width, print "The room is a good size, but the shape is kinda stupid." Finally, if none of these problems exist, print "Looks OK I guess... I'll take it."
Part 6 - Simple circuits
In Logisim, create circuits representing the following boolean expressions, and save them with the specified file name. For each problem, check that the circuit behaves correctly on all possible inputs.
1. (6_1.circ) (A v ¬B) ^ (¬A v B)
2. (6_2.circ) (A ^ B) v ¬(A v B)
3. (6_3.circ) ((A ^ B) v (A ^ C) v (B ^ C)) ^ (¬A v ¬B v ¬C)
Part 7 - Abacus
In Logisim, create circuits with the following properties, and save them with the specified file name. Again, you'll want to check that your circuits have the desired properties. You may find it helpful to explicitly construct the truth tables and boolean expressions for such a function before trying to build the circuits.
1. (pairity.circ) A circuit with three inputs and one output that yields T if an odd number of inputs are T, and F otherwise. (For those of you who need a reminder, 0 is most decidedly an even number.)
2. (majority.circ) A circuit with three inputs and one output that yields T if the majority of the inputs are T, and F otherwise.
Part 8 - Recursion Stack Diagram
As discussed in class on Friday, I want you to go through the exercise of following recursive function execution by hand-drawing a function stack diagram corresponding to a main() function invoking factorial(3). For reference, you can use the definition of factorial given below. I recommend that you use the Wing debugger and step through the execution. Within each box of the stack diagram, you should include the parameter variables along with any local variable (n, smallerresult, myresult) with their values. You must also include an arrow with an associated return value from one function to the caller of that function.
def factorial(n): if n == 0 or n == 1: return 1 else: smallerresult = factorial(n-1) myresult = n * smallerresult return myresult
Part 9 - Uploading your lab
Upload the assigned files to a folder named Lab04 in the Assignment Inbox.
-
logic.rtf
-
6_1.circ
-
6_2.circ
-
6_3.circ
-
parity.circ
-
majority.circ
-
GradeReport.py
-
Divisible.py
-
RoomSize.py