1/7
EE 322C Fall 2008 Second MidTerm
Solution
Dr. Ramesh Yerraballi
MW Class
Full Name
:
Solver King
Duration
: 75 minutes
Note: To earn partial credit you have to provide your work and not just give the final
answer
1.
[
25 points
] Answer the following
i.
[
10 points
] For the typical algorithms that you use to perform calculations by hand,
determine the time complexity (bigO) to:
[Oneline answers will suffice if you are correct]
a.
[
3 points
] Add two Ndigit integers
[Note: 1234 + 2341 is addition of two 4digit numbers (N=4); You may assume
that adding two digits takes a constant time]
O(N) : Adding two Ndigit numbers involves adding each
digit in turn with a possible carry in and carry out. N
additions of constant time (c) each gives cN, which is O(N)
b.
[
4 points
] Multiply two Ndigit integers
O(N
2
) Multiplying two Ndigit numbers (A x B say) is the
same as successively adding the multiplicand (A) a
multiplier (B) number of times. This involves N additions
of Nbit numbers, which makes it N
2
total additions.
Of course you could use the more common technique for
multiplication by hand which is:
multiply A by the first
digit in B and add it to the result from multiplying A by
the second digit in B and shifting the result one position
and repeating this process for the third, fourth and N’th
digit. This also takes O(N
2
). The reasoning is as follows:
Each multiplication is a maximum of 9 Ndigit additions (
O(9*N) = O(N)) and the shift is a O(N) operation, so the
cost of this step is O(N); There are N such steps which
gives us a total complexity of O(N
2
).
c.
[
3 points
] Determine whether an Ndigit integer is odd or even
O(1) : Constant time because all you need to look at is the
least significant digit and divide it by 2 to see if the
remainder is 0 or 1. None of the steps depend on N.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
2/7
ii.
[15 points] Consider an array
A
of length
N
, where each element of the array is
a
0
or a
1
. Describe in pseudocode (or java if you prefer) a lineartime algorithm to
find the Longest Contiguous Sequence of
1
’s in the array. If there are no
1
’s in the
array then the Longest Contiguous Sequence is zero.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 NETTLES
 Addition, Big O notation, Computational complexity theory, MCS sum, times does statement

Click to edit the document details