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

I need help making a method to get the first derivative!! The first thing is my

ID: 659762 • Letter: I

Question

I need help making a method to get the first derivative!!

The first thing is my assignment and then my code follows any help is appreciated.

I can reupload question to give you extra points!

// FILE polynomial.h
// CLASS: Polynomial_T (in the namespace csc161)
//
// Definition:
// A polynomial has one variable x, real number coefficients, and
// non-negative integer exponents. Such a polynomial can be viewed
// as having the form:
// A[n]*x^n + A[n-1]*x^(n-1) + ... A[2]*x^2 + A[1]*x + A[0]
// where the A[n] are the real number coefficients and x^i represents
// the variable x raised to the i power. The coefficient A[0] is
// called the "constant" or "zeroth" term of the polynomial.
//   
// NOTES:
// 1. This version works by storing the coefficients in
// a dynamic array. The coefficient for the x^k term is stored
// in location [k] of the dynamic array. When a new term is
// added beyond the current size of the array, then the
// dynamic array is replaced by a new, larger array.
// 2. Note that two functions have been implemented as inline functions
// in this file (the degree and operator() functions).
//   
//
// MEMBER CONSTANTS:
// const static size_t MAX_DEGREE
// The size of the initial array to store the coefficients.
//
// CONSTRUCTOR:
// POSTCONDITION: Creates a polynomial shell with all zero
// coefficients.
//
// MODIFICATION MEMBER FUNCTIONS:
//   
// void assign_coef(double p_coefficient, unsigned int p_exponent)
// POSTCONDITION: Sets the coefficient for the specified exponent.
//
// void clear( )
// POSTCONDITION: All coefficients of this polynomial are set to zero.
//
// void reserve(unsigned int p_additionalTerms)
// POSTCONDITION: Allocates additional memory to store additional terms.
// This guarantees that member functions will not need to worry about it.
//
// CONSTANT MEMBER FUNCTIONS:
// double getCoefficient(unsigned int p_exponent) const
// POSTCONDITION: Returns coefficient at specified exponent of this
// polynomial.
//
// unsigned int getDegree( ) const
// POSTCONDITION: The function returns the value of the largest exponent
// with a non-zero coefficient.
// If all coefficients are zero, then the function returns zero.
//
// Polynomial_T derivative( ) const
// POSTCONDITION: The return value is the first derivative of this
// polynomial.
//
// double eval(double p_x) const
// POSTCONDITION: The return value is the value of this polynomial with the
// given value for the variable x.
//
// unsigned int nextTerm(unsigned int p_e) const
// POSTCONDITION: The return value is the next exponent n which is LARGER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is zero.
//
// unsigned int previousTerm(unsigned int p_e) const
// POSTCONDITION: The return value is the next exponent n which is SMALLER
// than e such that coefficient(n) != 0.
// If there is no such term, then the return value is UINT_MAX
// from <climits>.
//
// CONSTANT OPERATORS:
// double operator( ) (double p_x) const
// Same as the eval member function.
//
// NON-MEMBER BINARY OPERATORS for the polynomial Class
// Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the difference of the coefficients of p1 & p2 for any given
// exponent.
//
// Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: return-value is a polynomial with each coefficient
// equal to the sum of the coefficients of p1 & p2 for any given
// exponent.
//
// Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2)
// POSTCONDITION: Each term of p1 has been multiplied by each term of p2,
// and the answer is the sum of all these term-by-term products.
// For example, if p1 is 2x^2 + 3x + 4 and p2 is 5x^2 - 1x + 7, then the
// return value is 10x^4 + 13x^3 + 31x^2 + 17x + 28.
//
// NON-MEMBER OUTPUT FUNCTIONS for the polynomial Class
// ostream& operator << (ostream& out, const Polynomial_T& p)
// POSTCONDITION: The polynomial has been printed to ostream out, which,
// in turn, has been returned to the calling function.
//
//
// DYNAMIC MEMORY
// Since this class uses dynamic memory, the copy constructor and assignment
// operator are overridden, and there is a destructor implemented. Also,
// if there is insufficient dynamic memory, the following functions throw
// a bad_alloc exception: the constructors, assignment, reserve, add_to_coef,
// assign_coef, and any function that returns a polynomial.

#ifndef POLY_H
#define POLY_H
#include <iostream> // Provides ostream
#include <vector> // If you decide on Vector

namespace csc161
{
  
class Polynomial_T
{
public:
   // CONSTANTS
   static const unsigned int MAX_DEGREE = 30;
  
// CONSTRUCTORS and DESTRUCTOR
   Polynomial_T();
   Polynomial_T(const Polynomial_T& source); // Copy constructor
   ~Polynomial_T( );

   // MODIFICATION MEMBER FUNCTIONS
   void operator =(const Polynomial_T& source);
   void assign_coef(double p_coefficient, unsigned int p_exponent);
   void clear( );
void reserve(unsigned int p_additionalTerms);
  
   // CONSTANT MEMBER FUNCTIONS
   double getCoefficient(unsigned int p_exponent) const;
   unsigned int getDegree( ) const { return current_degree; }
   Polynomial_T derivative( ) const;
   double eval(double p_x) const;
   unsigned int nextTerm(unsigned int p_e) const; // Helpers, might not be needed, if trivial
   unsigned int previousTerm(unsigned int p_e) const; // Helper, might not be needed, if trivial
  
   // CONSTANT OPERATORS - optional, we will cover this next time
   double operator( ) (double x) const { return eval(x); }
  
   // PRIVATE MEMBERS
private:

   // Make a choice of data structure, leave only applies
   double *coef; // Pointer to the dynamic array that stores coefs (if you decide on array)

   vector<double> coef_v; // Vector to store coefs (if you decide on vector)

   Term_T *coef; // Head of linked list (if you decide to on linked list).
   // Rename Node class to Term_T, change to store exponent and coef; include
   // term.h etc
   ///// Choice made. Please note that this might affect how data must be entered, if zeros are explicit etc.

   unsigned int size; // Current size of the coef data container (array, vector)
   unsigned int current_degree; // Current degree of the polynomial


};
  
// NON-MEMBER BINARY OPERATORS
Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2);
Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2); // Optional
Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2); // Optional
  
// NON-MEMBER OUTPUT FUNCTIONS
std::ostream& operator << (std::ostream& out, const Polynomial_T& p);
  
}
#endif

Polynomial.cpp

#include "Polynomial.h"

#include "Term.h"

#include <list>

using namespace std;

Polynomial::Polynomial(Term term) {

   this->terms.push_front(term);

}

Polynomial::Polynomial(list<Term> terms) {

   this->terms = terms;

}

Polynomial::Polynomial(const Polynomial& orig) {

   this->terms = orig.getTerms();

}

list <Term> Polynomial::getTerms() const {

   return this->terms;

}

Polynomial Polynomial::operator+(const Polynomial& other) {

   list <Term> newPoly;

   bool matchFound = false;

   list <Term>::iterator otherIter;

   list <Term>::iterator thisIter;

   list <Term> otherList = other.getTerms();

   // loop over all terms in argument  

   for (otherIter = otherList.begin();

                   otherIter != otherList.end(); ++otherIter) {

          // loop over all terms in this object (to find matches)

          matchFound = false;

          for (thisIter = this->terms.begin();

                          thisIter != this->terms.end(); ++thisIter) {

                 if ((*otherIter).getExpnt() == (*thisIter).getExpnt()) { // if matching expnt

                        matchFound = true;

                        newPoly.push_back(Term((*otherIter)+(*thisIter))); // add the two terms

                 }

          }

          if (!matchFound) { // no match found

                 newPoly.push_back(Term((*otherIter)));

          }

   }

   return Polynomial(newPoly);

}

Polynomial Polynomial::operator-(const Polynomial& other) {

   list <Term> newPoly;

   bool matchFound = false;

   list <Term>::iterator otherIter;

   list <Term>::iterator thisIter;

   list <Term> otherList = other.getTerms();

   // loop over all terms in argument  

   for (otherIter = otherList.begin();

                   otherIter != otherList.end(); ++otherIter) {

          // loop over all terms in this object (to find matches)

          matchFound = false;

          for (thisIter = this->terms.begin();

                          thisIter != this->terms.end(); ++thisIter) {

                 if ((*otherIter).getExpnt() == (*thisIter).getExpnt()) { // if matching expnt

                        matchFound = true;

                        newPoly.push_back(Term((*otherIter)-(*thisIter))); // add the two terms

                 }

          }

          if (!matchFound) { // no match found

                 newPoly.push_back(Term((*otherIter)));

          }

   }

   return Polynomial(newPoly);

}

// THIS WORKS BUT IT DOES NOT SIMPLIFY.

Polynomial Polynomial::operator *(const Polynomial& other) {

   list <Term> newPoly;

   list <Term>::iterator otherIter;

   list <Term>::iterator thisIter;

   list <Term> otherList = other.getTerms();

   // loop over all terms in argument  

   for (otherIter = otherList.begin();

                   otherIter != otherList.end(); ++otherIter) {

          // loop over all terms in this object (to find matches)

          for (thisIter = this->terms.begin();

                          thisIter != this->terms.end(); ++thisIter) {

                

                 newPoly.push_back(Term((*otherIter) * (*thisIter))); // add the two terms

          }

   }

   return Polynomial(newPoly);

}

Polynomial Polynomial::operator =(const Polynomial& other){

   this->terms = other.getTerms();

   return *this;

}

Polynomial Polynomial::operator +=(const Polynomial& other){

   Polynomial poly(Term(0));

   poly = *this + other;

   *this = poly;

   return *this;

}

Polynomial Polynomial::operator -=(const Polynomial& other){

   *this = Polynomial(*this - other);

   return *this;

}

Polynomial Polynomial::operator *=(const Polynomial& other){

   *this = Polynomial(*this * other);

   return *this;

}

Polynomial.h

#ifndef POLYNOMIAL_H

#define POLYNOMIAL_H

#include "Term.h"

#include <list>

#include <ostream>

using namespace std;

class Polynomial {

private:

   list <Term> terms;

public:

   Polynomial(Term term);

   Polynomial(list <Term> terms);

   Polynomial(const Polynomial& orig);

   list <Term> getTerms() const;

   // so we can see our polynomials

   friend ostream& operator<<(ostream& output, Polynomial& poly) {

          list <Term>::iterator iter;

          list <Term> termList = poly.getTerms();

          for (iter = termList.begin(); iter != termList.end(); ++iter) {

                 if (++iter == termList.end()) { // check if next term is end

                        --iter;

                        output << (*iter);

                 } else {

                        --iter;

                        output << (*iter) << " + ";

                 }

          }

          return output;

   }

   Polynomial operator+(const Polynomial& other);

   Polynomial operator-(const Polynomial& other);

   Polynomial operator*(const Polynomial& other);

   Polynomial operator=(const Polynomial& other);

   Polynomial operator+=(const Polynomial& other);

   Polynomial operator-=(const Polynomial& other);

   Polynomial operator*=(const Polynomial& other);

};

#endif /* POLYNOMIAL_H */

Term.cpp

#include "Term.h"

#include <iostream>

Term::Term(int coeff) {

   this->coeff = coeff;

   this->expnt = 0;

}

Term::Term(int coeff, int expnt) {

   this->coeff = coeff;

   this->expnt = expnt;

}

Term::Term(const Term& term) {

   this->coeff = term.getCoeff();

   this->expnt = term.getExpnt();

}

void Term::setCoeff(int coeff) {

   this->coeff = coeff;

}

void Term::setExpnt(int expnt) {

   this->expnt = expnt;

}

int Term::getCoeff() const {

   return this->coeff;

}

int Term::getExpnt() const {

   return this->expnt;

}

Term Term::operator +(Term& other) {

   if (other.getExpnt() == this->expnt) { // if exponents match

          return Term((other.getCoeff() + this->coeff), this->expnt); // add coeffs and keep same expnt

   } else {

          return Term(0); // error handling as we cant have a 0 coeff

   }

}

Term Term::operator -(const Term& other) {

   if (other.getExpnt() == this->expnt) { // if exponents match

          return Term((this->coeff - other.getCoeff()), this->expnt);

   } else {

          return Term(0); // error handling as we cant have a 0 coeff

   }

}

Term Term::operator =(const Term& other) {

   this->coeff = other.getCoeff();

   this->expnt = other.getExpnt();

   return *this; // i didnt know that you could do this...  

}

Term Term::operator *(const Term& other) {

   return Term((this->coeff * other.getCoeff()), // multiply to get coeff

                   (this->expnt + other.getExpnt()) // add to get expnt

                   );

}

Term.h

#ifndef TERM_H

#define TERM_H

#include <ostream>

using namespace std;

class Term {

private:

   int coeff;

   int expnt;

public:

   Term(int coeff);

   Term(int coeff, int expnt);

   Term(const Term& term) ;

   void setCoeff(int coeff);

   void setExpnt(int expnt);

   int getCoeff() const;

   int getExpnt() const;

  

   // for nice printing

   friend ostream& operator<<(ostream& output, Term& term){

           output << term.getCoeff() << "x^" << term.getExpnt();

           return output;

   }

  

   Term operator+(Term& other);

   Term operator=(const Term& other);

   Term operator-(const Term& other);

   Term operator*(const Term& other);

  

};

#endif /* TERM_H */

main.cpp

#include <cstdlib>

#include <iostream>

#include "Polynomial.h"

#include "Term.h"

#include <list>

using namespace std;

int main(int argc, char** argv) {

   // A SET OF TERMS FOR POLY1

   Term x(5, 2);

   Term y(4, 6);

   Term z(y * x); // woo operator overloading  

   list <Term> terms;

   terms.push_back(x);

   terms.push_back(y);

   terms.push_back(z);

   Polynomial poly1(terms);

   // A SET OF TERMS FOR POLY2

   Term a(2, 5);

   Term b(9, 8);

   Term c(a * b); // woo operator overloading  

   list <Term> terms2;

   terms2.push_back(a);

   terms2.push_back(b);

   terms2.push_back(c);

   Polynomial poly2(terms2);

   // A SET OF TERMS FOR POLY3

   Term d(3, 4);

   Term e(5, 7);

   Term f(a * b); // woo operator overloading  

   list <Term> terms3;

   terms3.push_back(d);

   terms3.push_back(e);

   terms3.push_back(f);

   Polynomial poly3(terms3);

   std::cout << "Polynomial 1 = " << poly1 << std::endl;

   std::cout << "Polynomial 2 = " << poly2 << std::endl;

   std::cout << "Polynomial 3 = " << poly3 << std::endl;

   Polynomial poly4(Term(0)); // empty polynomial

   // Addition

   poly4 = poly1 + poly2;

   std::cout << "Addition" << std::endl;

   std::cout << "poly1 (" << poly1 << ")" << " + " << "poly2 (" << poly2 << ")" << " = " << poly4 << std::endl;

   // Addition

   poly4 = poly1 - poly2;

   std::cout << "Subtraction" << std::endl;

   std::cout << "poly1 (" << poly1 << ")" << " - " << "poly2 (" << poly2 << ")" << " = " << poly4 << std::endl;

   // Multiplication

   poly4 = poly1 * poly2;

   std::cout << "Multiplication" << std::endl;

   std::cout << "poly1 (" << poly1 << ")" << " * " << "poly2 (" << poly2 << ")" << " = " << poly4 << std::endl;

   //...... much tired. sleep now.

  

//   // +=

//   std::cout << "+= operator" << std::endl;

//   std::cout << "poly3 (" << poly1 << ")" << " += " << "poly1 (" << poly3 << ")" << " returns ";

//   poly3 += poly1;

//   std::cout << poly3 << std::endl;

   return 0;

}

Explanation / Answer

Answer:

#ifndef TERM_POLY_H

#define TERM_POLY_H

#include <iostream>

using std::ostream;

class Term_poly

{

    friend ostream & operator<<(ostream &,const Term_poly &);

    friend class Polynominal;

private:

    int coef;

    int exp;

    Term_poly* link;

public:

    Term_poly(int c, int e);

    Term_poly(int c, int e, Term_poly* nxt);

    Term_poly* InsertAfter(int c, int e);

};

#endif

#ifndef POLYNOMIAL_H

#define POLYNOMIAL_H

#include "Term_poly.h"

#include <iostream>

using std::ostream;

using std::istream;

class Polynominal

{

    friend ostream& operator<<(ostream& out,const Polynominal& x);

    friend istream& operator>>(istream& in, Polynominal& x);

    friend Polynominal& operator + (Polynominal& a, Polynominal& b);

    friend Polynominal& operator - (Polynominal& a, Polynominal& b);

    friend Polynominal& operator * (Polynominal& a, Polynominal& b);

public:

    Polynominal();

    Polynominal(Polynominal& r);

    ~Polynominal();

    void AddTerm_polys(istream& in);

    void Output(ostream& out)const;

    Polynominal& PolyAdd(Polynominal& r);

    Polynominal& PolyMul(Polynominal& r);

    Polynominal& PolySub(Polynominal& r);

    Polynominal& operator=(Polynominal& r);

    Polynominal& operator += (Polynominal& a);

    Polynominal& operator -= (Polynominal& a);

    Polynominal& operator *= (Polynominal& a);

    void PolyClear();

private:

    Term_poly* thelist;

};

#endif

#include <iostream>

#include "Term_poly.h"

using std::cin;

using std::cout;

using std::endl;

Term_poly::Term_poly(int c, int e)

{

    link=0;

}

Term_poly::Term_poly(int c, int e, Term_poly* nex):coef(c),exp(e)

{

    link=nex;

}

Term_poly* Term_poly::InsertAfter(int c, int e)

{

    link=new Term_poly(c,e,link);

    return link;

}

ostream &operator <<(ostream& out,const Term_poly& val)

{

    if(val.coef==0)

        return out;

    out<<val.coef;

    switch(val.exp)

    {

        case 0:break;

        case 1:out<<"x";break;

        default:out<<"x^"<<val.exp;break;

    }

    return out;

}

#include  "polynomial.h"

#include <iostream>

using std::cin;

using std::cout;

using std::endl;

using std::ends;

Polynominal& Polynominal::operator=(Polynominal& r)

{

    this->PolyClear();

    Term_poly* q=thelist->link,* p;

    for(p=r.thelist->link;p->exp>=0;p=p->link)

        q=q->InsertAfter(p->coef,p->exp);

    return *this;

}

Polynominal::Polynominal(Polynominal& r)

{

    this->PolyClear();

    Term_poly* q=thelist->link,* p;

    for(p=r.thelist->link;p->exp>=0;p=p->link)

        q=q->InsertAfter(p->coef,p->exp);

}

Polynominal::Polynominal()

{

    thelist=new Term_poly(0,-1);

    thelist->link=thelist;

}

Polynominal::~Polynominal()

{

    Term_poly* p=thelist->link;

    while(p!=thelist)

    {

        thelist->link=p->link;

        delete p;

        p=thelist->link;

    }

    delete thelist;

}

void Polynominal::PolyClear()

{

    Term_poly* p=thelist->link;

    while(p->exp>=0)

    {

        thelist->link=p->link;

        delete p;

        p=thelist->link;

    }

}

void Polynominal::AddTerm_polys(istream& in)

{

    Term_poly* q=thelist;

    int c,e;

    for(;;)

    {

        cout<<"Input a Term_poly(coef,exp):";

        cin>>c>>e;

        if( e < 0 )

            break;

        q=q->InsertAfter(c,e);

    }

}

void Polynominal::Output(ostream& out)const

{

    int first=1,second = 0;

    Term_poly* p=thelist->link;

    for(;p!=thelist;p=p->link)

    {

        if(!first&&(p->coef>0))

            out<<"+";

        first=0;

        cout<<*p;

        second = 1;

    }

    if( !second )

        out<<"0";

}

Polynominal& Polynominal::PolyAdd(Polynominal& r)

{   

    Polynominal *t = new Polynominal;

    *t = *this;

    Term_poly *q,*q1=t->thelist,*p;

    p=r.thelist->link;

    q=q1->link;

    while( p->exp >= 0 )

    {

        while(p->exp < q->exp)

        {

            q1=q;

            q=q->link;

        }

        if(p->exp == q->exp)

        {

            q->coef=q->coef+p->coef;

            if( q->coef == 0 )

            {

                q1->link=q->link;

                delete (q);

                q=q1->link;

            }

            else

            {

                q1 = q;

                q = q->link;

            }

        }

        else

        {

            q1=q1->InsertAfter(p->coef,p->exp);

        }

        p=p->link;

    }

    return *t;

}

Polynominal& Polynominal::PolySub(Polynominal& r)//this - r

{

    Polynominal *t = new Polynominal;

    *t = *this;

    Term_poly *q,*q1=t->thelist,*p;

    p=r.thelist->link;

    q=q1->link;

    while( p->exp >= 0 )

    {

        while(p->exp < q->exp)

        {

            q1=q;

            q=q->link;

        }

        if(p->exp == q->exp)

        {

            q->coef = q->coef - p->coef;

            if( q->coef == 0 )

            {

                q1->link=q->link;

                delete (q);

                q=q1->link;

            }

            else

            {

                q1=q;

                q=q->link;

            }

        }

        else

        {

            q1=q1->InsertAfter(-p->coef,p->exp);

        }

        p=p->link;

    }

    return *t;

}

Polynominal& Polynominal::PolyMul(Polynominal& r)

{

    Term_poly *q,*p;

    Polynominal *temp=new Polynominal,*t=new Polynominal;

    Term_poly *qq;

    for(p=r.thelist->link;p->exp>=0;p=p->link)

    {

        for(q=thelist->link,qq=temp->thelist->link;q->exp>=0;q=q->link)

        {

            qq=qq->InsertAfter(q->coef*p->coef,p->exp+q->exp);

        }

        *t = t->PolyAdd(*temp);

        temp->PolyClear();

    }

    delete temp;

    return *t;

}

ostream& operator<<(ostream& out,const Polynominal& x)

{

    x.Output(out);

    return out;

}

istream& operator>>(istream& in, Polynominal & x)

{

    x.AddTerm_polys(in);

    return in;

}

Polynominal& operator + (Polynominal& a, Polynominal& b)

{

    Polynominal *t = new Polynominal;

    *t = a.PolyAdd(b);

    return *t;

}

Polynominal& operator - (Polynominal& a, Polynominal& b)

{

    Polynominal *t = new Polynominal;

    *t = a.PolySub(b);

    return *t;

}

Polynominal& operator *(Polynominal& a, Polynominal& b)

{

    /*Polynominal *t = new Polynominal;

    *t = a.PolyMul(b);

     return *t;*/

    return a.PolyMul(b);

}

Polynominal&  Polynominal::operator += (Polynominal& a)

{

    *this = *this + a;

    return *this;

}

Polynominal& Polynominal::operator -= (Polynominal& a)

{

    *this = *this - a;

    return *this;

}

Polynominal& Polynominal::operator *= (Polynominal& a)

{

    *this = *this * a;

    return *this;;

}

#include <iostream>

#include "polynomial.h"

using std::cin;

using std::cout;

using std::endl;

int main()

{

    Polynominal p,q,r;

    cout<<"input poly p: "<<endl;

    cin>>p;

    cout<<"input poly q: "<<endl;

    cin>>q;

    cout<<" Measuring game results played as below:"<<endl;

    cout<<"p+q="<<p+q<<endl;

    cout<<"p*q="<<p*q<<endl;

    cout<<"p-q="<<p-q<<endl;

    p+=q;

    cout<<"p+=q"<<p<<endl;

    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