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

The Ploynom.cc file: #include \"Polynom.h\" #include <iostream> #include <iomani

ID: 3597389 • Letter: T

Question

The Ploynom.cc file:

#include "Polynom.h"
#include <iostream>
#include <iomanip>
using namespace std;

Polynom::Polynom(){

// implementation needed
}

Polynom::Polynom(const vector<int>& coeff){

// implementation needed
}

const Polynom& Polynom::operator+(const Polynom& RHS) const {

// implementation needed
}

const Polynom& Polynom::operator-(const Polynom& RHS) const {

// implementation needed
}

double Polynom::operator()(double x) const{

// implementation needed
}

ostream& operator<<(ostream& ostr, const Polynom& RHS){

// implementation needed
}

bool Polynom::setCoeff(int k, int c) {
  
// implementation needed
}

bool Polynom::getCoeff(int k, int& c) {

// implementation needed
}

ostream& Polynom::insert(ostream& ostr) {

// implementation needed

In this assignment you will define, implement, and test a C+ class called Polynom to represent and use polynomials, A polynonial function of independent variable r can be written as The highest power of variable that occurs in the polya (in this case ) is called the degree of the polynomial. The quantities an,do are constants known as coefficients. In this assignment coefficients are int type and can be positive, negative, or 0. A basic operation for polynomials is to evaluate a polynomial at a specific value of r. For example, we can evaluate the quadratic polynomial r) q(z) =12 + 5r + 6 for for r-2, by writing the polynomial in the following form, and then substituting2 to obtain, g(2) ((25)2 +6)-(7)2+6)-(146)-20 We can add two polynomials and subtract one from the other. Examples are shown below p(z) = 3r, + 2r2 +1 + 16, q(1)-12 + 51 + 6 -3r' + 3r2 +6r +22 3+(-4)r+10 p(z) + q(z) _ (3 + 0)rs + (2 + 1):r" + (1 +5)| + (16 + 6) p(r)- g(r)-(3-0)(2 (1-5)(16-6) A simple way to represent a polynomial object of degree n is to use a vector of length n+1 to store the coefficients. For example, the polynomsp and q can be represented by vectors of length 4 and 3, respectively. p: 3 21 16, :1 5 6 It is possible that some of the coefficients in a polynomial are 0. Considr the polynomial r(r) = 5r9 + 2A + 19 where the largest power of is 9 so that we need a vector of length 10 to store the polynomial: r:50000 2000 6 This ament asks you to implement the functions that appear in Polynom.cc according to the specification provided in the decription given above.

Explanation / Answer

#include <iostream>
#include <sstream>
#include <stdexcept>
#include<iomanip>
#include <cmath>
#include "Polynom.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
Polynom::Polynom()
{
    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
Polynom::Polynom(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
Polynom::Polynom(const Polynom& 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
Polynom::~Polynom()
{
    delete[] coeff;
}

// Assignment operator
// Assign rhs Poly object to 'this' Poly object
const Polynom& Polynom::operator= (const Polynom& rhs)
{
    if (this == &rhs){
        return *this;
    }
    Polynom tmp(rhs);
    std::swap(maxPoly, tmp.maxPoly);
    std::swap(coeff, tmp.coeff);
    return *this;
}


// maxSize
// Return the size of the coefficient array
size_t Polynom::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 Polynom::grow(size_t newSize)
{
    int arrSize = (int) maxPoly;
    if ((int) newSize > arrSize)
    {
        double* newArrPtr = new double[newSize];
        for (int i = 0; i < arrSize; i++)
        {
            newArrPtr[i] = coeff[i];
        }
        std::swap(newArrPtr, coeff);
        std::swap(newSize, maxPoly);
    }

}

// degree
// Finds the degree of a polynomial (the highest power with a
//    non-zero coefficient)
// pre: Class object exists
// post: Returns the degree of the polynomial object.
size_t Polynom::degree() const
{
    size_t degree = 0;
    for (int i = 0; i < (int)maxPoly; i++)
    {
        if (coeff[i] != 0)
        {
            degree = (size_t)i;
        }
    }
    return degree;
}


// setCoeff
// Sets a term, value*x^i, in a polynomial, growing the array if necessary.
// pre: Class object has been initialized. 0 <= i.
// post: In the polynomial, the term with power i has coefficient
//       value. The polynomical was grown if required.
// Throws <std::out_of_range> if index i does not meet the precondition.
void Polynom::setCoeff(double value, size_t i)
{
    if ((size_t)0 <= i && i <= maxPoly)
    {
        if (i > maxPoly)
        {
            grow(i);
        }
        coeff[i] = value;
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }
}

// retrieveCoeff
// Finds the coefficient of the x^i term in poly
// pre: Class object has been initialized. 0 <= i
// post: Returns the value of the coefficient of the term with power i
// note: If the object does not contain a term with power i (e.g.,
//       i>=maxPoly), a coefficient value of zero is returned.
// Throws <std::out_of_range> if index i does not meet the precondition.
double Polynom::retrieveCoeff(size_t i) const
{
    if (0 <= (int)i)
    {
        return coeff[i];
    }
    else if (i >= (int)maxPoly)
    {
        return 0.0;
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }
}

// incrementCoeff
// Changes a term, value*x^i, in a polynomial, growing the polynomial if required.
// pre: Class object has been initialized. 0 <= i.
// post: In the Class object the term with power i has its coefficient
//       incremented by the given value. The array has grown if necessary.
// Throws <std::out_of_range> if index i does not meet the precondition.
void Polynom::incrementCoeff(double value, size_t i)
{
    if (0 <= (int)i)
    {
        if (i > (int)maxPoly)
        {
            grow(i);
        }
        coeff[i] *= value;
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }

}

// toString
// Produce a string representation of a Poly object
// pre: The class object has been initialized.
// post: A string representation is returned.
// dependencies: This function requires that the degree() and
// retrieveCoeff() functions are implemented.
// note: This function has been provided for you -- DO NOT CHANGE IT!
string Polynom::toString() const
{
    ostringstream result;
    bool printedSomething = false;
    for (int i = (int)degree(); i >= 0; i--)
    {
        double c = retrieveCoeff(i);
        if (c != 0.0)
        {
            printedSomething = true;
            if (i == 0)
            {
                result.setf(ios::showpos);
                result << " " << c;
                result.unsetf(ios::showpos);
            }
            else
            {
                result.setf(ios::showpos);
                result << " " << c;
                result.unsetf(ios::showpos);
                result << "*X^" << i;
            }
        }
    }
    if (!printedSomething)
    {
        result.setf(ios::showpos);
        result << " " << 0;
        result.unsetf(ios::showpos);
    }
    return result.str();

}

// numOfTerms
// Returns the number of terms in the polynomial.
// pre: The class object has been initialized.
// post: The number of non-zero terms of the polynomial is returned.
size_t Polynom::numOfTerms() const
{
    size_t numTerms = 0;
    for (int i = 0; i < (int)maxPoly; i++)
    {
        if (coeff[i] != 0)
        {
            numTerms++;
        }
    }
    return numTerms;
}

// evaluate
// Evaluate a polynomial for a specified value of X
// pre: The class object has been initialized
// post: The polynomial will be evaluated for the value of
//       X received as an argument. The resulting value is
//       returned.
double Polynom::evaluate(double x) const
{
    double polyTotal = 0.0;
    double coeff = 0.0;
    for (int i = 0; i < (int) maxPoly; i++)
    {
        coeff = retrieveCoeff((size_t) i);
        polyTotal += (coeff * pow(x, (double)i));
    }
    return polyTotal;
}

// add
// Add one polynomial to another
// pre: The class object has been initialized. The received
//       argument is also an initialized poly object.
// post: The argument polynomial is added to the object polynomial.
//       The object polynomial is changed to hold the sum. The object
//       polynomial is grown if required to hold the resulting sum.
// Note: the poly object being operated upon may be of a different
//   size (maxPoly) than the aPoly parameter. If the aPoly parameter
//   has a degree larger than the array in the 'this' Poly object,
//   then the array is grown large enough to hold the sum.
void Polynom::add(const Polynom& aPoly)
{
    if (aPoly.maxPoly > maxPoly)
    {
        grow(aPoly.maxPoly);
    }
    for (int i = 0; i < (int)maxPoly; i++)
    {
        coeff[i] += aPoly.coeff[i];
    }
}

// subtract
// Subtract one polynomial from another
// pre: The class object has been initialized. The received
//       argument is also an initialized poly object.
// post: The argument polynomial is subtracted from the object polynomial.
//       The object polynomial is changed to hold the result. The object
//       polynomial is grown if required to hold the result.
// Note: the poly object being operated upon may be of a different
//   size (maxPoly) than the aPoly parameter. If the aPoly parameter
//   has a degree larger than the array in the 'this' Poly object,
//   then the array is grown large enough to hold the result.
void Polynom::subtract(const Polynom& aPoly)
{
    if (aPoly.maxPoly > maxPoly)
    {
        grow(aPoly.maxPoly);
    }
    for (int i = 0; i < (int) maxPoly; i++)
    {
        coeff[i] -= aPoly.coeff[i];
    }
}

// addition operator
// Add two polynomials together and return a new polynomial that is the result
// pre: The class object has been initialized. The received
//       argument is also an initialized poly object.
// post: The argument polynomial is added to the object polynomial, and the
//       result is stored in a new polynomial which is returned.
//       The object polynomial is not changed.
// note: This function has been provided for you -- DO NOT CHANGE IT!
Polynom Polynom::operator+ (const Polynom& rhs) const
{
    Polynom result;
    result.add(*this);
    result.add(rhs);
    return result;
}

// subtraction operator
// Subtracts one polynomial from another and return a new polynomial that is the result
// pre: The class object has been initialized. The received
//       argument is also an initialized poly object.
// post: The argument polynomial is subtracted from the object polynomial, and the
//       result is stored in a new polynomial which is returned.
//       The object polynomial is not changed.
// note: This function has been provided for you -- DO NOT CHANGE IT!
Polynom Polynom::operator- (const Polynom& rhs) const
{
    Polynom result;
    result.subtract(*this);
    result.subtract(rhs);
    return result;
}

// equals
// Determine if two polynomials are equal
// pre: The class object has been initialized. The received
//       argument is also an initialized poly object.
// post: Returns true if the two polynomials are equal, false otherwise.
bool Polynom::equals(const Polynom& aPoly) const
{
    if (maxPoly != aPoly.maxPoly)
    {
        return(false);
    }
    for (int i = 0; i < (int) maxPoly; i++)
    {
        if (aPoly.coeff[i] != coeff[i])
        {
            return(false);
        }
    }
    return(true);
}

// Equality/inequality operators
// note: These functions have been provided for you -- DO NOT CHANGE IT!
bool Polynom::operator== (const Polynom& rhs) const
{
    return equals(rhs);
}

bool Polynom::operator!= (const Polynom& rhs) const
{
    return !equals(rhs);
}


// negate
// Negate a polynomial
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
//       multiplication by -1.0.
void Polynom::negate()
{
    multByConst(-1.0);
}

// multByConst
// Multiply a polynomial by a constant
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
//       multiplication by the value of argument val.
void Polynom::multByConst(double val)
{
    double* tmpArray = new double[maxPoly];
    for (int i = 0; i < (int)maxPoly; i++)
    {
        tmpArray[i] = retrieveCoeff((size_t)i) * val;
    }
    std::swap(tmpArray, coeff);
}

// derivative
// Compute the derivative of a polynomial
// pre: The class object has been initialized.
// post: The polynomial has been changed to represent its
//       derivative.
void Polynom::derivative()
{
    size_t derivArrSize = maxPoly - 1;
    double* derivArrPtr = new double[derivArrSize];
    for (int i = 1; i < (int) maxPoly; i++)
    {
        double coeffOfDeriv = retrieveCoeff((size_t)i) * (double)i;
        i -= 1;
        derivArrPtr[i] = coeffOfDeriv;
    }
    std::swap(derivArrPtr, coeff);
    std::swap(derivArrSize, maxPoly);
}

// insertion operator for output
// note: This function has been provided for you -- DO NOT CHANGE IT!
ostream & operator << (ostream &out, const Poly& p)
{
    out << p.toString();
    return out;
}

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