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. In mathematics, polynomial i

ID: 3669160 • Letter: U

Question

Using dynamic arrays, implement a polynomial class. In mathematics, polynomial is a function of the form f(x) = a0*x^0 + a1*x^1 + a2*x^2 + a3*x^3 + ....n terms. Here, a0, a1, a2 etc. are the coefficients of the polynomial and n is the order of the polynomial.
The private variables include the value of x (a real number), the order of the polynomial (an integer) and the a dynamic array that stores the coefficients (real numbers). The methods include a
a. default constructor that sets the value of x to zero and the order to 0,
b. a two argument constructor that takes as arguments the value of x and the order of the polynomial. The values of the coefficients are set to zero.
c. inputCoefficients(): prompts the user to input the value of the coefficients of the polynomial. For this homework, skip the user input. Instead assign the coefficent values equal to the index of the position in the array. For example, a0 = 0, a1 = 1, a2 = 2 and so on.
c. a copy constructor
d. evaluate() that returns the value of the polynomial.
e. destructor. Deallocates dynamic arrays and prints a message "Polynomial destroyed! "

Below is the testing program -

int main ()
{
Polynomial p1 (0.5,3); // Invokes two argument constructor for p1
p1.inputCoefficients(); // Set the coefficient of polynomial p1
cout << "Polynomial p1 evaluates to " << p1.evaluate() << endl; // Evaluates the polynomial p1
Polynomial p2(p1)
cout << "Polynomial p2 evaluates to " << p2.evaluate() << endl; // Evaluates the polynomial p2
return 0;

}

Explanation / Answer

#include <iostream>
using namespace std;

class Polynomial
{
private:
   int Nterms;
   double* pCoeffs;// from lowest to highest order
public:
   // functions
   double evaluateAt(double x);
   void print(void);

    // constructor
   Polynomial( double Coeffs[], int N_terms );// full construction from given array of coefficients
   // destructor
   ~Polynomial();// destructor VERY important this case  
};

// full constructor. Must be passed an array of coeffs. and the array size.
Polynomial::Polynomial( double Coeffs[], int N_terms )
{
   Nterms = N_terms;
   pCoeffs = new double[ Nterms ];// allocate an array to hold the coefficient values
   for(int i=0; i<Nterms; i++)
       pCoeffs[i] = Coeffs[i];// assign in straight order
}

// destructor - releases memory for the dynamically allocated coefficient array
Polynomial::~Polynomial()
{
   if( pCoeffs )
   {
       delete [] pCoeffs;
       pCoeffs = NULL;
   }
}
// finds P(x)
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];// add up the terms
           xPow *= x;// build up the power of x from term to term
       }  

   return sum;
}
// simple version produces crude output. Finetune to suit.
void Polynomial::print(void)
{
   // 1st term
   std::cout << pCoeffs[Nterms-1] << "x^" << Nterms-1;
   // remaining terms
   for(int i=Nterms-2; i>=0; i--)          
       std::cout << " + " << pCoeffs[i] << "x^" << i;      
   return;
}

int main(void)// example use
{
   // 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;
   

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