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

C++ A single variable polynomial is represented as an arithmetic expression show

ID: 3687134 • Letter: C

Question

C++

A single variable polynomial is represented as an arithmetic expression show below:

               A0 + A1 * x + A2* x2 + … + Ak* xk

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

Specify, design, and implement a class for polynomials. The class may contain a static member constant, MAXDEGREE, 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 has 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, polynomial::get(k) returns the coefficient of xk, and polynomial::set(k, 24.5) sets the coeeficient of xk to 24.5.

You should provide a member function for adding two polynomials, subtracting one polynomial from other, and evaluating a polynomial value for specific value of x, say x = 5.7.

Explanation / Answer

class Term { public: //CONSTRUCTORS Term(int theA,int theN,Term* theLink) : a(theA),n(theN),link(theLink){}; //ACCESSORS int getA() const {return a;} int getN() const {return n;} Term* getLink() const {return link;} //MUTATORS void setA(int theA){a = theA;} void setN(int theN){n = theN;} void setPol(int theA,int theN){a = theA;n = theN;} void setLink(Term* theLink){link = theLink;} //OVERLOADED OPERATORS Term operator + (Term object); Term operator - (const Term& object); Term operator * (const Term& object); friend ostream& operator > (istream& in,Term& object); //DESTRUCTORS //virtual ~Term(); private: //a*x^n int a; int n; Term* link; }; class Polynomial { public: friend ostream& operator > (istream& in,Polynomial& object); private: Term* top; }; #include #include #include #include #include "Poly.h" using namespace std; // Default Class constructor // Allocate an array of DEFAULTPOLY elements and initializes it to the constant 0 // post: Class object is initialized to degree-0 polynomial of 0 Poly::Poly() { coeff = new double [DEFAULTPOLY]; for (int i = 0; i < (int) DEFAULTPOLY; i++) { coeff[i] = 0.0; } maxPoly = DEFAULTPOLY; } // Non-default (alternate) Class constructor // Allocate an array of 'size' elements and initializes it to the constant 0 // post: Class object is initialized to degree-0 polynomial of 0 Poly::Poly(size_t size) { coeff = new double[size]; for (int i = 0; i < (int) size; i++) { coeff[i] = 0.0; } maxPoly = size; } // Copy constructor // Construct a new Poly that is a copy of an existing Poly // post: Class object is initialized to be a copy of the argument Poly Poly::Poly(const Poly& aPoly) { coeff = new double[aPoly.maxPoly]; for (int i = 0; i < (int) aPoly.maxPoly; i++) { coeff[i] = aPoly.coeff[i]; } maxPoly = aPoly.maxPoly; } // Destructor // Destroy a poly object by freeing the dynamically allocated array Poly::~Poly() { delete[] coeff; } // Assignment operator // Assign rhs Poly object to 'this' Poly object const Poly& Poly::operator= (const Poly& rhs) { if (this == &rhs){ return *this; } Poly tmp(rhs); std::swap(maxPoly, tmp.maxPoly); std::swap(coeff, tmp.coeff); return *this; } ////////////////////////////////////////////////// // // Member functions [specs for ADT's operations] // ////////////////////////////////////////////////// // maxSize // Return the size of the coefficient array size_t Poly::maxSize() const { size_t maxSize = maxPoly; return maxSize; } // grow // This method will allow us to increase the size of the dynamically allocated // array by allocating a new array of the desired size, copying the data from // the old array to the new array, and then releasing the old array. // If the newSize is less than or equal to the current size, then no actions // are taken. // Note: the maximum degree of a polynomial is one less than the size of the // array. The parameter newSize represents the size of the array. void Poly::grow(size_t newSize) { int arrSize = (int) maxPoly; if ((int) newSize > arrSize) { double* newArrPtr = new double[newSize]; for (int i = 0; i