15110 SUMMER SESSION ONE - 2013

Programming Assignment 7 - due Thursday, June 13 at 10:30AM using ELECTRONIC HANDIN

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.)

Part I

For Part I of this assignment, you will create a Ruby source file containing a ruby function(s) implementing each of the problems described below. In implementing these functions, you may not use to_s, to_i or any built-in Ruby function/operation that converts between numbers and strings. You may, however, use subscripting with [ ] to convert a character in a string to its ASCII value and use chr to convert an integer to the corresponding character with that ASCII value.

  1. [2 points] A positive integer \(n\) can be converted to a string of 1's and 0's holding that numbers binary represntation using the following algorithm:

    1. Let s be an empty string.
    2. While \(n > 0\) do the following:
      1. Let ch be "0" if \(n\) is even and "1" if \(n\) is odd.
      2. Add ch to the beginning of s.
      3. Divide \(n\) by 2.
    3. Return s.

    Write a function int_to_bin_string(n) (in int_to_bin_string.rb) which takes a non-negative integer n and returns the a string of 1's and 0's

    Note: You will have to figure out how to adjust the algorithm to deal with the case where parameter n = 0.

    Example:

    >> int_to_bin_string(127)
    => "1111111"
    >> int_to_bin_string(4)
    => "100"
    >> int_to_bin_string(3)
    => "11"
    >> int_to_bin_string(0)
    => "0"
    
  2. [2 points] The value of a number represented with a binary string s can be found by iterating over the indices of the string and, for each index i, updating value to be the value of the binary number represented by s[0..i]. An example is shown in the table below:

    sis[i]value
    "11001"0"1"1
    "11001"1"1"3
    "11001"2"0"6
    "11001"3"0"12
    "11001"4"1"25

    Write a function bin_string_to_int(s) (in bin_string_to_int.rb) that takes a non-empty string of 1's and 0's and converts it into the corresponding integer value.

    Example:

    >> bin_string_to_int("0")
    => 0
    >> bin_string_to_int("1")
    => 1
    >> bin_string_to_int("101")
    => 5
    >> bin_string_to_int("0011101110")
    => 238
    

    Hint: Remember that if s is a string s[0] is the ASCII value of the character at position 0 in the s. If s is "Hello", then s[0] != "H", but s[0] == 72 and s[0].chr == "H".

    Another hint: You may wish to look at the h3 function from Lab 7.

  3. [3 points]

    1. Write a function hex_string_to_bin_string(hex_string) (in hex_string_to_bin_string.rb) that converts a non-empty string containing the hexadecimal representation of a number (using the characters "0"-"9" and "A"-"F" as digits) into a string containing that number's binary representation.

      Example:

      >> hex_string_to_bin_string("2")
      => "0010"
      >> hex_string_to_bin_string("A")
      => "1010"
      >> hex_string_to_bin_string("2A")
      => "00101010"
      >> hex_string_to_bin_string("2AC")
      => "001010101100"
      
    2. Write a function hex_string_to_int(s) (in hex_string_to_int.rb) that converts a non-empty string containing the hexadecimal representation of a number and converts it into the corresponding integer value. This function should be implemented by making calls to the bin_string_to_int and hex_string_to_bin_string functions that you defined above.

      Example:

      >> load "bin_string_to_int.rb"
      => true
      >> load "hex_string_to_bin_string.rb"
      => true
      >> load "hex_string_to_int.rb"
      => true
      >> hex_string_to_int("0")
      => 0
      >> hex_string_to_int("4")
      => 4
      >> hex_string_to_int("D")
      => 13
      >> hex_string_to_int("2A7")
      => 679
      

Part II

For part II of this assignment, you will work on a module in the Online Learning Initiative at CMU that will teach you about a new topic called cellular automata. This module will help you learn the basics about cellular automata before we cover this topic in class. The module is designed so we can focus in class on the parts of the topic that you find more difficult, which in turn will hopefully maximize your learning for this topic.

In order to get credit for this part, you must complete the module by the deadline for the programming part. We will not be grading you on how many questions you get right in the module. Instead, we will be grading you on whether you completed the module on time or not.

To access the Cellular Automata module:

  1. Go to this URL: https://oli.web.cmu.edu/.
  2. Click the Sign In link in the upper right corner.

  3. Enter the course key cs4hs2012 into the box labeled "Course Key:" in the shaded section titled Register, and then click the REGISTER button.

  4. At the Sign In page, be sure to click on the "Carnegie Mellon / CMU users sign in here" link to the right of the "Sign In" heading. Authenticate using your Andrew ID and password.

  5. You should see a page that says "My Courses". Look for Principles of Computing - 15110m13 Cortina and click the link Enter Course underneath this heading.

  6. Click the link for Module 1: Cellular Automata to begin the online lesson.

If you have trouble accessing the online lesson, please email your instructor.

Submission

You should have a pa7 directory, containing:

  1. int_to_bin_string.rb
  2. bin_string_to_int.rb
  3. hex_string_to_bin_string.rb
  4. hex_string_to_int.rb

Zip up your directory and upload it using the autolab system.