# Logic Continued

Before we move on I want you to practice logic problems. Look at the following logic circuit: We are going to construct a truth table.
__A__ __B__ __C__ __X__ __Y__ __Z__ _Out_
0 0 0 . . . .
0 0 1 . . . .
0 1 0 . . . .
0 1 1 . . . .
1 0 0 . . . .
1 0 1 . . . .
1 1 0 . . . .
1 1 1 . . . .
We are going to use the intermediate points X, Y and Z to find the output 'out' as a function of the inputs A,B and C.
1. Look at X. It is at the output of an 'and' gate. The inputs to this gate are connected to the inputs A and B. Deduce the values that go in the X column: you only have to look in the A and B columns and see where A AND B are true.
2. Look at the Y. This is connected to the output of a NOT gate that is connected from the input c. So we can work out the values for the Y column.
3. Z is the output of an OR gate that hass as input B abd Y. So we can work this out by 'Oring'B and Y.
4. Finally OR X and Z.

## Three Agree Circuit

Lets practice what we learned last week. We need a circuit to indicate that three inputs all agree with each other. The truth table is:
__A__ __B__ __C__ _Out_
0 0 0 1
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 0
1 1 1 1
Please write a logical expression for it and design a logic circuit.

## Feedback

The circuits we have looked at can all be written with signals moving from left to right across the page. Nothing can loop back. The Software from the text book will only deal with this situation. In real life there is nothing to stop us from wiring an output back into an input. Sometimes this makes imposible demands on the circuit. Look at this NOT gate If the output is true then this is applied to the input, so the output becomes false. What will actually happen depends on deatils of the circuit. Its probably not a useful circuit. On the other hand look at the following two NOR gates. NOR gates only give a true output when both inputs are false.
__A__ __B__ __NOR__
0 0 1
0 1 0
1 0 0
1 1 0 Can we understand the behaviour of this fishy looking circuits? We have two inputs A and B. Lets look at various cases.
1. Case 1:Suppose that both A and B are true (1). If any of the inputs of a NOR gate are 1 then the output is 0. So putting 1 on both A and B means both outputs are 0. No ambiguity. It turns out that since the outputs are 0 the 'other inputs 'X' and 'Y' are at 0 but this has no effect.
2. Case 2: Suppose input A is 1, but input B is 0? Start with the top gate. We know that there is a 1 value on the A so we don't need to know what X is to know that the output Out1 is 0. Now Out1 is fed into Y, and B is 0. When a NOR gate has both inputs 0 then the output is 1, so output Out2 is 1 as is X but this has no effect. There is no ambiguity. If A is 1 and B is 0 then out1 is 0 and out2 is 1.
3. Case 3: Input A is 0 and Input B is 1. This is the mirror opposite of case 2 and the result, for the same reasons the result is out1 is 1 and out2 is 0.
4. Case 4: Both A and B are 0 and we are going to GUESS that X is 0. If A and X are both 0 then the out1 is going to be 1. As this 1 is applied at Y out2 is going to be 0. That is good because we guessed that X was 0 and it seems to agree. Everything is stable. A and B are 0 and we have out1 1 and out2 0.
5. Case 5: Both A and B are both zero and we are going to GUESS that X is 1. In this case out 1 will be 0. Now both the inputs Y and B to the lower gate are zero. So out2 is 1. Which is as it should be because we guessed X was 1.
Case s 4 and 5 are stable, but in both cases the inputs are both 0. If we got to this state from case 2, out1 would stay 0 and out2 stay1. If we got to it from Case 3 out1 would stay 1 and out2 stay0. We have discovered MEMORY. Feed back enables circuits that have two stable states. These are called 'flip flops' and they are really one bit memories.

Annotate the diagrams to show the 5 cases:

## Case 1 ## Case 2 ## Case 3 ## Case 4 ## Case 5 # C++ Programming 1

## Learning to Program

There are four phases in learning to program:
1. Phase 1: Housekeeping information. This is very boring. I'm sorry. It is about the mechanics of putting a program together."Why are we learning this stuff?"
2. Phase 2: First Steps: You write your first programs but the tasks that they perform are trivial. You spend half an hour writing a program to add two numbers together. You are programming but it seems like a lot of work. "Why bother?"
3. Phase 3: You write programs that require that simple operations are repeated hundreds, or perhaps millions of times. You would not want to do these things longhand but what you are doing is still geeky.
4. Phase 4:Programs build on existing blocks of code. Both the programmer and the user are thinking mainly about real world problems ("domain knowledge") rather than the rules of C++. When you reach phase 4 you are engaged in a creative act.

## Welcome to Phase 1/

C++ is an unambiguous way of telling the computer what to do. When You write a program in C++ it is called "source code". Source code must be turned into "executable code" for it to run on a computer. This conversion process has 2 parts: "compiling" and "linking". Linking joins the little bit of code that you just wrote to the bits you wrote and compiled previously and to various standard code modules that do routine tasks. These tasks involve talking to the operating system, which is necessary to do input and output. Go to the C++ compiler in your lab Software and load example1.cpp // File: example1.cpp

#include

void main()
{ const int first = 2;
const int second = 4;

int sum;

sum = first + second;
cout << sum << endl;
}

Learn how to compile and then execute the program. Now I want you to (using pen or pencil) annotate the program so you understand what each statement does.
1. Any line that starts with //, or the remainder of a line that contains // is a comment. It is there for the human reader and completely ignored by the compiler.
2. The program uses perticular input output routines found in a file called iostream.h. Before the start of the program proper there is an "include statement" that tells the linker to join our program file to precompiled software in iostream.h. If e.g. we were writing for an iphone there would be a different file included and we would be using different input output routines.
3. The main part of the program is called the "main" proceedure. It starts on the line containing the words void main(). This will vary slightly and annoyingly between compilers. The code for this proceedure is enclosed within braces{}. Find the closing brace and annotate it (circle and label).
4. Code consists of "statements". Statements are usually put one oper line, but it doesn't have to be so. Statements are separated by semicolons;. Circle all the semicolons that separate statements.
5. One of our first chores in writing code is to tell the compiler what memory we want to use, in particular what type of data we want to store and what type the data is. In this program we 'declare' two 'variables' called 'first' and 'second'. They are both of type 'int' that is to say they store whole numbers and they are given values of 2 and 4. These are made 'constant' which means we cant change the value (number) that is stored in them.
6. A third variable is declared which is not constant but also an integer. Annotate the declaration of variables.
7. The equals sign has a remarkable meaning in most computer languages. It means "work out the expression on the right hand side and put the answer in the variable names on the left hand side. Whereever there is a variable name on the right hand side use the number that is stored in the variable. This is called an "assignment statement".
8. Circle and lable the assignment statement in this program. Where will the new value be placed? What calculation was performed?
9. This program outputs the value using a routine called cout. Label the output statment.

## Modifying the program

1. Add a comment line at the top of the program to say they you modified the program and on what date.
2. Add comment lines to explain the declaration of variables (this is called a variable dictionary (Good Form!).
3. Add a comment after the closing brace so it looks like: }//end of main function
4. After the declaration of variable add a new cout line Saying its your first program and on a new line giving your name (use endl).
5. How does cout work? what does endl do?
6. Change all the variables from int to double. Assign 2.0 and 5.0 as initial values to first and second.
7. Change the assignment statement to sum=(first + second)/2;
8. Change the (original) cout staement to show the values of the first and second and state that you are averaging them. <
9. In your own words explain how cout works in terms of string in quotes, variable names and endle. What is the roll of the << sign?
Next week Programming proper! Think about what sort of project you would like to do. (Watch this space for ideas).

Home Page of RDScience