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 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.
>> 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
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 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 points] Write a MARS program in the file addup.txt that computes the sum of the numbers 1 through 10.
You should have a pa8 directory, containing:
Zip up your directory and upload it using the autolab system. (The autolab system will accept submissions beginning on Friday until the deadline Tuesday night.)