Csc 115 Final - Dec 1996 - UVic ESS

advertisement
UNIVERSITY OF VICTORIA
EXAMINATIONS DECEMBER 1996
Computer Programming II, CSC 115 F01
NAME: ___________________________________
REG. NO. ___________________
INSTRUCTOR: M. Levy
DURATION: 3 hours
TO BE ANSWERED ON THE PAPER
STUDENTS MUST COUNT THE NUMBER OF PAGES IN THIS EXAMINATION
PAPER BEFORE BEGINNING TO WRITE, AND REPORT ANY DISCREPANCY
IMMEDIATELY TO THE INVIGILATOR.
THIS QUESTION PAPER HAS 14 PAGES PLUS THIS COVER PAGE
All answers are to be written on this examination paper. There are 10 questions in all.
They carry equal weight. Attempt all questions.
You will be given a blank sheet of paper for rough work. Do not hand in the sheet.
Question
1
2
3
4
5
6
7
8
9
10
TOTAL
Mark
CSC 115 F01
Page 1
Question 1 [10 marks]
Suppose that a class named "A" is defined as follows, and stored in a file named "a.h":
class A {
private:
int a;
char b;
void f1(int);
public:
void f2(char x);
int f3(char y);
char c;
};
Below is a "main" program. Some of the statements are legal and some are illegal in C++.
Draw a line through each illegal statement. Do not mark the legal statement(s). One mark
will be deducted from 10 for each incorrect response. Note: this question refers to
compile-time errors only.
#include "a.h"
void main(void){
A x, *xp, *yp; // The declaration IS LEGAL
int k;
xp = new A;
yp = new A(2);
x.f2('a');
x->f2('a');
xp->f1(27);
x.c = 'a';
x.b = 'b';
k = 27*3+x.f3('z');
k = x.c + x.f3(x.c);
k = x.f2('a');
}
CSC 115 F01
Page 2
Question 2 [10 marks]
Consider the following class definitions:
class A {
private:
int x;
char y;
public:
int z;
};
class B: public A{
private:
char w;
int foo(int);
};
A. Supposing that an integer is stored in 4 bytes, a char in 1 byte and a pointer (to
anything) in 4 bytes, how much storage will be allocated to the following variables:
A anA; // STORAGE TAKEN: __________
A *anAp; // STORAGE TAKEN: ________
B aB; // STORAGE TAKEN: ___________
B *aBp; // STORAGE TAKEN: _________
B. Given the following declarations (same class declarations as part A):
A anA;
B aB;
A *anAp;
B *aBp;
indicate which of the following assignment statements are
illegal in C++ by drawing a line through the illegal
assigments.
anA = aB;
aB = anA;
anAp = aB;
aBp = anA;
anAp = new B;
aBp = new A;
CSC 115 F01
Page 3
Question 3 [10 marks]
A. Study the following program and then answer the questions below:
#include <iostream.h>
class A {
public:
A(void){ cout << "hello"; }
void f(void){ cout << "folks";}
};
class B: public A {
public:
B(void){ cout << "bye"; }
void f(void){ cout << "folks";}
};
void main(void){
A *a[3];
int i;
a[0] = new B; a[1] = new A; a[2] = new B;
cout << "****>";
for(i = 0; i< 3; i++)
a[i]->f();
cout << endl;
}
The program, when executed, will print:
ANSWER:_____________________________________________________________
B. Consider the following C++ function.. Each of its statements can be classified in one
of the following ways:
a. FINE
b. COMPILER ERROR
c. PROBABLY OK, BUT PROGRAMMER MUST BE SURE TARGET
HAS SUFFICIENT SPACE ALLOCATED
d. A RUN-TIME ERROR IS LIKELY
e. DANGEROUS: MAY PRODUCE SURPRISING RESULTS
In the space indicated, write a, b, c, d or e, depending on what you think is the best
description of the statement.
void f(void){
char s[100]; char *t;
s = "hello";
strcpy("bye", "hi");
t = "hi";
strcpy(s,"hello");
strcpy(t,"hello");
}
//
//
//
//
//
ANSWER___________
ANSWER___________
ANSWER___________
ANSWER___________
ANSWER___________
CSC 115 F01
Page 4
Question 4 [10 marks]
The class SimpleList is declared and defined below. It is implemented using a linked-list.
All the methods except for Insert and Remove are provided. Complete the definitions of
Insert and Remove. (They appear at the end of the question.)
#include <iostream.h>
class ListNode {
public:
ListNode(){next = 0;}
ListNode(int c, ListNode *n){
value = c; next = n;
}
int value;
ListNode *next;
};
class SimpleList {
private:
ListNode *first, *current;
int size;
public:
SimpleList();
void Insert(int);
// add a new integer after current
void InsertInFront(int); // add to front, reset current
void Remove();
// remove the element after current
void RemoveFirst();
// remove the first element
void ToFront();
// move current to front
void Move();
// move current to next elem.
int Retrieve();
// return the current element
void Update(int);
// change the current element
int Length();
// the length of the list
};
SimpleList::SimpleList(){
first = current = 0;
size = 0;
}
int SimpleList::Retrieve(){
if(size > 0)
return current->value;
else return -1;
}
void SimpleList::RemoveFirst(){
ListNode *tmp;
if(size == 0) return;
tmp = first;
first = tmp->next;
current = first;
delete tmp;
size--;
CSC 115 F01
Page 5
}
QUESTION 4 CONTINUES ON NEXT PAGE
void SimpleList::InsertInFront(int i){
ListNode *nptr = new ListNode(i,first);
first = nptr;
current = nptr;
size++;
}
void SimpleList::ToFront(){ current = first; }
void SimpleList::Move(){
if(current->next != 0) current = current->next;
}
void SimpleList::Update(int){}
int SimpleList::Length(){ return size;}
Complete these two method definitions:
void SimpleList::Remove(){
ListNode *tmp;
if(size == 0) return;
delete tmp;
size--;
}
void SimpleList::Insert(int i){
ListNode *nptr = new ListNode(i,0);
if(size == 0){
} else {
}
size++;
}
CSC 115 F01
Page 6
Question 5 [10 marks]
Here is a simplified version of an old children's song:
Old MacDonald had a farm.
On that farm he had a Pig.
With an Oink-Oink
Old MacDonald had a farm.
On that farm he had a Cow.
With an Moo-Moo
Old MacDonald had a farm.
On that farm he had a Lamb.
With an Baaah-Baaah
Complete the following C++ program that could be used to generate verses of this poem
for any collection of farm animals. HINT: look at the main function at the end of the
question before you start filling in the class declarations and method definitions. One
declaration in the main function must be filled in.
#include <iostream.h>
class Animal{
public:
};
class Pig
public:
{
};
class Cow
public:
{
};
QUESTION 5 CONTINUES ON NEXT PAGE
CSC 115 F01
Page 7
class Lamb
public:
{
};
char *Pig::Sound(){
return "Oink";
}
char *Pig::Name(){
return "Pig";
}
char *Cow::Sound(){
}
char *Cow::Name(){
}
char *Lamb::Sound(){
}
char *Lamb::Name(){
}
void main(void){
______________a[3];
char c;
// These initial animals may change:
a[0] = new Pig;
a[1] = new Cow;
a[2] = new Lamb;
for(int i=0; i<3; i++){
cout << "Old MacDonald had a farm." << endl;
cout << "And on that farm he had a ";
cout << a[i]->Name() << "." << endl;
cout << "With a " << a[i]->Sound();
cout << "-" << a[i]->Sound() << "." << endl;
cout << endl;
}
}
CSC 115 F01
Page 8
Question 6
A. Given the following function:
Boolean Search(int array[], int numElm, int val){
if(numElm < 1) return false;
int i = numElm/2;
cout << array[i] << ' ';
if(array[i] > val)
return Search(array,i,val);
if(array[i++] < val)
return Search(&array[i], numElm-i, val);
return true;
}
What is displayed when the function is called as follows?
int vector[] = {-24,-17,-5,0,19,27,31,34,35,40};
Search(vector, 10, 28);
Answer: ________________________________
B. Answer the following questions using Big-Oh notation:
1. If a binary search tree has n elements, then the efficiency of a search for label X in the
tree is ___________ in the worst case.
2. If a binary search tree has n elements, then the efficiency of a search for label X in the
tree is ___________ in the best case.
3. Linear search, in a list with n elements and without a sentinel is _________ in the
worst case.
4. Linear search, in a list with n elements and with a sentinel is ___________ in the
worst case.
5. Heapsort of an array with n elements is ______________________ in the worst case.
CSC 115 F01
Page 9
Question 7 [10 marks]
A. Complete the following recursive function definition of a recursive function that
returns the number of elements in a array between start and finish that are bigger than X.
int HowMany(int a[], int start, int finish, int X){
int howManyOfRest;
if( _________________________ ) return 0;
howManyOfRest = HowMany(a, ________________________ );
if( _____________________ ) return ___________________ ;
else return ______________________ ;
}
B. Suppose a binary tree is implemented using btreeNodes, declared as follows:
class bTreeNode{
public:
char label;
bTreeNode *leftChild;
bTreeNode *rightChild;
};
Assuming that you can use a function IsLeaf(bt) that will return true if bt points to a
leaf, complete the following definition of a recursive function that counts how many leaves
there are in a binary tree.
int Leaves(bTreeNode *bt){
if( __________________________ ) return 0;
if(IsLeaf(bt)) return ________ ;
return ___________________________________________ ;
}
CSC 115 F01
Page 10
Question 8
A. Draw the Binary Search Tree that would result if you inserted the following elements,
in the order given, starting on the left:
18, 6, 9, 52, 23, 8, 100, 2
B. Give the inorder, preorder and postorder traversal of the following binary tree:
are
boy
oh
are
exams
much
not
they?
fun
ANSWERS:
Preorder: ____________________________________________________
Postorder: ____________________________________________________
Inorder: ______________________________________________________
QUESTION 8 CONTINUES ON THE NEXT PAGE
CSC 115 F01
Page 11
C. Suppose a binary tree with 8 nodes has the following traversals:
inorder: d c h e a b g f
postorder: d h e c g f b a
Draw the binary tree:
CSC 115 F01
Page 12
Question 9 [10 marks]
This function uses a stack of characters to check for balanced parenthesis in a string. It
checks for three kinds of parenthesis: "()", "[]" and "{}". For example:
Balance("(hello [folks]) this") should return true;
Balance("((hello") should return false; and
Balance("(hello [folks)] this") should return false.
Use this declaration of the class stack:
class stack {
public:
stack(int);
void push(char);
void pop();
char top();
int empty();
private: char *array; int tos;
};
Complete the definition of Balance. You can assume that the stack methods above have
been defined - you must use them in Balance. You may assume that type Boolean is
properly defined. Also note: you may not add any additional variable declarations to
Balance. The function "partner" will probably be useful.
char Partner(char c){
switch(c){
case '(': return ')';
case '{':return '}';
case '[':return']';
}}
Boolean Balance(char *s){
stack s(strlen(s));
char current, topChar;
CSC 115 F01
Page 13
Question 10 [10 marks]
Here is the declaration of the class queue:
class queue {
private:
int *array; int max, size, front, rear;
public:
queue(int);
void add(int);
void remove();
int empty();
int full();
int next();
};
A. What is the output of the following program that uses a queue:
void main(void){
queue q(10);
q.add(35);
q.add(42);
q.add(58);
q.remove();
q.remove();
q.add(62);
cout << "****>" << q.next() << endl;
}
ANSWER: ______________________________________________
B. Complete the definitions of the following methods:
void queue::add(int elmt) {
front++;
if (front == max) ______________;
array[front] = elmt;
size++;
}
void queue::remove() {
}
CSC 115 F01
Page 14
C. An array of n elements can be turned into a heap by doing a "siftUp" operation on each
element. Complete this diagram by showing the array elements after completion of
each call to siftUp(). Note that the final entry should be a heap. Confirm this by
drawing the binary tree that corresponds to the final entry in your solution.
Initial Array:
Sift 1st element:
Sift 2nd element:
Sift 3rd element:
Sift 4th element:
Sift 5th element:
Sift 6th element:
6
6
5
5
8
8
9
9
10
10
The binary tree corresponding to the final state of the array is:
- END -
7
7
Download