Assignment 5

advertisement
Julian Corlaci
CSC 255 - 702
Assignment # 5
Chapter 4
20.
a. Take the sub-string and remember the first character.
b. Compare the remembered character from sub-string with the next (of first) character from string 1
c. If characters match then remember the next character from sub-string 1 and compare it to the next
character in string 1.
d. If characters match then repeat step 3 until there are no character left in sub-string. Report True.
e. If characters don’t match, stop and report False.
f. If characters don’t match, compare remembered character to next character in string 1.
g. If characters match, proceed in steps 3-5
h. If characters don’t match, proceed in step 6 until no character are left in sub-string or string1. If this
occurs, report False.
procedure SubStringSearch(FirstString, SecondString)
P = 0;
Success = false;
while (P + length of FirstString) <= (length of SecondString)
and Success = false) do
{ N = 1;
while (P + Nth character in SecondString = character in FirstString) do
(N = N + 1);
if (N = length of FirstString)
then (Success = true)))
P = P + 1}
21.
The body of the loop is everything between "(" and ")".
The initialization step is "assign Current the value 1".
The modification step is "Current <- Last + Temp".
The test step is "Current < 100" in the loop header.
Program prints the Fibonacci sequence < 100:
1 1 2 3 5 8 13 21 34 55 89
22.
1 1 2 3 4 5 8 13 21 34 55 89
23.
procedure MysteryWrite (Last, Current)
if (Current < 100) then
(Temp = Current + Last;
apply MysteryWrite to the values Current and Temp;
print the value assign to Current)
For reverse order the print statement has been moved to the end of the then clause.
24.
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O
First, the middle element is found. The index of the last element is 15, so the middle position is
(1+15)/2 = 8. The 8th element is H. Since J is larger than H, we do a binary search in the upper half
(from position 9 through position 15). The middle element in this part is (9+15)/2 = 12. The 12th
element is L. But, J is smaller than L, so now we apply the binary search to the list between positions
(9 through 11). The middle position here is (9+11)/2 = 10. The 10th position is J, so we stop.
In the case of "Z" (which is not in the list), we again find the middle element (at position 8), which is
H. Since Z is bigger than H, we do a binary search in the upper half (from position 9 to position 15). In
this case, the new middle is position (9+15)/2 = 12, which is L. Z is still bigger than L, so we again do
a binary search, this time from position 13 through position 15. This leads to the interrogation of the
element at location (13+15)/2 = 14, which is N. Z is still bigger than N, so we do another binary search
from location 15 to location 15, the exact element O. Because O is less than Z and we have run out of
elements to consider, we end in failure.
List of items examined by binary search for J and Z is:
Search for "J" ==> H L J (success)
Search for "Z" ==> H L N O (fail)
25.
Sequential Search: least case: 1
worst case : 6000
add = 1+2+3+ ... +5999+6000
average = add/6000 = 18003000/6000 = 3000.5
Binary Search: least case: 1
worst case : 12 <log base 2 6000 < 13 we need to search for 13 times at the worst
add = 1+2+3+ ... +13 = 91
average = add/13 = 91/13 = 7
28.
procedure Euclidean
if (neither X nor Y is 0)
then (divide the larger of X and Y by the smaller;
X = the value of the remainder;
Y = the value of the divisor; and
apply Euclidean to X and Y)
33.
procedure Factorial (Value)
if (Value is 0)
then (Return 1 as the answer)
else (Apply Factorial to (Value - 1),
multiply the result by Value, and
X = the value of this product),
return the number assigned to X as the answer)
43.
Binary Search: least case: 1
worst case: 11 <log base 2 4000 < 12 we need to search for 12 times at the worst
the largest number of entries: 12
Sequential Search: least case: 1
worst case: 4000
the largest number of entries interrogated: 4000
44.
The addition of two n-digit values would require 2n – 1 additions. Thus, addition would be in
THETA(n).
To multiply two n-digit values requires n2 multiplications. Thus, multiplication is in THETA(n2).
Chapter 7
5.
Using a contiguous block of memory cells in which the planes in the three-dimensional array are
stored as consecutive two-dimensional planes in row major order. If each entry requires one memory
cell and the first cell of the block is at address x, then the (i, j, k)th entry will be located at the address
x + (R * C)(i - 1) + C(j - 1) + (k - 1), where R is the number of rows and C is the number of columns.
9.
The list currently spells JANE. To spell JEAN, it should be changed to:
Address Contents
40
N
41
00
42
I
43
40
Address Contents
44
J
45
46
46
E
47
50
Address Contents
48
M
49
42
50
A
51
40
10.
Routine 1 is correct. The problem with Routine 2 is that it changes the value of the pointer field in
PreviousEntry to be that of NewEntry first, and then copy this same value to the pointer field of
NewEntry. Thus, the pointer field in NewEntry points to NewEntry or in other words New Entry
pointing to itself.
12.
Combining two sorted lists into a single sorted list is called merging.
set countS1, countS2 and countD all to zero
while countS1 < lengthS1 and countS2 < lengthS2
if source1 [countS1] < source2 [countS2] then
set dest [countD] to source1 [countS1]
increment countS1
else
set dest [countD] to source2 [countS2]
increment countS2
end while
increment countD
if countS1 = lengthS1 then
while countS2 < lengthS2
set dest [countD] to source2 [countS2]
increment countS2
increment countD
end while
else
while countS1 < lengthS1
set dest [countD] to source1 [countS1]
increment countS1
increment countD
end while
end if
In the case of linked lists, the merge can be performed without producing a third list. The entries can
remain in the same places in memory while their pointers are altered.
13.
Previous = NIL;
Current = value of the head pointer;
while (Current not NIL) do
(Next = value pointed to by Current;
pointer in the current entry = Previous;
Previous = Current;
Current = Next)
head pointer = Previous
Download