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

Problem 1 Write a class that represents a polynomial. The constructor polyno ial

ID: 3778260 • Letter: P

Question

Problem 1 Write a class that represents a polynomial. The constructor polyno ial(double c 0.0) creates a polymoonial that corresponds to p(a) c The method int degree C) returns the degree of the polynomial. The method nonzeroTerns int returns the number of nonzero terms of the polymomial. For example. z4-1 has 2 noazero terms. The procedure void BetCoeff (double c int deg) sets the coefficient of the term zdrs to c. Calling this function can increase or decrease the degree of the polymomial. assert that deg is nounegative. The method double getCoeff Cint deg) returns the coefficient of the term a The return value can be o.o. In fact, it deg is larer than the degree of the polynomial, the return value must be 0.0. assert that deg is noanegative. The method double (double operator evaluates the polymomial The arithmetic operators polynorial operator (polynomial P): polynonial operator C) polynomial operator -(polynooi al p) polynorial operator (polynomial P) polynorial& operator (polyno ial P) polyno ial & operator (polynomial p) Polynorialk operator. (polynomial P): perform arithmetic operations between polynomials. The declaration and definitions of the mou-member functions std stream& operator

Explanation / Answer

#include <iostream>

using namespace std;

class Polynomial

{

private:

   int coef[100];

   int deg;       

public:

   Polynomial::Polynomial()

   {

      for ( int i = 0; i < 100; i++ )

      {

         coef[i] = 0;

      }

   }

   void set ( int a , int b )

   {

      coef[b] = a;

      deg = degree();

   }

   int degree()

   {

      int d = 0;

      for ( int i = 0; i < 100; i++ )

         if ( coef[i] != 0 ) d = i;

      return d;

   }

   void print()

   {

      for ( int i = 99; i >= 0; i-- ) {

         if ( coef[i] != 0 ) {

            cout << coef[i] << "x^" << i << " ";

         }

      }

   }

   int evaluate ( int x )

   {

      int p = 0;

      for ( int i = deg; i >= 0; i-- )

         p = coef[i] + ( x * p );

      return p;

   }

   Polynomial differentiate()

   {

      if ( deg == 0 ) {

         Polynomial t;

         t.set ( 0, 0 );

         return t;

      }

      Polynomial deriv;

      deriv.deg = deg - 1;

      for ( int i = 0; i < deg; i++ )

         deriv.coef[i] = ( i + 1 ) * coef[i + 1];

      return deriv;

   }

   Polynomial plus ( Polynomial b )

   {

      Polynomial a = *this;

      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];

      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i];

      c.deg = c.degree();

      return c;

   }

   Polynomial minus ( Polynomial b )

   {

      Polynomial a = *this;

     Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];

      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];

      c.deg = c.degree();

      return c;

   }

   Polynomial times ( Polynomial b )

   {

      Polynomial a = *this;

      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ )

         for ( int j = 0; j <= b.deg; j++ )

            c.coef[i+j] += ( a.coef[i] * b.coef[j] );

      c.deg = c.degree();

      return c;

   }

};

int main()

{

   Polynomial a, b, c, d;

   a.set ( 7, 4 );

   a.set ( 1, 2 );

   b.set ( 6, 3 );

   b.set ( -3, 2 );

   c = a.minus ( b );

   c.print();

   cout << " ";

   c = a.times ( b );

   c.print();

   cout << " ";

   d = c.differentiate().differentiate();

   d.print();

  

   cout << " ";

  

   cout << c.evaluate ( 2 ); //substitue x with 2

   cin.get();

}

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