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

polynomial of the form: Inputs to the function should include x and the polynomi

ID: 3677040 • Letter: P

Question

polynomial of the form: Inputs to the function should include x and the polynomial coefficients, Ao As. The function should use default values of 0 for A2 As so that i will work with polynomials that are S order, 2d order, 3 order, 4order, or 5th order and unused higher-order coefficients do not need to be listed in the function call. Examples: Function Poly could be used to evalua and 5t order polynomials as follows: (answer: y = 2+3x = 2+3(1) = 5) y Polv(1,2,3); y = Poly(2,5,4,3); (answer: y-5+4x+3x2-5+4(2)+3 (2)2-25) y = Poly (2,3,4,5,6,7,8); (answer: y-3+4x+5x 6x3+7x4 +8x5 = 447) A) Write function Poly as described above B) Write a main function that prompts the user to enter the order (1-5) and then prompts the user to enter the correct number of costtients and the value of x. The main program should then call function Poly to calculate the output and display the result. C) Test the program for the 3 cases above as well as for 3d and 4 order polynomials for any values you choose. Check the values by hand (show the calculations) to insure that they are correcet.

Explanation / Answer

Run

  #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;          return 0;  }  

Run