# 15110 Fall 2012 [Touretzky/Kaynar]

## Programming Assignment 8 - due Tuesday, October 23

Note: You are responsible for protecting your solutions to these problems from being seen by other students either physically (e.g., by looking over your shoulder) or electronically. (More information can be found in the instructions for Programming Assignment 2.)

1. [1 point] In the file full_adder.rb, write a Ruby function full_adder(a,b,cin) that takes three boolean values as input and prints the result of every gate in the full adder circuit below. Note that ^ is the Ruby xor operator. Use && for logical and and || for logical or; do not use the and and or keywords.

Example output:

>> full_adder(true,false,true)
XOR gate 1 outputs true
XOR gate 2 (S) outputs false
AND gate 3 outputs true
AND gate 4 outputs false
OR gate 5 (Cout) outputs true
=> nil

2. [2 points] In file truth_table_and.rb write a Ruby function truth_table_and that prints the truth table for the conjunction of two Boolean variables x and y . The function does not take any arguments and returns nil. We started writing the code for the function for you. You need to complete the parts indicated by the inlined comments.

def truth_table_and
puts "x\ty\tx and y"
for x in [0,1] do
for y in [0,1] do
# you need to complete this part
end
end
#you need to complete this part
end


Example usage:

 >> truth_table_and
x     y    x and y
0     0       0
0     1       0
1     0       0
1     1       1
=> nil

3. [3 points] Visit the lecture slides for Unit 8B and make sure that you understand the description of a full adder on slide 5. Now write your own Ruby function table_full_adder in file table_full_adder.rb to compute and print the truth table on that slide, using the formulas shown. Your code should use nested for loops. Hint: Notice that the carry out bit $$C_{out}$$ and the sum bit $$S$$ are computed from A, B, and the carry in bit $$C_{in}$$. Think about what this implies for your loop structure.

4. [4 points] Write a MARS program in the file addup.txt that computes the sum of the numbers 1 through 10.

### Submission

You should have a pa8 directory, containing: