=========================preview======================
(COMP152)[2010](f)midterm~2385^_10036.pdf
Back to COMP152 Login to download
======================================================
COMP 152 Fall 2010
Midterm Solutions
1. (a) int read_recursively (ifstream& fin, RingNode*& current){
if (fin.eof())
return 0;

char input;
RingNode * tmp = current;

fin.get(input);
if (input == \n)
return 0;

current = new RingNode;
current->value = input;
if( tmp == NULL ) // first node in the ring

current -> next = current; // self-loop for a single node
else // other nodes in the ring
current -> next = tmp; // point back to the head to form a loop

return 1+read_recursively(fin, current->next);
}

Using static variable, another solution is
//Alternative Solution
int read_recursively (ifstream& fin, RingNode*& current){
static RingNode* head = NULL;

current = head;
if (fin.eof())
return 0;

char input;
fin.get(input);
if (input == \n)

return 0;

current = new RingNode;
current->value = input;
if (head == NULL)

head = current;

return (1+read_recursively(fin, current->next));
}

(b) Note that the following recursive function deletes from the head. One can implement a recursive function which deletes from the tail (not shown).
void delete_recursively (RingNode*& current){

if (current == NULL)
return;

if (current != current->next){ // not a single node yet
RingNode* tmp = current->next;
current->next = tmp->next;
delete tmp;
delete_recursively (current);

}

else{ // a single node left
delete current;
current = NULL;

}
}

2. (a) Polygon::Polygon( const Polygon& mt){ cout << "copy constructor" << endl; if( mt._point == NULL ){
_m=0;

_n=0;

_point=NULL;

}

else{

_m = mt._m;

_n = mt._n;

_point = new double*[_m];

for( unsigned int i=0; i<_m; i++ ){

_point[i] = new double[_n];
for( unsigned int j=0; j<_n; j++ )
_point[i][j] = mt._point[i][j];
}
}
}

(b) Polygon::~Polygon(){ cout << "destructor" << endl; if(_point!=NULL) {
for( unsigned int i=0; i<_m; i++ )
delete [] _point[i];
delete [] _point;
}
}

(c) double* Polygon::FindCentroid() const{ double* centroid = new double[_n]; for( int j=0; j<_n; j++ ){
centroid[j] = 0;
for( int i=0; i<_m; i++ ) {

centroid[j]+=_point[i][j];
}
centroid[j]/=_m;

}
return centroid;
}

(d) default constructor copy constructor 1234 5678 9 10 11 12 destructor 5678 destructor
3. (a) void Polynomial::add(CoefType c, int e){ NodePointer terms = head; NodePointer t; while (terms->next!=NULL){
if (terms->next->data.expo < e){ // find an internal position
NodePointer temp = terms->next;
t = new Node(c,e);
terms->next = t;
t->next = temp;
return;

}

else if (terms->next->data.expo == e){ //clash with existing exponent
terms->next->data.coef += c;
return;

}

else terms = terms->next;
}
t = new Node(c,e); // append to the tail
terms->next = t;

}

(b) Polynomial::Polynomial(CoefType* c, int* e, int num){
NodePointer terms = new Node();
head = terms;
for (int i =0 ;i< num ; i++){

add(c[i], e[i]);
}
}

(c) Polynomial::~Polynomial(){ NodePointer te