I need to create a program that uses a class and main function to manipulate pol
ID: 3711415 • Letter: I
Question
I need to create a program that uses a class and main function to manipulate polynomials in C++. The sample answer should look like this
1 Enter the degree of the polynomial -> 3
2
3 Enter the coefficients -> 4 1 0 1
4 f(x) = 4x^3 + x^2 + 1
5
6 Enter a value at which to evaluate this polynomial -> 3
7 f(3) = 118
8
9 f'(x) = 12x^2 + 2x
10 Integrate [f(x),x] = x^4 + 0.333333 x^3 + x + 1
11
12 Enter the upper and lower limits of the integral -> 3 0
13 Integrate [f(x),x ,3 ,0] = 93
So far, this is the code I have. It's incomplete, and I'm unsure of how classes work.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
//An abstract data type represent a polynomial
class Polynomial
{
private:
//This polynomial in degree-coefficients format
double* poly;
public:
//Creates the zero polynomial
Polynomial()
{ ... }
/*Creates a polynomial using the contents of the specified array.
* @param p an array in degree-coefficients format */
Polynomial(const double[] p)
{ ... }
/* Deallocates the memory for this polynomial */
~Polynomial() { ... }
/** * Gives the degree of this polynomial * @return the degree of this polynomial */
int degree() const
{
int n = poly[0];
return n;
}
/** * Evaluates this polynomial at the specified value.
* @param x numeric value at which the polynomial is to be evaluated.
* @return the value of the polynomial when evaluated with x */
double eval(double x) const
{
int degree = poly[0];
double result = 0;
int n = 1;
int i;
for (i=degree; i > 0; i--)
{
result += poly[i]*(pow(x,n));
n++;
}
result += poly[degree+1];
return result;
}
string str() const
{
stringstream ss;
if (poly[0] == 0)
{
ss << poly[1];
}
if (poly[0] == 1)
{
int a = poly[1];
int b = poly[2];
if (b > 0)
{
if (a == 1)
ss << "x + " << b;
else if (a == -1)
ss << "-x + " << b;
else
ss << a << "x + " << b;
}
else if (b < 0)
{
if (a == 1)
ss << "x " << b;
else if (a == -1)
ss << "-x " << b;
else
ss << a << "x " << b;
}
else
if (a == 1)
ss << "x";
else if (a == -1)
ss << "-x";
else
ss << a << "x";
}
if (poly[0] > 1)
{
int degree = poly[0];
int i;
if (poly[1] == 1)
ss << "x^" << degree;
else if (poly[1] == -1)
ss << "-x^" << degree;
else
ss << poly[1] << "x^" << degree << " ";
for (i = 2; i <= degree-1; i++)
{
if (poly[i] > 0)
{
if (poly[i] == 1)
ss << " + " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " + " << "-x^" << degree - i+1 << " ";
else
ss << " + " << poly[i] << "x^" << degree - i+1 << " ";
}
else if (poly[i] < 0)
{
if (poly[i] == 1)
ss << " - " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " - " << "-x^" << degree - i+1 << " ";
else
ss << " - " << poly[i] << "x^" << degree - i+1 << " ";
}
else
;
}
int a = poly[i];
int b = poly[i + 1];
if (b > 0)
{
if (a > 0)
{
if (a == 1)
ss << "+ x + " << b;
else
ss << "+ " << a << "x + " << b;
}
else if (a < 0)
{
if (a == -1)
ss << "- x + " << b;
else
ss << a << "x + " << b;
}
else
ss << "+ " << b;
}
else if (b < 0)
{
{
if (a > 0)
{
if (a == 1)
ss << "+ x +- " << b;
else
ss << "+ " << a << "x - " << b;
}
else if (a < 0)
{
if (a == -1)
ss << "- x - " << b;
else
ss << "- " << a << "x - " << b;
}
else
ss << b;
}
}
else
if (a > 0)
{
if (a == 1)
ss << "+ x";
else
ss <<"+"<< a << "x";
}
else if (a < 0)
{
if (a == -1)
ss << "- x";
else
ss << a << "x";
}
else
;
}
return ss.str();
}
Polynomial differentiate() const
{
int degree = poly[0];
double *result = new double[degree+1];
*result = degree-1;
int n = degree;
int i;
for (i = 1; i <= degree; i++)
{
result[i] = poly[i]*n;
n--;
}
return result;
delete[] result;
}
Polynomial integrate(double c) const
{
int degree= poly[0];
int i;
double *respoly = new double[degree+3];
*respoly = degree+1;
int n = degree;
for (i = 1; i <= degree+1; i++ )
{
respoly[i] = poly[i] / (n+1);
n--;
}
degree = poly[0]+2;
respoly[i] = c;
return respoly;
delete[] respoly;
}
};
int main()
{
cout << "Enter the degree of the polynomial -> ";
int degree;
cin >> degree;
int size = degree+1;
cout << "Enter the coefficients -> ";
double *poly = new double[size+1];
*poly = degree;
int i;
for (i = 1; i <= size; i++)
{
cin >> poly[i];
}
cout << "f(x) = " << polyToString(poly) << endl<< endl;
cout << "Enter a value at which to evaluate this polynomial -> ";
double x;
cin >> x;
cout << "f(" << x << ") = " <<setprecision(6)<< polyEval(poly, x)<< endl<< endl;
double* prime = NULL;
prime = differentiate(poly);
cout << "f’(x) = " << polyToString(prime) << endl;
delete[] prime;
double* integral = NULL;
integral = integrate(poly,poly[degree+2]);
cout << "Integrate [f(x),x] = "<<setprecision(6) << polyToString(integral) << endl << endl;
double xmax, xmin;
cout << "Enter the upper and lower limits of the integral ->";
cin >> xmax >> xmin;
cout << "Integrate [f(x) ,x ,"<<xmax<<" ,"<<xmin<<"] = "<<polyEval(integral,xmax)-polyEval(integral,xmin);
return 0;
}
Explanation / Answer
here is your program : --------------->>>>>>>>>>>>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
//An abstract data type represent a polynomial
class Polynomial
{
private:
//This polynomial in degree-coefficients format
double* poly;
public:
//Creates the zero polynomial
Polynomial( ){}
/*Creates a polynomial using the contents of the specified array.
* @param p an array in degree-coefficients format */
Polynomial(double p[]){
poly = p;
}
/* Deallocates the memory for this polynomial */
~Polynomial() { delete[] poly;}
/** * Gives the degree of this polynomial * @return the degree of this polynomial */
int degree() const{
int n = poly[0];
return n;
}
/** * Evaluates this polynomial at the specified value.
* @param x numeric value at which the polynomial is to be evaluated.
* @return the value of the polynomial when evaluated with x */
double eval(double x) const{
int degree = poly[0];
double result = 0;
int n = 1;
int i;
for (i=degree; i > 0; i--){
result += poly[i]*(pow(x,n));
n++;
}
result += poly[degree+1];
return result;
}
string str() const{
stringstream ss;
if (poly[0] == 0){
ss << poly[1];
}
if (poly[0] == 1){
int a = poly[1];
int b = poly[2];
if (b > 0){
if (a == 1)
ss << "x + " << b;
else if (a == -1)
ss << "-x + " << b;
else
ss << a << "x + " << b;
}
else if (b < 0){
if (a == 1)
ss << "x " << b;
else if (a == -1)
ss << "-x " << b;
else
ss << a << "x " << b;
}
else if (a == 1)
ss << "x";
else if (a == -1)
ss << "-x";
else
ss << a << "x";
}
if (poly[0] > 1){
int degree = poly[0];
int i;
if (poly[1] == 1)
ss << "x^" << degree;
else if (poly[1] == -1)
ss << "-x^" << degree;
else
ss << poly[1] << "x^" << degree << " ";
for (i = 2; i <= degree-1; i++){
if (poly[i] > 0){
if (poly[i] == 1)
ss << " + " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " + " << "-x^" << degree - i+1 << " ";
else
ss << " + " << poly[i] << "x^" << degree - i+1 << " ";
}
else if (poly[i] < 0){
if (poly[i] == 1)
ss << " - " << "x^" << degree - i+1 << " ";
else if (poly[i] == -1)
ss << " - " << "-x^" << degree - i+1 << " ";
else
ss << " - " << poly[i] << "x^" << degree - i+1 << " ";
}
}
int a = poly[i];
int b = poly[i + 1];
if (b > 0){
if (a > 0){
if (a == 1)
ss << "+ x + " << b;
else
ss << "+ " << a << "x + " << b;
}
else if (a < 0){
if (a == -1)
ss << "- x + " << b;
else
ss << a << "x + " << b;
}
else
ss << "+ " << b;
}
else if(b < 0){
if (a > 0){
if (a == 1)
ss << "+ x +- " << b;
else
ss << "+ " << a << "x - " << b;
}
else if (a < 0){
if (a == -1)
ss << "- x - " << b;
else
ss << "- " << a << "x - " << b;
}
else
ss << b;
}
else if (a > 0){
if (a == 1)
ss << "+ x";
else
ss <<"+"<< a << "x";
}
else if (a < 0){
if (a == -1)
ss << "- x";
else
ss << a << "x";
}
}
return ss.str();
}
double* differentiate() const{
int degree = poly[0];
double *result = new double[degree+1];
*result = degree-1;
int n = degree;
int i;
for (i = 1; i <= degree; i++){
result[i] = poly[i]*n;
n--;
}
return result;
}
double* integrate(double c) const{
int degree= poly[0];
int i;
double *respoly = new double[degree+3];
*respoly = degree+1;
int n = degree;
for (i = 1; i <= degree+1; i++ ){
respoly[i] = poly[i] / (n+1);
n--;
}
degree = poly[0]+2;
respoly[i] = c;
return respoly;
}
};
int main()
{
cout << "Enter the degree of the polynomial -> ";
int degree;
cin >> degree;
int size = degree+1;
cout << "Enter the coefficients -> ";
double *poly = new double[size+1];
*poly = degree;
int i;
for (i = 1; i <= size; i++){
cin >> poly[i];
}
Polynomial pol(poly);
cout << "f(x) = " <<pol.str()<< endl<< endl;
cout << "Enter a value at which to evaluate this polynomial -> ";
double x;
cin >> x;
cout << "f(" << x << ") = " <<setprecision(6)<<pol.eval(x)<< endl<< endl;
double* pr;
pr = pol.differentiate();
Polynomial prime(pr);
cout << "f’(x) = " <<prime.str()<< endl;
double *in;
in = pol.integrate(0);
Polynomial integral(in);
cout << "Integrate [f(x),x] = "<<setprecision(6) <<integral.str()<< endl << endl;
double xmax, xmin;
cout << "Enter the upper and lower limits of the integral ->";
cin >> xmax >> xmin;
cout << "Integrate [f(x) ,x ,"<<xmax<<" ,"<<xmin<<"] = "<<integral.eval(xmax)-integral.eval(xmin);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.