Define class Polynomial with two private variables: int size; and double *ptr; I
ID: 3619561 • Letter: D
Question
Define class Polynomial with two private variables: int size; and double *ptr;In the public part, define these two constructors and two functions print and add as below:
public:
polynomial (); // Default constructor to set a zero polynomial
polynomial (double [], int); // set a polynomial by an array
void print (); // Print the polynomial's coefficients
void add (polynomial); // add a second polynomial to the first polynomial
Note a polynomial is represented by the set of its coefficients (which is an array); and you can start from the highest degree coefficient or from the lowest degree coefficient (the constant term).
Printing a polynomial sounds easy by just putting the ^ after each term and put + to connect the terms. Actually it is not easy if you want to make it meaningful.
Printing a polynomial is like this:
double g[] = {1, 2, 3, 4}; // Polynomial g(x) = x^3 + 2x^2 + 3x + 4
// Note {1, 2, 3, 4} can also mean 4x3 + 3x2 + 2x +1 depending on whether the first coefficient is the coefficient of the highest degree term or the constant term.
This is not trivial or easy since for x3 + 2x2 + 3x +4, you do not want to print like 1x^3 + 2x^2 + 3x + 4 with 1 in front of x^3.
For g[] = {1, -1, 1, -1}, you do not want to print like 1x^3 + -1 x^2 + 1x + -1, but more intelligently x^3 – x^2 + x -1. Also terms with 0 coefficients should be suppressed when printing.
Adding two polynomials is easy if the two polynomials have different degrees. It is not easy if they have the same degrees since adding two cubic polynomials can result in a quadratic polynomial or linear polynomial if the highest coefficients cancel.
So basically, we are given the header file (the declarations and initiations) and the output file and we have to fill in the file in between.
Header file:
class polynomial {
public:
polynomial (); // Default constructor to set a zero polynomial
polynomial (double [], int);
void print ();
void add (polynomial);
private:
int size;
double *ptr;
};
Output file (Used to test the class polynomial):
#include
#include "polynomial.h"
int main () {
// double g[] = {1, 2, 3, 4}; // Polynomail g(x) = x^3 + 2x^2 + 3x + 4
double g[] = {1, -2, 0, -4};
polynomial f;
polynomial f2(g, sizeof(g)/sizeof(double));
f2.print();
}
And we have to write whats in between. If you are not familiar with header files and such, you can combine all of this into one program to look like this:
#include
class polynomial {
public:
polynomial (); // Default constructor to set a zero polynomial
polynomial (double [], int); // set a polynomial by an array
void print (); // Print the polynomial's coefficents
void add (polynomial); // add a second polynomial to the first polynomial
private:
int size; // size of polynomial coefficient array
double *ptr; // pointer to the polynomial array
};
int main () {
// double g[] = {1, 2, 3, 4}; // Polynomail g(x) = x^3 + 2x^2 + 3x + 4
double g[] = {1, -2, 0, -4};
polynomial f;
polynomial f2(g, sizeof(g)/sizeof(double));
f2.print();
}
And the challenge is to figure out what goes in between.
Explanation / Answer
please rate - thanks it works if the arrays are different sizes, that's why I had to use malloc and realloc.if the 2nd array was larger then the first, the first had to be made larger message me if any problems
#include <iostream>
using namespace std;
class polynomial
{private:
double *ptr;
int size;
public:
polynomial (); // Default constructor to set a zero polynomial
polynomial (double [], int); // set a polynomial by an array
void print (); // Print the polynomial's coefficients
void add (polynomial); // add a second polynomial to the first polynomial
};
int main()
{double g[] = {1, 5, 3, 5};
double h[]={1, -2, 0, -4};
polynomial f;
polynomial f2(g, sizeof(g)/sizeof(double));
polynomial f3(h, sizeof(h)/sizeof(double));
cout<<" ";
f2.print();
cout<<"+";
f3.print();
f2.add(f3);
cout<<"----------------- ";
f2.print();
system("pause");
return 0;
}
polynomial::polynomial()
{
size=0;
//ptr = new double [size];
ptr=(double *)malloc(size*sizeof(double));
}
polynomial::polynomial (double a[], int n)
{int i;
size=n;
//ptr = new double [size];
ptr=(double *)malloc(size*sizeof(double));
for(i=0;i<n;i++)
ptr[i]=a[i];
}
void polynomial ::print()
{int i;
cout<<" ";
for(i=0;i<size-1;i++)
{if(ptr[i]!=1&&ptr[i]!=0)
if(ptr[i]==-1)
cout<<"-";
else
cout<<ptr[i];
if(ptr[i]!=0)
cout<<"x";
if(size-i-1>1&&ptr[i]!=0)
cout<<"^"<<size-i-1;
if(i!=size-1)
if(ptr[i+1]>0)
cout<<"+";
}
if(size>0&&ptr[size-1]!=0)
cout<<ptr[size-1];
cout<<endl<<endl;
}
void polynomial::add (polynomial a)
{int i,j=0,n;
//for(i=0;i<size;i++)
// cout<<a.ptr[i]<<" ";
// cout<<endl;
if(size==a.size)
for(i=0;i<size;i++)
ptr[i]=ptr[i]+a.ptr[i];
else if(size<a.size)
{
n=a.size-size;
ptr = (double*) realloc (ptr,a.size * sizeof(double));
for(i=size;i>=0;i--)
ptr[i+n]=ptr[i];
for(i=0;i<n;i++)
ptr[i]=0;
size=a.size;
for(i=0;i<size;i++)
ptr[i]=ptr[i]+a.ptr[i];
}
else
for(i=size-a.size;i<size;i++)
ptr[i]=ptr[i]+a.ptr[j++];
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.