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

A single variable polynomial is represented as an arithmetic expression shown be

ID: 3783491 • Letter: A

Question

A single variable polynomial is represented as an arithmetic expression shown below: A0 + A1 * x + A2 * x 2 + … + Ak * x k

The highest exponent, k, is called the degree of the polynomial, and the constants A0, A1, …, Ak are the coefficients. For example, here are two polynomials with degree 3:

2.1 + 4.8x + 0.1x2 + (-7.1)x3

2.9 + 0.8x + + 1.7x3

Design, and implement a class for polynomials. The class may contain a static member constant, MAX, which indicates the maximum degree of any polynomial (this allows you to store the coefficients in an array with a fixed size).

The default constructor will be one which will initialize the polynomial with all coefficients set to zero. In addition, you must have another constructor that takes two parameters: degree, and coefficient_array which are used to initialize the polynomial appropriately. In addition you should have a member function that gets and sets the value of an appropriate coefficient. For example, get(k) returns the coefficient of xk , and set(k, 24.5) sets the coefficient of xk to 24.5. You should provide a member function for comparing two polynomials (whether they have same coefficients), and evaluating a polynomial value for specific value of x, say x = 5.7.

Please implement poly.cpp in (In C++ please) given the following poly.h header file:

#ifndef POLY_H

#define POLY_H

class poly

{

public:

static const int MAX = 10;

// CONSTRUCTORS

poly( );// a polynomial with degree 0

poly(const int k );// a polynomial with degree k

poly(const int k, const double initial_coeff[] );

// MEMBER FUNCTIONS

void set(const int k, const double coeff);// sets the kth coefficient

// CONSTANT MEMBER FUNCTIONS

int get(const int k ) const;// gets the kth coefficient

int degree() const;

double evaluate (const double xval) const;//evaluates the polynomial

private:

double data[MAX];

int degree;

};

}

#endif

Explanation / Answer

//Following is the C++ program for the given problem statement.
#include <stdio.h>
#include <iostream>
   #include <math.h>
using namespace std;
class poly
{
   
     public:
          static const int MAX=10;
          poly()
          {
               degree=0;
               for(int i=0;i<MAX;i++)
                    data[i]=0;
          }
          poly(const int k)
          {
               degree=k;
          }
          poly(const int k, const double initial_coeff[] )
          {
               degree=k;
               for(int i=0;i<MAX;i++)
                    data[i]=initial_coeff[i];
          }
          void set(const int k,const double coeff)
          {
               data[k]=coeff;
          }
          int get(const int k)const
          {
               return data[k];
          }
          int get_degree()const
          {
               return degree;
          }
          double evaluate(const double xval) const
          {
               double ans=0;
               for(int i=0;i<=degree;i++)
               {
                    ans+=(data[i]*pow(xval,i));
               }
               return ans;
          }
          private:
          double data[MAX];
          int degree;
};

int main()
{
     poly p(3);
     cout<<p.get_degree()<<" ";
     double d[]={1,2,4,6};
     poly p1(4,d);
     cout<<p1.get(2)<<" ";
     p1.set(2,54);
     cout<<p1.get(2)<<" ";
     cout<<p1.evaluate(2)<<" ";
     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