Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this: Design a C++ class for polynomials. Your class should implement a constructor that accepts a vector of numbers and initializes the

Need help with this: Design a C++ class for polynomials. Your class should implement a constructor that accepts a vector of numbers and initializes the polynomial using it. The array contains the constants a0, a1, ...ak, in that specific order. In addition, your class should implement the following methods:

add(p) which adds another polynomial p and returns the resultant polynomial

subtract(p) which subtracts another polynomial p and returns the resultant polynomial

multiply(p) which multiplies the polynomial by another polynomial p, and returns the resultant polynomial

display() which displays the polynomial evaluate(double x) which evaluates the polynomial given a value for x, and returns the result

findRoots() which, if the polynomial is of degree 2 or less, finds and prints the roots.

I have a basic structure, but I do not seem to be able to implement the operations properly as I get an error message all the time except when printing or evaluating the polynomial:

class Polynomial {

public:

Polynomial(vector poly);

double add(vector addpoly []);

double subtract(double subspoly []);

double multiply(double multipoly[]);

void display();

double evaluate(double eval);

void findRoots();

private:

vector coefficient;

int degree;

};

Polynomial::Polynomial(vector poly){

coefficient = poly;

degree =-1;

}

double Polynomial::add(vector addpoly []){

vector sum;

for (int i=0; i

sum[i] = coefficient[i]+addpoly[i];

}

return sum;

}

double Polynomial::subtract(double subspoly []){

}

double Polynomial::multiply(double multipoly []){

}

void Polynomial::display(){

for (int i=0;i

cout<

if (i!=0){

cout<<"x^"<

}

if (i!= coefficient.size()-1){

cout<<" + ";

}

}

}

double Polynomial::evaluate(double eval){

double result = 0;

for (int i=0;i

result = result*eval + coefficient[i];

}

return result;

}

void Polynomial::findRoots(){

}

int main(){

vector array1 {5, 0, 3.5, 16, -2};

vector array2 {1, -2, 0.5};

Polynomial P1 (array1);

Polynomial P2 (array2);

P1.display();

return 0;

}

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