Date: January 18th, 2013
Time: 08:10-10:00
Open book; turn off computer & mobile phone
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Recursive function
#include <iostream>
static int count = 0; // static variable
int fibonacci(int n)
{
count++;
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n-1) + fibonacci(n-2);
std::cout << "fibonacci() called" << std::endl;
}
int main()
{
std::cout << fibonacci(5) << std::endl;
std::cout << "This recursive function has been called "
<< count << " times." << std::endl;
return 0;
}
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Representing a polynomial with a linked-list
#include <iostream>
struct ListElement
{
int value;
ListElement* pNext;
};
void Print(ListElement* p)
{
int i;
for (i = 0; p != NULL; p = p->pNext, i++)
{
std::cout << "+" << p->value;
if (i > 0)
{
std::cout << "X";
if (i > 1)
std::cout << "^" << i;
}
}
std::cout << std::endl;
}
int main()
{
ListElement LE4 = { 1, NULL };
ListElement LE3 = { 2, &LE4 };
ListElement LE2 = { 0, &LE3 };
ListElement LE1 = { 3, &LE2 };
Print(&LE1);
return 0;
}
(10%) The above Print() function does not handle negative coefficients beautifully. For example, the polynomial "X^3-X^2-3" will be output as "+-3+0X+-1X^2+1X^3". If we want it to print "-3-X^2+X^3", what expression should you supply in the following blank?
// Representing a polynomial with a linked-list
#include <iostream>
using std::cout;
struct ListElement
{
int value;
ListElement* pNext;
};
void Print(ListElement* p)
{
int i;
for (i = 0; p != NULL; p = p->pNext, i++)
{
if (p->value == 0) continue; // Skip this term
if (p->value == -1) cout << '-';
if (p->value > 0) cout << "+";
if ( (1) p->value != 1) cout << p->value;
if (i > 0)
{
cout << "X";
if (i > 1)
cout << "^" << i;
}
}
cout << std::endl;
}
int main()
{
ListElement LE4 = { 1, NULL };
ListElement LE3 = { -1, &LE4 };
ListElement LE2 = { 0, &LE3 };
ListElement LE1 = { -3, &LE2 };
Print(&LE1);
return 0;
}
// Adding two polynomials
#include <iostream>
struct Polynomial
{
int coefficient;
Polynomial* pNext;
};
void Print(Polynomial* p)
{
int i;
for (i = 0; p != NULL; p = p->pNext, i++)
{
std::cout << "+" << p->coefficient;
if (i > 0)
{
std::cout << "X";
if (i > 1)
std::cout << "^" << i;
}
}
std::cout << std::endl;
}
Polynomial* Add(Polynomial* p1, Polynomial* p2)
{
int n;
Polynomial* pSum = NULL;
Polynomial* pHead = NULL;
Polynomial* pPrevious = NULL;
for (n=0; p1 || p2; n++)
{
pSum = new Polynomial;
if (!pHead) pHead = pSum;
if (pPrevious) pPrevious->pNext = pSum;
if (p1 == NULL)
{
pSum->coefficient = p2->coefficient;
p2 = p2->pNext;
}
else if (p2 == NULL)
{
pSum->coefficient = p1->coefficient;
p1 = p1->pNext;
}
else // Neither p1 nor p2 are NULL
{
pSum->coefficient = p1->coefficient + p2->coefficient;
p1 = p1->pNext;
p2 = p2->pNext;
}
pPrevious = pSum;
}
pSum->pNext = NULL;
return pHead;
}
int main()
{
Polynomial a2 = { 1, NULL };
Polynomial a1 = { 2, &a2 };
Polynomial a0 = { 1, &a1 }; // 1 + 2X + X^2
Polynomial b2 = { 4, NULL };
Polynomial b1 = { 4, &b2 };
Polynomial b0 = { 1, &b1 }; // 1 + 4X + 4X^2
Polynomial* ps = Add( &a0, &b0 );
Print(ps);
return 0;
}