Академический Документы
Профессиональный Документы
Культура Документы
Final Exam
Bar Code
6) When you are told that time is up, stop working on the test.
Good Lu
k!
3
10
4
6
5
8
6
6
7
8
8
16
9
8
95
Page
(10+2+2=14 Marks)
A robot wants to play a guessing game with numbers against a
omputer. The goal for the robot is to
guess a given number between two numbers as qui
kly as possible. The algorithm should display the
guessed number along with the number of guesses.
Exer ise 1
a) You are asked to write an algorithm that given a number N whi
h is initially hidden for the robot, it
nds this number in the least number of guesses. The algorithm takes as input the hidden number
and the range.
For example: For a given number 14 and a range between 1 and 50 the algorithm should display
the following
Please guess a number between 1 and 50
The robot guesses 25
The
omputer says my number is lower than your guessed one
The robot guesses 12
The
omputer says my number is higher than your guessed one
The robot guesses 18
The
omputer says my number is lower than your guessed one
The robot guesses 15
The
omputer says my number is lower than your guessed one
The robot guesses 14
The robot found the number in 5 guesses
Solution:
get CN
get lo
get hi
print "Please guess a number between " + lo + "and " + hi
set GN to INT((lo + hi)/2)
print "The robot guesses " + GN
set
to 1
while(GN <> CN){
if (CN < GN)
then
print "The
omputer says my number is lower than your guessed one"
set hi to GN - 1
else
print "The
omputer says my number is higher than your guessed one"
set lo to GN + 1
endif
set GN to INT((lo + hi)/2)
set
to
+ 1
print "The robot guesses " + GN
}
print "The robot found the number in" +
+ " guesses"
b) For a given number between 1 and 100, how many guesses at most should be done a
ording to
your algorithm?
Solution:
7 guesses.
) For a given number between 1 and n, nd out an expression that
orresponds to the maximum
number of guesses a
ording to your algorithm?
Solution:
Log2 (n)
Page
(8+3+6+2=19 Marks)
Exer ise 2
5
5
5
5
5
1
2
3
4
5
A[i
1
3
-2
2
1
A[absolute(A[i)
1
2
3
-3
-1
2 1
b) What does the algorithm do for any list of length n
onsisting of elements in the range of 1 to n?
Solution:
The algorithm prints all elements of the list that are dupli ated. Note that if an element is dupli ated
Page
) Find the total number of exe
uted operations. Show your workout.
get n
get A1, ...,An
set i to 1 -----------------------------------------> 1 operation --> exe
uted on
e
while(i =< n) ------------------------------------->
{
if(A[absolute(A[i) > 0) ----------------------->
then
set A[absolute(A[i) = -A[absolute(A[i) --->
else
print absolute(A[i) -------------------------->
endif
set i to i + 1 ---------------------------------->
}
Solution:
get n
get A1, ...,An
set i to 1 ----------------------------------------->
while(i =< n) ------------------------------------->
{
if(A[absolute(A[i) > 0) ----------------------->
then
set A[absolute(A[i) = -A[absolute(A[i) --->
else
print absolute(A[i) -------------------------->
endif
set i to i + 1 ---------------------------------->
}
1 + n + 1 + n + 1 + (n-1) + n = 4n + 2
This holds for all lists however the we
omputed the total number of operations based on the
ase
where all elements of the list have the same value.
O(n)
Page
(10 Marks)
Write an algorithm that given a list of strings representing a series of
oin tosses (head and tail) returns
true if the list
ontains anywhere within it 10
onse
utive o
urren
es of heads.
Exer ise 3
Solution:
get
get
set
set
n
A1...An
i to 1
ount to 0
while (i <= n)
{
if (Ai = "head")
then
set
ount to
ount + 1
if(
ount = 10)
then
print "True"
set i to n
endif
else
set
ount to 0
endif
set i to i + 1
Page
(2+2+2=6 Marks)
Exer ise 4
a) Convert the binary number 1000102 whi
h is in two's
omplement to a de
imal number (base 10).
Show your workout.
Solution:
The number is negative, then get its 2's
omplement: 011101 + 1 = 011110, whi
h is 3010
b) Convert the de
imal number 42110 to a number in base 6. Show your workout.
Solution:
Division
421
70
11
1
Quotient
70
11
1
0
Remainder
1
4
5
1
15416
) Convert the hexade
imal number C3AF 1516 to a number in base 2. Show your workout.
Solution:
0011
1010
1111
0001
0101
Page
(3+2+3=8 Marks)
We would like to store the oating-point number 42.31 in a
omputer that uses 16 bits to represent real
numbers. (10 for the mantissa and 6 for the exponent, both in
luding the sign bit). Show your work as
indi
ated below.
Exer
ise 5
101010.010
.101010010 26
) Show how the binary number will be stored in the 16 bits below.
Solution:
101010010
00110
Sign of
mantissa
1 bit
Mantissa
Sign of
exponent
1 bit
Exponent
9 bits
5 bits
Page
(6 Marks)
Exer ise 6
Assume that our
omputer stores de
imal numbers using 6 bits. Perform the subtra
tion
(22)10 (10)10
using 2's
omplement notation. Give the result of the subtra
tion in de
imal. Show your workout, i.e. all
steps performed.
Solution:
+22: 010110
-22: 101001 + 1 = 101010
+10: 001010
-10: 110101 + 1 = 110110
(-22) + (-10) = 101010 + 110110 = 1 100000
100000 get its 2's
omplement: 011111 + 1 =1000002 = 3210
Exer ise 7
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
and
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
B, C
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
and
X
0
0
0
1
0
0
0
1
0
0
0
0
1
1
0
0
Page
(4+4=8 Marks)
are the input variables and X, Y, Z, and W
0
0
1
0
0
0
0
0
1
0
0
1
0
0
1
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
0
0
0
1
0
1
1
0
1
0
0
1
0
1
1
0
1
0
a) Use the sum-of-produ
ts algorithm to nd the Boolean expressions that des
ribe the output of the
truth table.
Solution:
Hint:
Think about
AB
and
CD
as a representation of
Solution:
Assume that the two numbers are N1 and N2 with the
orresponding binary representation AB
and CD respe
tively.
The
ir
uit
omputes the operation |N12 N22 |.
Page
(8+8=16 Marks)
A parity bit generator is a
ir
uit that outputs one if the number of ones in the input is odd.
Exer ise 8
a) Your task is to
onstru
t a truth table for a 4-bit input parity generator.
Solution:
A
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
1
0
1
0
0
1
1
0
0
1
0
1
1
0
b) Assume that you have already manufa tured 4-bit input parity generators (PBG).
We would like to design a
ir
uit of 16-bit input parity generator. Design a
ir
uit using only 4-bit
input parity generators as shown in the gure.
Solution:
Page
10
Page
11
(4+8=12 Marks)
Exer ise 9
x1 = x
x0 = 0
xx = x
x x = 0
Commutativity
Asso
iativity
Distributivity
DeMorgan's Law
Hint: The
ir
uit of the simplied expression
onsists of only three gates.
xy = yx
x(yz) = (xy)z
x + yz = (x + y)(x + z)
(xy) = x + y
Solution:
=
=
C D + B C + B D
C D + B (C + D)
(Commutativity)
(Distributivity)
=
=
C D + B (C + D)
C D + B (C D )
(x = x)
(DeM organ sLaw)
=
=
C D + (C D ) B
(Commutativity)
(C D + B )(C D + (C D ) ) (Distributivity)
=
=
(C D + B ) 1
C D + B
(x + x = 1)
(x 1 = x)
=
=
(C + D) + B
((C + D) B)
(DeM organ)
(DeM organ)
Extra Page
Page
12
Extra Page
Page
13
Extra Page
Page
14
Extra Page
Page
15
Extra Page
Page
16