15110 Summer II 2012

advertisement
15110 Summer II 2012
[adapted from Cortina/von Ronne]
Written Assignment 4 – due Monday, July 30 at 9 am sharp.
Reading Assignment
Read sections 7.1-7.6 in chapter 7, sections 8.1-8.2, and sections 9.1-9.6 of the
textbook Explorations in Computing. It is recommended to read chapter 4 of the
book Blown To Bits, pages 109-137, and chapter 3 as well if you haven’t already.
(Feel free to read the whole chapter if the material interests you.)
Instructions




Type or neatly write the answers to the following problems.
Please STAPLE your homework before you hand it in.
On the first page of your homework, include your name, andrew ID, date, and
the assignment number. Number your pages.
You must hand in your homework at the start of class on the given due date.
Exercises
1. Consider the array [16, 26, 55, 13, 21, 26, 31, 44] which we want to sort using
merge sort:
Merge Sort Algorithm:
1. Sort the first half of the array using merge sort.
2. Sort the second half of the array using merge sort.
3. Merge the two sorted halves to get a sorted array.
a. Show the list after step 1 of the algorithm is completely done.
b. Show the list after step 2 of the algorithm is completely done.
c. Show the merge process of step 3 by tracing this step as shown in the
course slides/notes.
d. Explain why this algorithm is recursive. What is its base case?
2. Consider the 8-bit value 10101011.
a. What is its value if it is interpreted as an unsigned integer? Show your
work.
b. What is its value if it is interpreted as a signed integer? Show your work.
c. Write this value in hexadecimal.
3. As we discussed in class, floating point numbers are stored in binary using
various formats, and one popular format is IEEE-754. How are the following
values stored in binary using IEEE-754 format?
a. The binary value: 110.01011
(HINT: First, convert this into the following form: 1.______ × 2exponent)
b. +infinity
(HINT: You can use the Internet to research this question.)
4. Using the Huffman tree for the Hawaiian alphabet shown on page 186 of your
textbook (and in our course slides):
a. Decode the following Hawaiian word that was encoded into binary using
the given Huffman tree: 0001011000111110000100110.
Once you decode the word, you can use RubyLabs to check your
answer:
>>
=>
>>
=>
>>
=>
>>
=>
>>
=>
include BitLab
Object
f = read_frequencies(:hafreq)
{"K"=>0.106, "W"=>0.009, "L"=>0.044, ... }
tree = build_tree(f)
{ 1.000 { 0.428 { 0.195 { ... } } } }
hc = assign_codes(tree)
{"K"=>001, "W"=>110010, "A"=>10, ... }
encode(INSERT_YOUR_ANSWER_HERE, tree)
0001011000111110000100110
See sections 7.4 and 7.5 for more information on how to build a
Huffman tree, and how to encode and decode messages using RubyLabs.
(NOTE: Be sure you understand what each of the steps are doing above.
Read through the relevant sections for a thorough explanation and try out
the tutorial in the textbook for yourself.)
b. The Hawaiian alphabet has 13 characters (5 vowels, 7 consonants and 1
apostrophe). If we used a fixed-width encoding for characters (i.e. every
character is encoded using the same number of bits), we would need a 4bit code for every character so we could get at least 13 unique codes for
the 13 characters of the Hawaiian alphabet:
A
E
I
O
0000
0001
0010
0011
U
'
H
K
0100
0101
0110
0111
L
M
N
P
1000
1001
1010
1011
W
1100
Go to Popular Hawaiian Words and Phrases. Find a word that would be
longer if encoded with the Huffman tree than with the 4-bit fixed-width
code above. Give the encoding using the Huffman tree and the 4-bit
fixed-width code above to justify your answer.
c. In general, when do you get shorter binary encodings using Huffman
trees?
5.
a. The RGB color for goldenrod is given in hexadecimal as DAA520. Express
the red, green and blue values of this color in decimal. Show your work.
b. An image format takes a 24-bit RGB image and compresses it as
follows. For each pixel, we average the red, green and blue components
and store only this average. This yields a file that is one-third the size of
the original image file. Is this compression algorithm
a lossless or lossy compression technique? Explain.
6. The following boolean expressions can be realized as an electric circuit. Such
circuits can be represented abstractly using logic gates (instead of transistors).
Draw how these expressions would be realized as a computational circuit at the
gate level of abstraction.
(X and Y) or (Y and (not Z))
(not (X or (not Y))) and Z
7. This question deals with generating random numbers using a linear
congruential generator.
a. 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.
b. 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.)
8. Use the rand function in Ruby to solve each of the following tasks:
a. 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.
b. 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.
c. 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.
d. 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.)
9. 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
end
# simulate a roll of a six-sided die
def roll2()
return rand(6) + 1
end
# simulate a roll of a six-sided die
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
10. Consider three programs running simultaneously, each wanting access to the
same three databases. When a program accesses one database, it locks it so
another program cannot use it. Once the program is done using the database, it
unlocks the database. If a program tries to access a database and it is locked, it
waits until it is unlocked. Explain how deadlock can occur for these three
programs.
11. An image is simply an array of arrays of arrays of RGB values. For example,
here is a 3 X 2 image consisting of a red and green pixel in the top row, a blue
and white pixel in the middle row, and two black pixels in the bottom row:
[ [ [255,0,0] , [0,255,0] ],[ [0,0,255] , [255,255,255] ],[ [0,0,0] ,
[0,0,0] ] ]
We can remove the red components of an image using the following function in
Ruby:
def remove_red(image)
num_rows = image.length
num_columns = image[0].length
for row in 0..num_rows-1 do
for column in 0..num_columns-1 do
green = image[row][column][1]
blue = image[row][column][2]
image[row][column] = [0, green, blue]
end
end
return nil
end
It is possible to accelerate the performance for the remove_red function by
modifying it to perform the work on the different pixels concurrently instead of
following the ordering given by the loops. Could the same be done for the
problem of implementing a function that returns an array of n pseudorandom
numbers generated using a linear congruential generator? Why or why not?
12. A sorting network can also be drawn as a wire diagram, as shown below for a
6-element sorting network. Data enter from the left along the 6 horizontal lines
and exit toward the right. Each vertical line represents a comparator with its
inputs coming in from the left and outputs being sent out to the right. When two
data values reach a comparator, they swap positions if the top value of the
comparator is greater than the bottom value of the comparator. Shown here:
a. Trace the operation of this sorting network on the inputs 8, 2, 9, 3, 0, 7
(from top to bottom). For each comparator write down the two numbers
that leave the comparator to the right of the comparator, one per wire.
For example, for the first comparator, it compares the 8 and 2 (which
need to be swapped), sending out 2 along the top wire and 8 along the
bottom wire. For the second comparator, it compares 8 and 9, sending
out the 8 along the top wire and 9 along the bottom wire. Complete the
rest of the wire diagram to show that this network sorts these 6 values.
b. In general, which comparisons in this diagram can be performed
concurrently?
c. How many steps does it take to sort concurrently? How does this
compare to the number of steps needed if we did each comparison
sequentially (i.e. one at a time, non-concurrently)?
Download