15110 Spring 2012 [Cortina/von Ronne]

Problem Set 8 - due Friday, March 30 in class

Reading Assignment

Read sections 8.1-8.2 and 9.1-9.6 of the textbook Explorations in Computing.

Instructions

Exercises

  1. (1 pt) Another way to represent unsigned integers is to use a representation called Binary Coded Decimal. In this representation, each decimal digit of an integer is represented using 4 bits as follows:

    0     1     2     3     4     5     6     7     8     9
    0000  0001  0010  0011  0100  0101  0110  0111  1000  1001
    

    Let ABCD represent a 4-bit binary-coded decimal number as described above. For example, the decimal digit 7 is represented in binary-coded decimal as 0111, so in this case, A = 0, B = 1, C = 1, and D = 1.

    A seven-degment display can be used to display each of the ten decimal digits as shown below.

    Seven Segment Display and 
Examples

    We can define a circuit abstractly below that requires four Boolean inputs A, B, C, and D represent a decimal digit encoded in binary-coded decimal format, and seven Boolean outputs s1, s2, ..., s7 to control each segment of the display:

    Seven Segment Display Unit 
Diagram

    A segment si is lit if si is 1 (true) and not lit if si is 0 (false).

    1. Derive a Boolean formula for s2 that is true (1) if and only if segment s2 is lit.

      Your answer should be of the following form:

      s2 = (________) ∨ (________) ∨ (________) ∨ ...

      where each missing section is a boolean expression with all 4 input variables that is true when ABCD represents a decimal digit that results in the segment s2 of the display being lit. To help you get started, the digit 0 (binary 0000) will light segment s2, so the first expression in the formula above would be

      (¬A ∧ ¬B ∧ ¬C ∧ ¬D)

      since s2 would be true (i.e. 1) if A = 0, B = 0, C = 0, and D = 0. You will need to find all of the other missing expressions for s2.

    2. Derive a simple Boolean expression for s6 that is true (i.e. 1) if and only if the segment s6 is lit. (HINT: Segment s6 is lit for all digits except when the digit is the number 2.)

  2. (1 pt) A simpler adder is called a half adder which only adds 2 bits (X and Y) to produce a sum bit (S) and a carry_out (C) bit, as shown in the following truth table:

     X  Y  |  C  S
    ---------------
     0  0  |  0  0
     0  1  |  0  1
     1  0  |  0  1
     1  1  |  1  0
    

    This adder can be represented abstractly using the following diagram:

    Half Adder Unit Diagram

    1. Give Boolean formulas for C and S as functions of X and Y.

    2. Using two half-adders represented abstractly as boxes and one OR gate, show how to build a full-adder as defined in class. Your picture should have three inputs, X, Y, and Carry_In and two outputs, S and Carry_Out.

  3. (1 pt) When a programmer writes a program in a high-level language (e.g. Ruby), we need an interpreter or a compiler in order to execute the program on a computer. Explain why.

  4. (2 pts) Consider the following MARS program which is shown dumped from memory:

    0000: DAT #0 #6
    0001: DAT #0 #1
    0002: ADD -1 -1
    0003: SUB #1 -3
    0004: JMN -2 -4
    0005: DAT #0 #0
    

    1. Explain clearly what the instructions at address 2 is computing.

    2. Explain clearly what the instructions at address 3 is computing.

    3. Explain clearly what the instructions at address 4 is computing.

    4. What is this program computing overall?

  5. (2 pts) This question deals with generating random numbers using a linear congruential generator.

    1. A linear congruential random number generator is created with a = 5, c = 9, m = 16. What is the period of this random number generator if it starts with a seed of 0? What is the sequence of values generated by this random number generator in one period? Show your work for full credit.

    2. Consider a pseudo random number generator based on the linear congruential method with a = 81, c = 337 and m = 1000. Using irb and RandomLab, generate a set of 20 random numbers from this generator. Try this with several different seeds. Look at each sequence of numbers you get. What is the pattern in each of the sequences that would make you question whether you want to use this generator? (HINT: For each sequence, look at the last digit of each number in the sequence.)

  6. (2 pts) Use the rand function in Ruby to solve each of the following tasks:

    1. Write a Ruby function spin1() that returns an integer and simulates the wheel spinner from the Milton Bradley game of Life. The wheel contains the numbers from 1 to 10.

    2. Write a Ruby function spin2() that returns an integer and simulates the Showcase Showdown wheel from the Price Is Right game show. The wheel contains the multiples of 5 from 5 to 100.

    3. Write a Ruby function roll1() that returns an integer and simulates a backgammon doubling cube. The 6-sided cube contains the values 2, 4, 8, 16, 32, and 64.

    4. Write a Ruby function roll2() that returns a string and simulates a poker die. The 6-sided cube contains the values "A", "K", "Q", "J", "10", and "9". (You should simulate just one die.)

  7. (1 pt) Consider the following code written for a simple dice game. In this game, if the player does not roll "doubles", then the player adds the sum of the dice to their total. If the player rolls "doubles", the player's total gets reset back to 0. At the end of the game, the player wins the number of total points accumulated after 10 rolls. There is a logical error in the program below. Explain what is wrong in this computation below and show how to correct it.

    def roll1()
            return rand(6) + 1     # simulate a roll of a six-sided die
    end
    
    def roll2()
            return rand(6) + 1     # simulate a roll of a six-sided die
    end
    
    def simple_game()
            total = 0
    	for i in 1..10 do
                    if roll1() != roll2() then
                            total = total + roll1() + roll2()
                    else
                            total = 0
                    end
            end
            return total
    end