Вы находитесь на странице: 1из 17

Page

January 26, 2014

German University in Cairo


Fa ulty of Media Engineering and Te hnology
Prof. Dr. Slim Abdennadher
Asso . Prof. Dr. Rimon Elias

Introdu tion to Computer S ien e, Winter Term


2013-2014

Final Exam

Bar Code

Instru tions: Read arefully before pro eeding.

1) Duration of the exam: 3 hours (180 minutes).


2) (Non-programmable) Cal ulators are allowed.
3) No books or other aids are permitted for this test.
4) This exam booklet ontains 16 pages, in luding this one. Five extra sheets of s rat h paper are
atta hed and have to be kept atta hed. Note that if one or more pages are missing, you will
lose their points. Thus, you must he k that your exam booklet is omplete.
5) Write your solutions in the spa e provided. If you need more spa e, write on the ba k of the sheet
ontaining the problem or on the three extra sheets and make an arrow indi ating that. S rat h
sheets will not be graded unless an arrow on the problem page indi ates that the
solution extends to the s rat h sheets.

6) When you are told that time is up, stop working on the test.
Good Lu k!

Don't write anything below ;-)


Exer ise
1
2
Possible Marks
14
19
Final Marks

3
10

4
6

5
8

6
6

7
8

8
16

9
8

95

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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)

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Page

(8+3+6+2=19 Marks)

Exer ise 2

Given the following algorithm


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
}

where absolute(X) al ulates the absolute value of X.


a) What is the output of the algorithm for the following input.
1 3 2 2 1

Draw a tra ing table.


Solution:
n

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

k times, then this element will be printed k 1 times.

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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 operation --> exe uted on e


1 operation --> exe uted (n + 1) time
1 operation --> exe uted (n) times
1 operation --> exe uted on e
1 operation --> exe uted (n-1) times
1 operation --> exe uted (n) times

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.

Total number of operations:

d) Determine the order of magnitude of the algorithm.


Solution:

O(n)

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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:

The number in groups of four bits: 1100

0011

1010

1111

0001

0101

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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

a) Show the binary representation of the de imal number 42.31.


Solution:

101010.010

b) Show the binary number in normalized s ienti notation.


Solution:

.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

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Exer ise 7

Given the following truth table where


are the output variables.
A

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:

X = A B CD + A BCD + ABC D + ABC D


Y = A B CD + AB C D + AB CD + ABCD
Z = A BCD + AB C D
W = A B C D + A B CD + A BC D + A BCD + AB C D + AB CD + ABC D + ABCD

b) What is the fun tionality of the ir uit?


two numbers onsisting of two bits ea h.

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

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

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:

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Page

10

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Page

11

(4+8=12 Marks)

Exer ise 9

Given the following Boolean expression


C D + CB + B D

a) Draw the ir uit using and, or and not gates.


b) Simplify the Boolean expressions using the Boolean algebra. Please mention the applied rules.
x+0=x
x+1=1
x+x=x
x + x = 1
(x ) = x
x+y =y+x
x + (y + z) = (x + y) + z
x(y + z) = xy + xz
(x + y) = x y

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)

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Extra Page

Page

12

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Extra Page

Page

13

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Extra Page

Page

14

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Extra Page

Page

15

Introdu tion to Computer S ien e, Final Exam, January 26, 2014

Extra Page

Page

16

Вам также может понравиться