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

Please help to fill out the main.cpp part. I have header file and generic file b

ID: 659967 • Letter: P

Question

Please help to fill out the main.cpp part. I have header file and generic file but I got problem with main part. Please help this.

// *********************************************************************

// This program models a bag with pets to demonstrate polymorphism

// It holds a vector of pointers to a generic Pet, but specific pets are added

// *********************************************************************

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

#include "polynomial_1.h"

using namespace csc161;

///////////////////// FUNCTIONS PROTOTYPES

///////////////////// FUNCTIONS IMLEMENTATION

// ======================

//     main function

// ======================

int main()

{

    Polynomial_T poly1(5, 0); // Instantiates a Polynomial_T

cout << "here1 ";

    poly1.assign_coef(3, 3);

    poly1.assign_coef(2, 2);

    poly1.assign_coef(1, 1);

cout << poly1;

    cout << poly1.derivative();

    cout << (poly1.derivative()).eval(1);

    //cout << poly1.eval(1);

cout << endl;

// system("pause");

return 0;

}

// Header file

#ifndef POLY_H

#define POLY_H

#include <iostream> // Provides ostream

#include <vector> // If you decide on Vector

#define _SCL_SECURE_NO_WARNINGS 1

namespace csc161

{

class Polynomial_T

    {

    public:

    // CONSTANTS

    static const unsigned int MAX_DEGREE = 30;

   

    // CONSTRUCTORS and DESTRUCTOR

    Polynomial_T();

    Polynomial_T(double c, unsigned int exponent);

    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; }

    void add_to_coef(double amount, unsigned int exponent);

    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;        // 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

/////////////

// FILE: poly1.cxx

// CLASS IMPLEMENTED: polynomial (see poly1.h for documentation)

// INVARIANT for the polynomial class:

//   1. coef points to a dynamic array with size elements.

//   2. For each k < size, the coefficient of the x^k term is

//      stored in coef[k].

//   3. The degree of the polynomial is stored in current_degree

//      (using zero for the case of all zero coefficients).

#include <algorithm> // Provides fill_n and copy functions

#include <cassert>   // Provides assert

#include <climits>   // Provides std::UINT_MAX

#include <cmath>     // Provides pow

#include <fstream>   // Provides ofstream for make_gif

#include <iostream> // Provides ostream

#include "polynomial_1.h"

using namespace std;

namespace csc161

{

    //const unsigned int Polynomial_T::MAX_DEGREE;

    Polynomial_T::Polynomial_T() { };

    Polynomial_T::Polynomial_T(double c, unsigned int exponent)

    {

       coef = new double[MAX_DEGREE];

                     

       fill_n(coef, MAX_DEGREE, 0.0);

        current_degree = 0;

       size = MAX_DEGREE;

        assign_coef(c, exponent);

    }

    Polynomial_T::Polynomial_T(const Polynomial_T& source)

    {

        //coef = new double[source.size];

       coef = source.coef;

        size = source.size;

        current_degree = source.current_degree;

        //copy(source.coef, source.coef + size, coef);

    }

Polynomial_T::~Polynomial_T()

    {

        //delete [ ] coef;

    }

    void Polynomial_T::operator =(const Polynomial_T& source)

        {

        double *new_coef;

   

        if (this == &source)

            return;

        if (size != source.size)

        {

       new_coef = new double[source.size];

       //delete [ ] coef;

       coef = new_coef;

       size = source.size;

        }

        current_degree = source.current_degree;

        copy(source.coef, source.coef + size, coef);

        }

    void Polynomial_T::add_to_coef(double amount, unsigned int exponent)

    {

        assign_coef(getCoefficient(exponent) + amount, exponent);

    }

   

    void Polynomial_T::assign_coef(double coefficient, unsigned int exponent)

    {

        if (exponent >= size)

            reserve(int(1.1*exponent + 1));

        // Adjust the coefficient

        coef[exponent] = coefficient;

        // Maintain the current degree:

        if ((exponent > current_degree) && (coefficient != 0.0))

       current_degree = exponent;

        while ((current_degree > 0) && (coef[current_degree] == 0.0))

       --current_degree;

    }

   

    void Polynomial_T::clear()

    {

        fill_n(coef, size, 0.0);   

        current_degree = 0;

    }

    void Polynomial_T::reserve(unsigned int number)

    {

        double *larger_array;

        if (number <= size)

       return; // The allocated memory is already enough.

        if (number < current_degree+1)

       return; // Can't allocate less than we are using.

        larger_array = new double[number];

        fill_n(larger_array, number, 0.0);

        copy(coef, coef + size, larger_array);

        delete [ ] coef;

        coef = larger_array;

        size = number;

    }

   

    double Polynomial_T::getCoefficient(unsigned int exponent) const

    {

        if (exponent <= current_degree)

       return coef[exponent];

        return 0.0;

    }

   

    Polynomial_T Polynomial_T::derivative() const

    {

        unsigned int i;

       Polynomial_T answer;

        answer.reserve(getDegree( ));

        for (i = 1; i != 0; i = nextTerm(i))

           answer.assign_coef(i * getCoefficient(i), i-1);

        return answer;

    }

   

    double Polynomial_T::eval(double x) const

    {

        unsigned int i = 0;             // An exponent

        double power_of_x;              // x raised to the i power

        double answer = getCoefficient(0); // Sum of the polynomial's terms

        for (i = nextTerm(0); i != 0; i = nextTerm(i))

        {

                power_of_x = pow(x, i);

       answer += getCoefficient(i) * power_of_x;

        };

        return answer;

    }

    unsigned int Polynomial_T::nextTerm(unsigned int e) const

    {

        // Move e up until there is a term above it:

        while ((e < getDegree( )) && (getCoefficient(e+1) == 0.0))

       ++e;

        // If e hit degree, then no term was found and we return 0:

        return (e >= getDegree( )) ? 0 : e+1;

    }

    unsigned int Polynomial_T::previousTerm(unsigned int e) const

    {

        // Move e down until there is a term below it:

        while ((e > 0) && (getCoefficient(e-1) == 0.0))

       --e;

        // If e hit zero, then no term was found and we return UINT_MAX:

        return (e == 0) ? UINT_MAX : e-1;

    }

    Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2)

    {

       Polynomial_T answer = p1;

        unsigned int i = 0;

        answer.reserve(p2.getDegree( ) + 1);

        do

        {

       answer.add_to_coef(p2.getCoefficient(i), i);

       i = p2.nextTerm(i);

        }   while (i != 0);

        return answer;

    }

    Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2)

    {

       Polynomial_T answer = p1;

        unsigned int i = 0;

        answer.reserve(p2.getDegree( ) + 1);

        do

        {

       answer.add_to_coef(-p2.getCoefficient(i), i);

       i = p2.nextTerm(i);

        }   while (i != 0);

        return answer;

    }

   

    Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2)

    {

       Polynomial_T answer; // Will be set to p1 * p2

        unsigned int i,j; // Exponent for a term of p1 or p2

        answer.reserve(p1.getDegree( ) + p2.getDegree( ) + 1);

        i = 0;

        do

        {

       j = 0;

       do

       {

           answer.add_to_coef(p1.getCoefficient(i) * p2.getCoefficient(j), i+j);

           j = p2.nextTerm(j);

       }   while (j != 0);

       i = p1.nextTerm(i);

        }   while (i != 0);

        return answer;

    }

   

    ostream& operator << (ostream& out, const Polynomial_T& p)

    {

        unsigned int i = p.getDegree( );

        double number;

        // Each iteration of this loop prints one term of the polynomial:

        do

        {

       // Get the coefficient:

       number = p.getCoefficient(i);

       // Print a sign

       if (number < 0)

       {

           out << ((i == p.getDegree( )) ? "-" : " - ");

           number = -number;

       }

       else if (i < p.getDegree( ))

           out << " + ";

       // Print the coefficient, variable x, and exponent

       if ((number != 1.0) || (i == 0))

           out << number;

       if (i > 0)

           out << 'x';

       if (i > 1)

           out << '^' << i;

       // Move to the next lowest term:

       i = p.previousTerm(i);

        }   while (i != UINT_MAX);

out << endl;

        return out;     //return the output stream

    }

}

Explanation / Answer

Note:

The error in the problem is first try to store the header file name and the name of the header file as same.

Here, you have created with a header file name as POLY_H then the name of the file should be stored as, POLY.h.

While including the header files, include name of header file as similar that you stored with the name of the Header file name.

That is #include "POLY.h"

And the code is every complicated. Inorder to find the derivative of a polynomial, then there is an error in changing in current_degree value.

When it calls the assign_coef(2, 1) function it is unable to assign the value .

There is an error in derivative function too. Please go through the definition and flow of the functions.

For convience, here is the Polynomial function with derivative function:

Program code:

// DerivativeOfAFunction.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

#include <math.h>

using namespace std;

//function prototypes

double polynomialDerivation(double x, double c[], int n);

void definePolynomial(double c[], int n);

//main function

int main()

{

     //declare and initialisethe value of x

     double x=1.5;

     //difine the array of coefficients

     double co_effs[4]={3,2.2,-1,0.5};

     //difine the number of coefficients

     int num_of_Coeffs=4;

     //Print the output

     cout<<"The derivative of the polynomial function is: ";

     //call the definePolynomial so that it displays the

     //polynomial function

     definePolynomial(co_effs, num_of_Coeffs);

     //display the result

     cout << " = " << polynomialDerivation(x,co_effs,num_of_Coeffs) << endl;

     system("pause");

     return 0;

}

//define the polyinomial depending on the coefficients and

//type of polyonial

void definePolynomial(double c[], int n)

{   

     for(int i=0; i<n; i++) //start with 1 because the first element is constant.

     {

          if(i==0)

          {

              cout<<c[i]<<"+";

          }

          else

          {

              if(i==n-1)

                   cout<<c[i]<<"x^("<<i<<")";

              else

                   cout<<c[i]<<"x^("<<i<<")+";          

          }

     }   

}

//define the polynomial derivation that computes the value of the

//polynomial derivative and substitutes the value of x and

//the computed polynomial result is returned

double polynomialDerivation(double x, double c[], int n)

{

     double result = 0;

     double p = 1;

     int counter = 1;

     //start with 1 because the first element is constant.

     for(int i=1; i<n; i++)

     {

          result = result + c[i]*p*counter;

          counter++; // number of power

          p = p*x;

     }

     return result;

}

Sample output:

The above code displays the actual polyomial function and the derivative value of the function when x=1.5

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