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

Using dynamic arrays, implement a polynomial class with polynomial addition, sub

ID: 3583554 • Letter: U

Question

Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication.
Discussion: A variable in a polynomial does very little other than act as a placeholder for the coefficients. Hence, the only interesting thing about polynomials is the array of coefficients and the corresponding exponent. Think about the polynomial
x*x*x + x + 1
One simple way to implement the polynomial class is to use an array of integers to store the coefficients. The index of the array is the exponent of the corresponding term. Where is the term in x*x in the previous example? If a term is missing, then it simply has a zero coefficient.
There are techniques for representing polynomials of high degree with many missing terms. These use so-called sparse polynomial techniques. You can put the non-zero terms into a vector instead of a array. and order the vector by their exponents.
Provide a default constructor, a copy constructor, and a parameterized constructor that enable an arbitrary polynomial to be constructed. Also
supply an overloaded operator = and a destructor.
Provide these operations:
polynomial + polynomial
polynomial - polynomial
polynomial * polynomial
Supply functions to assign and extract coefficients, indexed by exponent.

Input

The first line is an arithmatic operator (+, - or *).

The following two lines are two polynomials. Use two numbers to describe coefficients in polynomials. For example 10 * x * x * x would be 10 3. 10 is the coefficient of x * x * x, and 3 is the exponent.

For example, the input for polynomial additon (10X^3 + 2X^2 + 1) + 5X^2 would be

For example, the input for polynomial additon (2X + 1) + (4X^4 + 2X + 3) would be

For example, the polynomial multiplication (-4X^3-3X^2-2X-1)*(4X^3+X^2)

Output

Sample Input

Sample Output

Explanation / Answer

#include <iostream>

using namespace std;

class Polynomial

{

private:

                int Nterms;

                double* pCoeffs;      

public:

                double evaluateAt(double x);

                void print(void);

                Polynomial( double Coeffs[], int N_terms );

                ~Polynomial();                

};

Polynomial::Polynomial( double Coeffs[], int N_terms )

{

                Nterms = N_terms;

                pCoeffs = new double[ Nterms ];

                for(int i=0; i<Nterms; i++)

                                pCoeffs[i] = Coeffs[i];

}

Polynomial::~Polynomial()

{

                if( pCoeffs )

                {

                                delete [] pCoeffs;

                                pCoeffs = NULL;

                }

}

double Polynomial::evaluateAt(double x)

{

                double sum = 0.0;

                double xPow = 1.0;

                if( pCoeffs )       

                                for(int i=0; i<Nterms; i++)

                                {                                             

                                                sum += xPow*pCoeffs[i];

                                                xPow *= x;

                                }             

                return sum;

}

void Polynomial::print(void)

{

                std::cout << pCoeffs[Nterms-1] << "x^" << Nterms-1;

                for(int i=Nterms-2; i>=0; i--)                                       

                                std::cout << " + " << pCoeffs[i] << "x^" << i;                       

                return;

}

int main(void

{

                // Pnum = 8*x^4 + 10*x^3 + x^2 + 29*x + 19      

                double Cnum[5] = { 19.0, 29.0, 1.0, 10.0, 8.0 };

                Polynomial Pnum( Cnum, 5 );

                cout << "Pnum = ";

                Pnum.print();

                cout << " Pnum(2.0) = " << Pnum.evaluateAt(2.0) << endl;

                return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote