Exam1 Sol.

CSIT 221
Solution of Exam I
Fall 03
Name:
1) (27 points) Decide whether the following are true or false. If the statement is true, write true on the left.
If it's false, write false on the left.
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
k)
l)
m)
n)
o)
p)
q)
r)
(True) A static method/data member of a class can be called without creating an object of the
class.
(True) A const method cannot change the data members of the class.
(True) The precedence or associativity of an operator cannot be changed by overloading.
(True) You can not create new operators. You can overload only existing operators (not all of
them).
(True) The time complexity (running time) of binary search of a one-dimensional array of N
elements is O(log2 N).
(True) The insertItem and deleteItem methods for UnsortedType with arrays (unsorted lists as
arrays) do not work for SortedType with arrays (sorted lists as arrays).
(True) The retrieveItem method for UnsortedType with arrays (unsorted lists as arrays) works
for SortedType with arrays (sorted lists as arrays), but it can be replaced by a more efficient
version.
(True) The only literal pointer constant is 0 (which is the same as NULL).
(True) If array is an array, then array and array[0] have the same address.
(True) If pointer is a pointer, then (*pointer).name can be written as pointer -> name.
(True) The life time of a dynamic variable is the period from creating it using new to deleting
it using delete.
(True) If you have a class called MyClass and if you declare array to be an array of type
MyClass, then MyClass must have a default constructor. If not, then there will be an error.
(True) If pointer is a pointer pointing to a dynamic variable/array, then delete pointer does not
delete the pointer. It deletes the variable/array the pointer is pointing to.
(False) The destructor for QueueType as a dynamic array is O(N), where N is the size of the
queue.
(False) The destructor for QueueType as a linked structor is O(1).
(False) Destructors can have parameters and cannot be overloaded.
(False) A dynamic variable has no name and can be addressed directly.
(True) If pointer is a pointer, then *pointer.name is interpreted as *(pointer.name).
2) (15 points) What is the time complexity (running time) of the following (write the answer in terms of
N, where N is the size of the input)? Use the big-O notation. Write the answer on the left.
a) O(1) insertItem for UnsortedType as an array.
b) O(N) deleteItem for UnsortedType as an array.
c) O(1) insertItem for UnsortedType as a linked structure (i.e. unsorted linked list).
d) O(N) makeEmpty for UnsortedType as a linked structure (i.e. unsorted linked list).
e) O(N) deleteItem for UnsortedType as a linked structure (i.e. unsorted linked list).
f) O(N) The destructor for UnsortedType as a linked structure (i.e. unsorted linked list).
g) O(N) insertItem for SortedType as an array.
h) O(N) deleteItem for SortedType as an array.
i) O(N) retrieveItem for UnsortedType as an array.
j) O(N)
int i; factorial = 1;
for (i = 1; i < N; i++)
factorial = factorial * i;
cout << factorial << endl;
1
3)
(6 points) Let
int *p, a = 8;
long *q, b = 3;
p = &a; q = &b;
Decide whether each of the following is valid or not. If the statement is valid write "valid" on the left.
If it is not valid write "not valid" on the left.
a) (Invalid) q = &a;
b) (Valid) *q = *p;
c) (Invalid) q = p;
4) (28 points) For the following, use templates. Write the implementation part only and include the
templates line that proceeds the method. Do not make any changes on the specification part of the
classes we presented in class. Do ONLY one part from each group. If you do more than one part from
each group, then the first one will be graded even if it's incomplete. Also, cross out the solutions that
you don't want me to grade. Write the programs on the extra paper you'll get from me.
Group 1:
a)
Write the insertItem method for UnsortedType as a linked structure. Do not make any restrictions
on the method and use the needed exception classes that we did in class.
Solution:
template <typename ItemType>
void UnsortedType<ItemType>::insertItem(ItemType item)
{
if (isFull())
throw FullList();
bool found;
retrieveItem(item,found);
if (found)
throw InTheList();
NodeType<ItemType> *location;
location = new NodeType<ItemType>;
location -> info = item;
location -> next = listData;
listData = location;
length++;
}
b) Write the deleteItem method for UnsortedType as a linked structure. Do not make any restrictions
on the method and and use the needed exception classes that we did in class.
Solution:
template <typename ItemType>
void UnsortedType<ItemType>::deleteItem(ItemType item)
{
bool found;
retrieveItem(item,found);
if (!found)
throw NotFound();
NodeType<ItemType> *location = listData;
NodeType<ItemType> *tempLocation;
if (item == listData -> info)
{
2
tempLocation = location;
listData = listData -> next;
}
else
{
while (item != (location -> next) -> info)
location = location -> next;
tempLocation = location -> next;
location -> next = (location -> next) -> next;
}
delete tempLocation;
length--;
}
Group 2:
c)
Write the Enqueue method for QueueType as a linked structure.
Solution:
template <typename ItemType>
void QueueType<ItemType>::Enqueue(ItemType newItem)
{
if (isFull())
throw FullQueue();
else
{
NodeType<ItemType> *location;
location = new NodeType<ItemType>;
location -> info = newItem;
location -> next = NULL;
if (rear == NULL)
front = location;
else
rear -> next = location;
rear = location;
// cout << " front = " << front -> info << endl;
// cout << " rear = " << rear -> info << endl;
size++;
}
}
d) Write the Dequeue method for QueueType as a linked structure. Dequeue must also return the
deleted item.
Solution:
template <typename ItemType>
void QueueType<ItemType>::Dequeue(ItemType& item)
{
if (isEmpty())
throw EmptyQueue();
else
{
NodeType<ItemType> *tempPtr;
item = front -> info;
tempPtr = front;
front = front -> next;
if (front == NULL)
rear = NULL;
3
delete tempPtr;
size--;
}
}
Group 3:
e)
Write the Push method for StackType as a linked structure.
Solution:
template <typename ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if (isFull())
throw FullStack();
else
{
NodeType<ItemType> *location;
location = new NodeType<ItemType>;
location -> info = newItem;
location -> next = topPtr;
topPtr = location;
size++;
}
}
f)
Write the Pop method for StackType as a linked structure.
Solution:
template <typename ItemType>
void StackType<ItemType>::Pop()
{
if (isEmpty())
throw EmptyStack();
else
{
NodeType<ItemType> *tempPtr;
tempPtr = topPtr;
topPtr = topPtr -> next;
delete tempPtr;
size--;
}
}
Group 4:
g) Write the destructor for QueueType as a dynamic array.
Solution:
template<typename ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}
h) Assume the elements of UnsortedType as a linked structure are printable. Write a void method
called printList, with no parameters, to print the elements of the list.
Solution:
4
// In this method, we assume ItemType is a printable type.
template <typename ItemType>
void UnsortedType<ItemType>:: printList() const
{
NodeType<ItemType>* location = listData;
while (location != NULL)
{
cout << location -> info << "\t";
location = location -> next;
}
cout << endl << endl;
}
6) (12 points) Let a = 77, b = 88, and c = 44, be variables of type int and let p1, p2, p3 be pointers of type
int, and suppose that the memory addresses reserved for them are as follows:
Find the output of the following:
p1 = &a;
p2 = &b;
cout << p1 << endl << &p1
<< endl << *p1 << endl ;
p3 = p2;
cout << p3 << endl;
*p1 = *p2;
cout << p1 << endl << *p1 << endl;
Answer:
006AFDF4
006AFDE8
77
006AFDF0
5
006AFDF4
88
7) (8 points) Find the output of the following for each of the following input.
#include <iostream>
#include <cmath>
using namespace std;
float f(float,float);
class ExceptionA
{
};
class ExceptionB
{
};
class ExceptionC
{
};
int main()
{
float x, y, z;
cout << "Enter x followed by y: ";
cin >> x >> y;
try
{
z = f(x,y);
cout << z<< endl;
}
catch(ExceptionA)
{
cout << "Division by zero.\n";
}
catch(ExceptionB)
{
cout << "A negative denominator is not allowed.\n";
}
catch(...)
// Line *1
{
// Line *2
cout << "There is something wrong.\n";
// Line *3
}
// Line *4
return 0;
}
float f(float x, float y)
{
if (y == 0)
throw ExceptionA();
else if (y < 0)
throw ExceptionB();
else if (y > 1000)
throw ExceptionC();
return (x / sqrt(y));
}
a)
0
0
6
Answer:
Division by zero.
b) 0
7000
Answer:
There is something wrong.
8) (4 points) Find the output of the program in the previous question if lines Line *1, Line *2, Line *3, and
Line *4 are deleted and if the input is
0 9000
If there is an error that results in the termination of the program, write down what the error is (of course in
this case there should be no output).
Answer:
Abnormal termination of the program because the thrown exception was not caught.
Stuff You May Need:









Data members of UnsortedType/SortedType as a static array:
int length; // Number of items stored in the list.
int currentPos; // Index of the last element accessed by getNextItem.
ItemType info[MAX_ITEMS];
Data members of StackType as a static array
int top; // The index of the top element of the stack.
ItemType items[MAX_ITEMS];
Data members of QueueType as a dynamic array
int r; // Index of the next available cell of the queue
int f; // Index of the next item to be removed
int N; // Max. # of items of the queue + 1
ItemType* items; // Pointer to a dynamic array
Data members of StackType as a dynamic array
int top; // The index of the top element of the stack.
int maxStack; // Max. # of stack items
ItemType* items; // Pointer to a dynamic array
Definition of struct NodeType
struct NodeType
{
ItemType info;
NodeType *next;
};
Data members of StackType as a linked structure:
int size; // The number of elements in the stack.
NodeType<ItemType>* topPtr; // Pointer to the top element of the stack.
Data members of QueueType as a linked structure:
int size; // The number of elements in the Queue.
NodeType<ItemType>* front; // Pointer to the front element of the Queue.
NodeType<ItemType>* rear;
// Pointer to the rear element of the Queue.
Data members of UnsortedType as a linked structure:
NodeType<ItemType> *listData, *currentPos;
int length;
Exception Classes for UnsortedType:
class FullList
{
// An exception class for the case when the list is full.
// This class is used by insertItem.
};
class NotFound
{
// An exception class for the case when an element is not in the list.
// This class is used by deleteItem.
7


};
class InTheList
{
// An exception class for the case when the item to be inserted is
// already there. This class is used by insertItem.
};
class LastElement
{
// An exception class for the case when the element at currentPos is
// the last element in the list. This class is used by getNextItem.
};
Exception Classes for StackType:
class FullStack
{
// Exception class used by Push when the stack is full.
};
class EmptyStack
{
// Exception class used by Pop and Top when the stack is empty.
};
Exception Classes for QueueType:
class FullQueue
{
// Exception class used by Enqueue when the queue is full.
};
class EmptyQueue
{
// Exception class used by Dequeue and Front when the queue is empty.
};

The header part of some methods you may need:
void insertItem(ItemType item)
void deleteItem(ItemType item)
void Push(ItemType item)
void Pop()
void Enqueue(ItemType item)
void Dequeue(ItemType& item)
8