CP Final Spring 2018 - Solution
CP Final Spring 2018 - Solution
Solution:
(a)
A() Called.
A() Called.
--------------
~A()Called for 5 .
~A()Called for 5 .
~A()Called for 10 .
(b)
*ptr = 10
(c)
ABC() called for 50
c = 50
a = 10
b = 10
~XYZ() Called.
10
(d)
Test
Check
Test
~Vehicle() called.
~Vehicle() called.
~Vehicle() called.
Step 1. If a word anywhere in the text is preceded by a comma (i.e. the comma comes before the word), find
all occurrences of that word in the text and put a comma before each of those occurrences, except in the case
where such an occurrence is the first word of a sentence or already preceded by a comma.
Step 2. If a word anywhere in the text is succeeded by a comma, (i.e. the comma comes after the word) find
all occurrences of that word in the text, and put a comma after each of those occurrences, except in the case
where such an occurrence is the last word of a sentence or already succeeded by a comma.
Step 3. Apply Steps 1 and 2 repeatedly until no new commas can be added to the text.
Note that the text must always remain in the standard format, which follows these rules:
• The text begins with a word.
• Between every two words in the text, there is either a single space, a comma followed by a space, or
a period followed by a space (denoting the end of a sentence and the beginning of a new one).
• The last word of the text is followed by a period with no trailing space.
• The first letter of each sentence is capitalized.
Example
Consider the text: Please sit spot. Sit spot, sit. Spot here now here.
(note that this text is in the standard format)
Step 1 produces: Please, sit spot. Sit spot, sit. Spot here now here.
Step 2 produces: Please, sit spot. Sit spot, sit. Spot, here now here.
Step 1 produces: Please, sit spot. Sit spot, sit. Spot, here now, here.
STOP, as no more commas can be added to text.
Implement Sprinkler’s Algorithm in a function called SprinkleCommas. The function accepts a dynamic
character array of exactly the same size as the text. Your function updates this same array and makes sure
that at the end the size of the array is still exactly the same as the size of the text.
Solution:
public:
PascalTriangle(int nrows){
//implement this function
}
~PascalTriangle(){
//implement this function
}
};
(a) Implement the constructor PascalTriangle(int nrows) to compute the triangle for n rows.
For example, for a triangle such as in the picture above n=5, meaning it has rows
numbered from 0 to 4. Make sure that the triangle occupies exactly the amount of space
needed to store all the numbers.
(b) Implement the destructor ~PascalTriangle() to deallocate the triangle.
As you might have noticed, the coefficients of the binomial (x+y)k are exactly the numbers in the row
number k of the Pascal’s triangle! This is a very useful application of the Pascal’s Triangle. If we wish to
print the polynomial that results from the expansion of (x+y)k we simply look at row number k of the
Pascal’s triangle. Of course, we will also need to compute the exponents of x and y in the expansion.
(c) Write a global method void printBinomialExpansion(int k) which prints the expansion of
(x+y)k in the format shown below. This method may use the class PascalTriangle for this purpose. Note: this
is a global method. Make sure that it can access the triangle inside the a PascalTriangle object.
class PascalTriangle{
friend void printBinomialExpansion(int k);
public:
PascalTriangle(int nrows){
n = nrows;
triangle = new int*[n];
~PascalTriangle(){
for(int i=0; i < n; i++){
delete [] triangle[i];
}
delete [] triangle;
}
};
PascalTriangle pt(k+1);
if(k == 1){
cout << "x";
}
else{
cout << "x^" << k;
}
if(i == 1){
cout << "y" << "+";
}
else{
cout << "y^" << i << "+";
}
}
if(k == 1){
cout << "y";
}
Hint: Consider the function string tokenizer function char*[] strtok(char* text, char* sep) is already
available to you for use. It returns an array of words in the given text separated by the separator sep.
Solution:
class Exam {
protected:
int questionsNumber;
int last;
Question** questions;
public:
Exam();
Exam(int);
void addQuestion(Question *);
virtual void print();
virtual ~Exam();
};
Exam::Exam() {
questionsNumber = 0;
last = -1;
questions = 0;
}
Exam::Exam(int n){
void Exam::print(){
for(int i=0; i <= last; i++ ){
cout << "Question " << i+1 << endl;
questions[i]->print();
cout << endl;
}
}
Exam::~Exam() {
if(questions != 0){
for(int i=0; i <= last; i++){
delete questions[i];
}
delete [] questions;
}
}
class Question {
public:
virtual void print() = 0;
};
MCQ::MCQ() {
text = NULL;
optionsNumber = 0;
options = NULL;
}
optionsNumber = n;
options = new char*[n];
for (int i=0; i < n; i++){
options[i] = new char[strlen(o[i])+1];
strcpy(options[i],o[i]);
}
}
MCQ::~MCQ() {
if (text != NULL){
delete [] text;
}
if(options != NULL){
for(int i=0; i < optionsNumber; i++){
delete [] options[i];
}
}
delete [] options;
}
public:
FIB();
FIB(char* t,int b);
virtual void print();
virtual ~FIB();
};
FIB::FIB() {
text = NULL;
blank = -1;
}
void FIB::print(){
FIB::~FIB() {
if (text != NULL){