You will implement and test a polynomial class, using fixed-sized array as the d
ID: 3726701 • Letter: Y
Question
You will implement and test a polynomial class, using fixed-sized array as the data structure to store the terms in a polynomial. Each term in the polynomial is also a class that you will need to implement. Recall that the general form of a polynomial is as follows: P(x) = anx" + an-1x"-| + + aix + ao Here, each term has a coefficient, denoted as a, and a exponent i, which represent the power of the variable x. Your polynomial class must implement the following operations: Addition-given two polynomials P and P , compute the polynomial F-P + Subtraction-given two polynomials P and R, compute the polynomial -R- 3. Multiplication - given two polynomials P and P, compute the polynomial P,-P*P 5. Indefinite integral -given a polynomial P, finds its indefinite integral (anti-derivative). 1. 2 4. Derivative- given a polynomial P, finds its derivative. 6. Definite integral given a polynomial P, evaluate its definite integral over an interval [a,b] 7. 8. Degree-given a polynomial P, find its degree (the largest exponent in any term). Evaluate- given a polynomial P, evaluate it at value x, to compute y- P(r) You must first implement the polyterm class, which implements a term in a polynomial. You will use a fixed-size array to store the terms in the polynomial, in decreasing order of exponent. Thus, each element in the array represents a term in the polynomial. The array will have a maximum size CAPACITY that limits the number of terms that can be added to the polynomial.Explanation / Answer
# define CAPACITY 100
class poly
{
int degree;
float coef[CAPACITY];
public:
void getdata();
void putdata();
poly addition(poly,poly);
poly subtraction(poly,poly);
poly multiplication(poly,poly);
poly derivative(poly);
};
void poly::getdata()
{
cout<<”Enter the degree of the polynomial:”<<endl;
cin>>degree;
cout<<”Enter the coefficients in descending order of exponent:”<<endl;
for(int i=degree; i>=0;i--)
cin>>coef[i];
}
void poly::putdata()
{
cout<<”The coefficients of the resultant polynomial in the descending order of exponenet:”<<endl;
for(int i=degree;i>=0;i--)
cout<<coef[i]<<” “;
cout<<endl;
}
poly poly::addition(poly t1, poly t2)
{
int min, max;
poly temp;
if(t1.degree<=t2.degree)
{
min=t1.degree;
max=t2.degree;
temp=t2;
}
else
{
min=t2.degree;
max=t1.degree;
temp=t1;
}
for(int i=0;i<=min;i++)
temp.coef[i]=t1.coef[i]+t2.coef[i];
return temp;
}
poly poly::subtraction(poly t1, poly t2)
{
int min, max;
poly temp;
if(t1.degree<=t2.degree)
{
min=t1.degree;
max=t2.degree;
temp=t2;
}
else
{
min=t2.degree;
max=t1.degree;
temp=t1;
}
for(int i=0;i<=min;i++)
temp.coef[i]=t1.coef[i]-t2.coef[i];
return temp;
}
poly poly::multiplication(poly t1,poly t2)
{
poly temp;
temp.degree=t1.degree+t2.degree;
for(int i=0;i<=temp.degree;i++)
temp.coef[i]=0;
for(int i=0;i<=t1.degree;i++)
for(int j=0;j<=t2.degree;j++)
temp.coef[i+j]=temp.coef[i+j+(t1.coef[i]*t2.coef[j]);
return temp;
}
void main()
{
poly p1,p2,p3;
p1.getdata();
p2.getdata();
p3=addition(p1,p2);
cout<<”Addition result:”<<endl;
p3.putdata();
p3=subtraction(p1,p2);
cout<<”Subtraction result:”<<endl;
p3.putdata();
p3=multiplication(p1,p2);
cout<<”Multiplication result:”<<endl;
p3.putdata();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.