can I get a working example in c++ to study plement the member runcions o that t
ID: 3704563 • Letter: C
Question
can I get a working example in c++ to study
plement the member runcions o that tney nave tn Sane s ch Exerdise P12.14. Write a class Polynomial that stores a polynomial such as p(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (510),(9.7)(-10)4-10,0) Supply member functions to add, multiply, and print polynomials. Supply a con- structor that makes a polynomial from a single term. For example, the polynomial p can be constructed as Polynomial p(Term(-10, 0)): p. add (Polynomial (Term(-1, 1))): p.add (Polynomial(Term(9, 7))) p.add(Polynomial (Term(5, 10))); Then compute p(x) p(x). Polynomial q p.multiply(p) q.printO;Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
struct polynomial // polynomial declaration
{
int coefficient;
int exponent;
};
int readPoly(struct polynomial []); // functions for reading, displaying and //evaluating polynomial
int evaluation(int a,struct polynomial []);
void displayPoly( struct polynomial [],int terms);
main()
{
int a, value;
struct polynomial p[50];
cout<<" Enter polynomial details:";
a=readPoly(p);
cout<<" Polynomial is: ";
displayPoly(p,a);
value=evaluation(a,p);
cout<<" The value of polynomial is:"<<value<<endl;
return 0;
}
int readPoly(struct polynomial p[])
{
int b,i;
cout<<" Enter total number of terms in polynomial: ";
cin>>b;
cout<<" Enter coefficient and exponent "<<endl;
for(i=0;i<b;i++)
{
cout<<" Enter Coefficient("<<i+1<<"):";
cin>>p[i].coefficient;
cout<<" Enter Exponent("<<i+1<<"):";
cin>>p[i].exponent;
}
return(b);
}
int evaluation(int a,struct polynomial p[])
{
int sum=0,x;
cout<<" Enter the value of x for evaluation: ";
cin>>x;
for(int i=0;i<a;i++)
sum=sum + p[i].coefficient*pow(x,p[i].exponent);
return(sum);
}
void displayPoly(struct polynomial p[50],int n)
{
int c;
for(c=0;c<n-1;c++)
cout<<p[c].coefficient<<"(x^"<<p[c].exponent<<")+";
cout<<p[c].coefficient<<"(x^"<<p[c].exponent<<")";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.