3 1 + 2 2 + + 1 +

CPS305 Midterm
** NO QUESTIONS WILL BE ANSWERED DURING THIS TEST **
About his test:
 has 14 questions. Check you have them all.
 out of 43 marks
 closed-book with no aids
 code to be written in ANSI C
 only questions answered in pen can be re-evaluated
 given formulas:
n(n+1)/2
= 1+2+3+…n
2n+1-1
= 20+21+22+ … 2n
Full binary tree height (x-1) has 2x-1 nodes
First Name: ___________SOLUTION__________________
Last Name: ___________________________________________
Student ID: ___________________________________________
Lab section/day/time (check one):
□Section 1 Thursday 10am ENG202
□Section 2 Monday 10am ENG202
□Section 3 Wednesday 11am ENG201
□Section 4 Thursday 2pm ENG201
□Section 5 Wednesday 2pm ENG203
□Section 6 Thursday 11am ENG202
1. (2 marks) Define the term ADT (Abstract Data Type).
Anything similar to either of these:
-Data structure plus operations (I-2 of notes)
-A way of packaging data structures and operations into a useful collection whose properties
have been carefully studied. (P10-11 of text)
2. (3 marks) Your textbook states that data structures such as files, lists, arrays and strings are “fictional
entities that we create in our imaginations,” and that “they provide no essential capacity that cannot
be implemented in terms of machine primitives.” Then why do computer scientists use data
structures? Briefly, give 3 very different reasons:
See Page 9-10 of text and I-3 of notes. Any 3 from these, i.e., any 3 from:
more efficient (cheaper) to use components vs. building from scratch (text and notes)
helps us deal with complexity (text and notes)
useful for helping us solve programming problems, providing a stock of ideas (text)
provide building-blocks (notes)
provide information-hiding (notes)
3. (3 marks) Consider the following function X and list L. Suppose a main program calls: X(&L);
Draw L at location /*HERE*/ in the function (just before the call returns.)
void X(NodeType **L) {
NodeType *R, *N, *L1;
L1=*L;
R=NULL;
while (L1 != NULL) {
N=L1;
L1=L1->Link;
N->Link = R;
R=N;
}
*L=R;
/*HERE*/
}
4. (2 marks) A programmer uses information hiding when writing an ADT.
(a) Who is the programmer hiding information from?
the user (of the ADT)
(b) What information is the programmer hiding?
any of these: the implementation, the function code, etc.
5. (5 marks) Consider these 7 recursive terms:
(1) going down
(2) going up
(5) last and all-but-last (6) first and all-but-first
(3) edges-and-center
(7) tail recursion
(4) division-in-half
For each of the following 4 algorithms, list ALL the recursive terms above that apply to it (write
only numbers in the boxes).
SumSquares (m, n)
if (m < n) return m*m + SumSquares(m+1,n)
return m*m
Numbers: 2, 6
SumSquares (m, n)
if (m >= n) return m*m
temp = (m+n)/2 /*integer division*/
return SumSquares(m, temp) + SumSquares (temp+1, n)
Numbers: 4
SumSquares (m, n)
if (m < n) return m*m + SumSquares(m+1, n-1) + n*n
return m*m
Numbers: 3 (also could have 1,2,5,6)
1 mark
SumSquares (m, n)
if (m >= n) return n*n
return n*n + SumSquares (m, n-1)
Numbers: 1, 5
1.5 marks
1 mark
1.5 marks
6. (3 marks) There are several reasons why infinite regress may occur. Give 3 very different reasons.
R-13 of notes gives several. Any 3 from:
forget base case
incomplete base case
never gets to base case (but need to say WHY): Why?
programmer error
incorrect usage given API
not enough resources
Text page 83 also gives many. These are accepted.
7. (3 marks) Draw an fully annotated call tree (including operations) for Fib(4), where Fib is as follows:
3
Fib(x) {
if (x <= 1) return x;
return Fib(x-1) + Fib(x-2);
}
1
Fib(4)
2
Fib(3)
1
+
Fib(2)
1
Fib(1)
1
Fib(1)
0
+
Fib(0)
1
+
1
Fib(2)
1
+
Fib(1)
1
0
NOTE: annotated call trees from notes R-8, and text p. 69-70
0
Fib(0)
0
8. (4 marks) The following algorithm inserts an item into a linked list. Assume:
a. list is circular with a header node, and list items are in ascending order.
b. “&&” is like the C short-circuit operator. i.e., in code (A && B), B is not evaluated when A is false.
c. overflow is impossible
Derive the exact average number of times the “point” moves along the list in order to insert the item;
i.e., count ONLY “point=point->link”. Show your work. Do not give big-O notation.
insert (item, list)
new=GetNode()
new->info = item
point = list
while (point->link != list && point->link->info < item )
point = point->link
new->link = point->link
point->link = new
COUNT THIS ONLY
From Notes LL2
into position 1:
0 times
into position 2:
1 times
…
into position n:
n-1 times
into position n+1:
n times
Average n+1 things: average is:
(0+1 + 2 + … + n )/ (n+1) = n/2.
9. (4 marks) Consider the following algorithm.
e
e
/* calculate b where b and e are both positive integers. To calculate b , call the function as
follows: Power ( b, b, e) */
Power ( Result, b, e)
if e > 1 {
Result = Power ( (Result * b), b, (e-1) )
}
return Result
(a) Give a recurrence relation to express its space complexity. DO NOT SOLVE IT.
S(1) = K S(n) = K + S(n-1)
(b) Give a recurrence relation to express its time complexity. DO NOT SOLVE IT.
T(1) = K1 T(n) = K2 + T(n-1) Note the constants differ here
10. (3 marks) Consider function makeList, which puts n integers into a linked list. Derive the space
complexity of makeList, in bytes. Assume all variables (integers, pointers, etc.) each take 4 bytes of
storage, and a stack frame takes 128 bytes. Show your work. Do not give big-O notation.
typedef struct NodeTag {
int key;
struct NodeTag *link;
} Node;
Node *makeList ( int n ) {
int i;
Node *previous, *tmp;
if ( n < 1 ) return NULL;
previous=NULL;
for ( i = 1 ; i <= n ; i++ ) {
tmp = (Node *)malloc(sizeof(Node));
tmp->key = i;
tmp->link = previous;
previous = tmp;
}
return tmp;
}
-space for variables: n, i, previous, tmp
= 4*4bytes
= 16 bytes
-space for each node is one int + one pointer
= 8 bytes, so space for n nodes is n*8
-space for stack frame = 128 bytes
Total is: 8n + 144 bytes
11. (2 marks) A linked list is implemented using the parallel array approach, as shown below. What are the
items of the list, in order from first to last?
List
4
S
1
T
5
R
-1
U
6
C
3
T
2
U
0
R
3
E
7
S
9
C U U S T T R (Note: Parallel arrays done in notes LL1)
12. (2 marks) Suppose you implement a list (of integers) in C, using an array size 100. Suppose the list
grows and shrinks over time, but always maintains X items, where 65≤X≤85. This array implementation
uses less space (memory) than if the list were implemented in C as a linked list with pointers, assuming
integers and pointers each take 4 bytes of storage. Explain why.
The array always uses 100*4 bytes = 400 bytes.
The linked-list always uses AT LEAST 65*(4+4) bytes
(65 nodes, where each node uses 1 int and 1 pointer)
= 65*8 = 520 bytes.
Since the array uses 400 bytes and the linked-list at least 520 bytes, the array always uses less memory.
(Might also include pointer for list/array itself, but does not change result).
13. (3 marks) We studied an algorithm that used a stack to evaluate postfix expressions. Show how the
stack changes over time when that algorithm is used to evaluate the following expression. Start a new
stack drawing each time the algorithm completes action “pop 2 things from stack”. Draw more stacks
if you need to.
3 1 + 2 2+ + 1+
2
2
4
1
3
4
4
Algorithm was done in notes RLL2
1
8
9
Time →
14. (4 marks) Suppose a Queue is implemented with linked allocation, using the following definitions
and functions:
typedef struct QueueNodeTag {
ItemType
Item;
struct QueueNodeTag *Link;
} QueueNode;
typedef struct {
QueueNode *Front;
QueueNode *Rear;
} Queue;
void InitializeQueue (Queue *Q) {
Q->Front = NULL;
Q->Rear = NULL;
}
int Empty (Queue *Q) {
return (Q->Front == NULL && Q->Rear == NULL);
}
Complete the function below to remove an item from the queue and place it into F.
int Remove ( Queue *Q, ItemType *F ) {
QueueNode *Temp;
if ( Empty(Q) ) {
Underflow();
return 0;
}
*F = Q->Front->Item;
Temp = Q->Front;
Q->Front = Q->Front->Link;
free(Temp);
if (Q->Front == NULL) Q->Rear = NULL;
return 1;
}
1 mark for resetting front; 1 mark for freeing the node; 1 mark for resetting rear if deleted from a 1-item Q; 1
mark for returning an integer.