Docsity

ECE 3035 A & B Computing Mechanisms Exam Two Solutions, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035 A & B Computing Mechanisms Spring 2006
4 problems, 5 pages Exam Two Solutions 12 April 2006
1
Problem 1 (5 parts, 28 points) WordSearch Baseline
Part A (4 points) Suppose the letter map is composed of a rectangular grid of cells where each
cell contains a letter A – Z, and letters are equally likely to occur. If the map contains N cells,
how many cells, on average, must be examined to match the first letter of the match word?
(Don’t include cells that are examined to match the remaining three characters.)
(N + 1)/2 searches/map
Part B (4 points) On average, what is the probability that the first character of a match word
does not match a cell being examined? You may express your answer as a fraction.
25/26
L1: addi $01, $01, -1 # I-11
swi 525 # I-12
bne $02, $11, L1 # I-13
add $08, $01, $00 # I-14
div $01, $03 # I-15
mfhi $06 # I-16
mflo $07 # I-17
slt $04, $06, $16 # I-18
… (many more instructions)
Part C (6 points) The code fragment above is the start of the main search loop. Suppose the
letter map has N letters. Write an expression for the total number of instructions executed for all
examined cells that do not match the first character of the match string. Show all work.
(N + 1)/2 x 25/26 x 3 instructions
Part D (6 points) Recall that each dimension of the word map is [4..15] inclusive. What is the
average number of cells (N) in a map? For maximum credit, show all work.
average (4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +13 + 14 + 15) = 9.5
9.5 x 9.5 = 90.25 cells
L2: add $01, $01, $05 # I-31
swi 525 # I-32
bne $02, $12, L3 # I-33
add $01, $01, $05 # I-34
swi 525 # I-35
bne $02, $13, L3 # I-36
add $01, $01, $05 # I-37
swi 525 # I-38
bne $02, $14, L3 # I-39
jr $18 # I-40
L3: add $01, $08, $00 # I-41
jr $31 # I-42
Part E (8 points) Assume $01 = P, $05 = A, $12 = J, $13 = K, and $14 = L and the letter array
is M[]. Write an expression (in terms of P, A, J, K, L, and M[]) for when I-40 is executed.
I-40 executes if (M[P+A] == J) && (M[P+2A] == K) && (M[P+3A] == L)
pf3
pf4
pf5

Partial preview of the text

Download ECE 3035 A & B Computing Mechanisms Exam Two Solutions and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

4 problems, 5 pages Exam Two Solutions 12 April 2006

Problem 1 (5 parts, 28 points) WordSearch Baseline

Part A (4 points) Suppose the letter map is composed of a rectangular grid of cells where each

cell contains a letter A – Z, and letters are equally likely to occur. If the map contains N cells,

how many cells, on average, must be examined to match the first letter of the match word?

(Don’t include cells that are examined to match the remaining three characters.)

(N + 1)/2 searches/map

Part B (4 points) On average, what is the probability that the first character of a match word

does not match a cell being examined? You may express your answer as a fraction.

L1: addi $01, $01, -1 # I- swi 525 # I- bne $02, $11, L1 # I- add $08, $01, $00 # I- div $01, $03 # I- mfhi $06 # I- mflo $07 # I- slt $04, $06, $16 # I- … (many more instructions)

Part C (6 points) The code fragment above is the start of the main search loop. Suppose the

letter map has N letters. Write an expression for the total number of instructions executed for all

examined cells that do not match the first character of the match string. Show all work.

(N + 1)/2 x 25/26 x 3 instructions

Part D (6 points) Recall that each dimension of the word map is [4..15] inclusive. What is the

average number of cells ( N ) in a map? For maximum credit, show all work.

average (4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 +13 + 14 + 15) = 9.

9.5 x 9.5 = 90.25 cells

L2: add $01, $01, $05 # I- swi 525 # I- bne $02, $12, L3 # I- add $01, $01, $05 # I- swi 525 # I- bne $02, $13, L3 # I- add $01, $01, $05 # I- swi 525 # I- bne $02, $14, L3 # I- jr $18 # I- L3: add $01, $08, $00 # I- jr $31 # I-

Part E (8 points) Assume $01 = P , $05 = A , $12 = J , $13 = K , and $14 = L and the letter array

is M[]. Write an expression (in terms of P , A , J , K , L , and M[] ) for when I-40 is executed.

I-40 executes if (M[P+A] == J) && (M[P+2A] == K) && (M[P+3A] == L)

4 problems, 5 pages Exam Two Solutions 12 April 2006

Problem 2 (3 parts, 24 points) Activation Frames

Consider the following C fragment:

int Foo() { int Limit = 150; int StopIndex = 2; int Result = 0; Int Array[] = {20, 30, 101, 35, 80}; int Baz(int Limit, int *StopIndex, int Array[]);

Result = Baz(Limit, &StopIndex, Array); } int Baz(int Max, int *End, int Values[]) { int X, Sum = 0;

for (X = 0; X <= *End; X++) Sum += Values[X]; *End += 1; if (Sum > Max) return 1; else return 0; }

Part A (10 points) Describe the current state of the stack just before Baz returns. Fill in the unshaded

boxes to show Foo and Baz’s activation frames. Include a symbolic description and the actual value (in

decimal). Do not include values for return addresses. Label the frame pointer and stack pointer.

address description value (^9900) RA of Foo’s caller (^9896) FP of Foo’s caller SP, Foo’s FP 9892 (^) return value (^9888) Limit 150

(^9884) StopIndex 3

(^9880) Result 0

(^9876) Array[4] 80

(^9872) Array[3] 35

(^9868) Array[2] 101

(^9864) Array[1] 30

(^9860) Array[0] 20

(^9856) RA of Foo

(^9852) FP of Foo 9892

(^9848) Max (Limit) 150

(^9844) &End (&StopIndex) 9884

(^9840) Values (Array) 9860

Baz’s FP 9836 (^) Baz’s RV 1

(^9832) X 3

SP 9828 (^) Sum 151

9824

4 problems, 5 pages Exam Two Solutions 12 April 2006

Problem 3 (3 parts, 22 points) Hash Tables

typedef struct Link { L-1 Memory

int Key; (^) L-2 Address Data int Value; L-3 6000 struct Link *Next; (^) L-4 6004 } Link; (^) L-5 6008 L-6 6012 typedef struct { (^) L-7 6016 Link *Buckets[NUMBUCKETS]; (^) L-8 6020 7084 Link *FreeLinks; (^) L-9 6024 int Size; L- ... } HashTable; (^) L-11 7056 6 L-12 7060 12 void Remove(HashTable *HT, int Key) { (^) L-13 7064 0 int Index; (^) L- ... Link *ThisLink; (^) L-15 7084 3 Link **TrailingPointer; (^) L-16 7088 17 int Hash(int Key); (^) L-17 7092 7120 void Print_Next_Link(Link *Next); (^) L- ... Index = Hash(Key); (^) L-19 7120 5 ThisLink = HT->Buckets[Index]; (^) L-20 7124 29 TrailingPointer = &(HT->Buckets[Index]); (^) L-21 7128 7056 L- ... while(ThisLink != NULL && ThisLink->Key != Key) { (^) L- TrailingPointer = &(ThisLink->Next); (^) L- ThisLink = ThisLink->Next;} (^) L- if (ThisLink != NULL) { (^) L- *TrailingPointer = ThisLink->Next; (^) L- ThisLink->Next = HT->FreeLinks; (^) L- HT->FreeLinks = ThisLink; (^) L- HT->Size -= 1;}}} (^) L-

Part A (10 points) Assume NUMBUCKETS = 7, Key = 5, HT = 6000, and Index = 5. Given

the snapshot of memory addresses shown to the right, what is the value of the following variables at each

of the indicated locations in the code:

Variable at L-22 at L-26 at L-

ThisLink 7084 7120 7120

TrailingPointer 6020 7092 7092

Part B (6 points) What list operation does line L-27 perform?

This line splices out the removed Link object from the bucket list.

Part C (6 points) Removing which lines of code would cause a memory leak? Explain why.

L-28 and L-29 (i.e., not pushing the removed link onto the free list)

The removed link could no longer be accessed.

4 problems, 5 pages Exam Two Solutions 12 April 2006

Problem 4 (4 parts, 26 points) Short Answer

Part A (12 points)

#define NUMBUCKETS 4

typedef struct Link { int Key; int Value; struct Link *Next; } Link;

typedef struct { Link *Buckets[NUMBUCKETS]; Link *FreeLinks; int Size; } HashTable;

int main(int argc, char *argv[]) { HashTable HT; FILE *FP; int NumVals, Key, Value; void Initialize_Hash_Table(HashTable *HT); void Insert(HashTable *HT, int Key, int Value); int Lookup(HashTable *HT, int Key); void Remove(HashTable *HT, int Key); void Print_Hash_Table(HashTable *HT);

How large (in words) is a Link object? three

How large (in words) is a HashTable object? six

In what space is HT allocated (static, heap, or stack) stack

In what space are Links allocated (static, heap, or stack) heap

How large (in words) is the caller generated activation frame for Lookup()? three

How large (in words) is the caller generated activation frame for Insert()? three

Part B (6 points) Consider a hash table implemented as an array of five buckets, each bucket

containing an unsorted list of items. Suppose the hash table contains 555 entries total. Suppose

25% of the lookup keys are not found in the hash table. What is the average number of searches

per hash table lookup? Show work.

555 entries / 5 buckets = 111 entries/bucket; (111 + 1)/2 = 56 searches (found)

0.75 x 56 + 0.25 x 111 = 42 + 27.75 = 69.75 Searches

Part C (4 points)

In C, the function namespace is (global, hierarchical) global

In C, the data namespace is (global, hierarchical) hierarchical

Part D (4 points) List three differences between the heap and static data storage.

1. Heap data is dynamically allocated; static data is statically allocated.

2. Heap data is explicitly managed by application program; static data is unmanaged.

3. Heap data can be reused through program; static data is permanent.