Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a class to represent and manipulate polynomials. A polynomial can be rep

ID: 3829355 • Letter: D

Question

Develop a class to represent and manipulate polynomials. A polynomial can be represented as a linked list of terms where each term consists of the exponent and the coefficient. An example of a polynomial is 3x+4x4 - x 2 + 2x3 . The linked list should be sorted in ascending order by the exponents of its terms. Please observe the following:

• The polynomial term is a struct with two integer members representing the exponent and the coefficient.

• Provide a default constructor, copy constructor and a destructor.

• Overload the assignment operator.

• Overload the + operator to add two polynomials. The resulting polynomial must be simplified (i.e. each exponent appears only once)

• Overload the stream insertion operator to display the polynomial in the format: 3x^1 + 4x^4 – 1x^2 + 2x^3

• Provide a function to insert a new term to an existing polynomial. The new term should be inserted in its correct location and should not result in a duplicated exponent.

• Provide a function to remove the term with a given exponent from the polynomial.

• Provide a client program that tests each of the requested operation

Explanation / Answer

#include <iostream>
using namespace std;
int *multiply(int A[], int B[], int m, int n)
{
int *prod = new int[m+n-1];
for (int i = 0; i<m+n-1; i++)
prod[i] = 0;
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
prod[i+j] += A[i]*B[j];
}

return prod;
}
void printPoly(int poly[], int n)
{
for (int i=0; i<n; i++)
{
cout << poly[i];
if (i != 0)
cout << "x^" << i ;
if (i != n-1)
cout << " + ";
}
}
int main()
{
int A[] = {5, 0, 10, 6};
int B[] = {1, 2, 4};
int m = sizeof(A)/sizeof(A[0]);
int n = sizeof(B)/sizeof(B[0]);
cout << "First polynomial is ";
printPoly(A, m);
cout << " Second polynomial is ";
printPoly(B, n);
int *prod = multiply(A, B, m, n);
cout << " Product polynomial is ";
printPoly(prod, m+n-1);
return 0;
}