Course 10.3Lesson 3 of 3
A problem in words can be turned into a logic expression, a circuit and a truth table. You learn which words point to which gate and how to place the brackets. Then you write an expression from a truth table, with one AND term for each row whose output is 1.
Try the code and the simulations beside the lesson.
A greenhouse has a fan. The fan should switch on when it is hot and the window is not open, or when the air is damp.
The expression is X = (T AND (NOT W)) OR H.
The circuit has a NOT gate on W, an AND gate for the first group and an OR gate at the output. It is drawn exactly as the words say.
The fan is on in 5 of the 8 rows. Its output column, in the order T W H from 0 0 0 to 1 1 1, is 0, 1, 0, 1, 1, 1, 0, 1.
Paper 2 also uses the words AND, OR and NOT in pseudocode, to join conditions. Here is the fan rule as Cambridge pseudocode. It is shown, not run: the site's code boxes run Python only.
IF (Hot = TRUE AND WindowOpen = FALSE) OR Damp = TRUE
THEN
FanOn ← TRUE
ELSE
FanOn ← FALSE
ENDIFThe runnable Python version of the same rule is in Try it in Python below.
Sometimes you are given only the table. Use the rows where the output is 1.
Take this table, with the rows in the usual order:
| A B C | X |
|---|---|
| 0 0 0 | 0 |
| 0 0 1 | 0 |
| 0 1 0 | 0 |
| 0 1 1 | 1 |
| 1 0 0 | 0 |
| 1 0 1 | 1 |
| 1 1 0 | 0 |
| 1 1 1 | 1 |
X = 1 in the rows 0 1 1, 1 0 1 and 1 1 1. So:
X = ((NOT A) AND B AND C) OR (A AND (NOT B) AND C) OR (A AND B AND C)
Check: each term is 1 in its own row only, so the OR of the three is 1 in exactly those rows. Any expression with the same table is correct. Here a shorter one also works: X = C AND (A OR B). Test it on the table yourself.
For two inputs, the same method on the XOR column 0, 1, 1, 0 gives (A AND (NOT B)) OR ((NOT A) AND B), which you met in the last course.
The program writes the fan rule with Python's and, or and not ideas, and lists every row that switches the fan on.
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.