Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to solve this c++ question. #include #include using namespace std; class Term { private: int coef; int exp; public: Term(int c = 0,

Please help to solve this c++ question.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

#include

#include

using namespace std;

class Term

{

private:

int coef;

int exp;

public:

Term(int c = 0, int e = 0);

void set(int c, int e);

int evaluate(int x) const;

int exponent() const;

int coefficient() const;

};

class Polynomial

{

private:

public:

Polynomial();

void input();

int evaluate(int x) const;

Term largestTerm() const;

int degree() const;

};

//----------------------------------------------------------------------------

int main()

{

cout

cout

cout

cout

for (int x = 0; x

cout

cout

system("pause");

return 0;

}

//----------------------------------------------------------------------------

// class Term

// The constructor is given

Term::Term(int c, int e) : coef(c), exp(e) {}

// Implement the other methods

void Term::set(int c, int e) {}

int Term::exponent() const {}

int Term::coefficient() const {}

int Term::evaluate(int x) const {}

//----------------------------------------------------------------------------

// class Polynomial

// Implement the constructor and the other methods of the class Polynomial

Question In this exercise you will be adopting the concept of composition to model polynomials. A polynomial is a mathematical function composed of unit expressions called terms. The following is an example of a single-variable polynomial. 5x2 - 2x + 7 Notes: a polynomial may have one, two or more variables. However, in this exercise, the scope is limited only to single-variable polynomials. Each term of a polynomial contains a coefficient and an exponent. The polynomial from the above example has three terms and their coefficient and exponent are shown in Table 1. Table 1 Term Coefficient Exponent 5r 5 2 -2x -2 1 7 7 0 Then, a single-variable polynomial can be modeled with two classes as shown in Figure 1 and their descriptions are given in Table 2. Polynomial Term coef Polynomial (...) exp input() Term (...) Jevaluate (x) set (...) largest Term 0 coefficient() degree() exponent 0 Jevaluate (x) Figure 1 Table 2 Description Class Members (attributes / methods) class Term coef and exp The attributes for the terms's coefficient and exponent, respectively. Term) The constructor (s) such as overloaded, default constructor, etc. set (c, e) sets the term's attributes, coef and exp respectively. return the term's attributes, coef and exp respectively. coefficient () and exponent() evaluate (x) evaluates the term with the value of x. For example, ifx= 2, then the term 5x will evaluate to 5(22) = 20, and the term -2x will evaluate to -2(2) = -4. Notes: Use the math function, pow() to implement this method. class Polynomial input() adds the list of terms of a polynomial using user inputs. The user needs to enter the coefficient and exponent for each term. evaluate (x) evaluates the polynomial by summing up all the terms based on the value of x. For example, if x=2, then the polynomial 5x2 - 2x + 7 will evaluate to 20 - 4 + 7 = 23. largestTerm() returns the term whose the largest exponent. For example, the largest term in the polynomial 5x2 - 2x + 7 is 5x2,5 + x is x?, x2 - 4x + x is - 4x and so on. degree() returns the degree of a polynomial by taking the largest exponent. For example, the degree of the polynomial 5x - 2x + 7 is 2, x- 5 is 3, 9x is 1 and so on. Notes: You may want to use the method largest Term() here. Based on the classes above, modify the codebase (exercise.cpp) to achieve the goal of the program. Do the following tasks: Notes: Separate the class declaration and definition in the same file. 1. Implement the class Term. Note that, the class's attributes and a constructor have been given in the program. Complete the other methods. Also, you do not need to add additional members for this class. 2. Implement the class Polynomial. Add all required attributes and methods to the class. 3. In the main function, write the code to: a. create a Polynomial object and add terms to the polynomial using user inputs. b. print the degree of the polynomial onto the screen. c. evaluate the polynomial with different values of x from 0 to 5 and print the results onto the screen. Output: Expected result from the program is as shown in Figure 2. Bold text indicates keyboard inputs. Run 1 - user enters the polynomial 3.x2 Enter a polynomial: How many terms ? => 1 Enter term #1 (coef and exp) => 3 2 Polynomial's degree = 2 X Polynomial value OHM 0 3 12 27 48 75 ON 4 5 Run 2 - user enters the polynomial X - 10 Enter a polynomial: How many terms? => 2 Enter term #1 (coef and exp) => 1 1 Enter term #2 (coef and exp) => -10 0 Polynomial's degree - 1 Polynomial value 1 2 3 UN PO -10 -9 -8 -7 -6 -5 5 Run 3 - user enters the polynomial -5x2 + x + 1 - 7x Enter a polynomial: How many terms ? => 4 Enter term #1 (coef and exp) => -5 2 Enter term #2 (coef and exp) => 1 3 Enter term #3 (coef and exp) => 1 0 Enter term #4 (coef and exp) => -7 1 Polynomial's degree = 3 Polynomial value x UWNPO 0 1 2 3 4 5 1 -10 -25 -38 -43 -34 Figure 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions