Dynamic arrays - polynomial - C++ - completing the code...

S

strotee76

What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.

With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);

~Polynomial ();

const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);

double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);

private:
short mnTerms;
double *mpCoefficients;
};
#endif

=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;

#include "Polynomial.h"

int main ()
{
Polynomial tmp1;
Polynomial tmp2;


tmp1.setTerm(1,1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout << endl;

cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;

cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;

cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;

cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;

cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;

cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;

cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;

cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;

cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;

cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"


//constructor
Polynomial::polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}
//constructor
Polynomial::polynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial::eek:perator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;

if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients = rhs.mpCoefficients;

return *this;
}
const Polynomial Polynomial::eek:perator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients+rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}
const Polynomial Polynomial::eek:perator- (const Polynomial &rhs) const
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients-rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=-rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::eek:perator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients*=rhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (double lhs,const Polynomial &rhs)
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients*rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients*=rhs.mpCoefficients;
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{

}
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
return stream;
}
 
C

Chris Theis

strotee76 said:
What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.

I just looked through the coded quickly and found some things I'll explain
below.
With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);

~Polynomial ();

You might consider declaring the dtor virtual.
const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);

The const declarations prevents chaining. Are you sure you want that?
double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);

private:
short mnTerms;
double *mpCoefficients;
};
#endif
[SNIP]
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"


//constructor
Polynomial::polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];

Attention, there might already be some memory allocated which mpCoefficients
points to. Without checking and deallocation this will result in a memory
leak.
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}


[SNIP] I did not check the rest
double Polynomial::setTerm (short term,double coefficient)
{

}

So what is the problem? Just check whether the variable term is a valid
index and set the value of your data array to coefficient.
[SNIP]

Regards
Chris
 
R

Rolf Magnus

strotee76 said:
What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.

I didn't find many real errors, but I added a note where I would do it
different.
With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);

You probably should make the above constructor explicit to avoid
accidental conversion from short to Polynomal.
~Polynomial ();

const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);

Why do all the above operators return const objects?
And you might want to add +=, -= and *=.
double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);

Does that operator need to be a friend? In fact, it would be better if
you could write all the nonmember operators in such a way that they
don't need to be friends. That should be easy.
private:
short mnTerms;
double *mpCoefficients;
};
#endif

=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;

#include "Polynomial.h"

int main ()
{
Polynomial tmp1;
Polynomial tmp2;


tmp1.setTerm(1,1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout << endl;

No need to use endl here. '\n' would suffice. The difference is that
endl will flush the output buffer after sending a '\n', which isn't
really needed at that place. Same for the other endl's.
cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;

cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;

cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;

cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;

cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;

cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;

cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;

cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;

cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;

cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"


//constructor
Polynomial::polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}

Use initialization intead of assignment:

Polynomial::polynomial()
: mnTerms(3),
mpCoefficients(new double[mnTerms])
{
}

// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}


Polynomial::polynomial(const Polynomial &x)
: mnTerms(x.mnTerms),
mpCoefficients(new double [mnTerms])
{
// #include <algorithm> above
std::copy(x.mpCoefficients, x.mpCoefficients + mnTerms,
mpCoefficients);
}
//constructor
Polynomial::polynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}


exit() should be used very rarely, if at all. If you decide to use that
code in a bigger program, it's annoying if your code terminates the
program somewhere deep in some library code. So it's better to quit the
program by just returning from main.

Polynomial::polynomial(short nTerms)
: mnTerms(nTerms)
{
if (nTerms<0)
throw std::range_error("number of terms must be non-negative");

mpCoefficients=new double[mnTerms];

std::fill(mpCoefficients, mpCoefficients + nTerms, 0);
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial::eek:perator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;

if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients = rhs.mpCoefficients;


std::copy(rhs.mpCoefficients, rhs+mpCoefficients + i, mpCoefficients);
return *this;
}


const Polynomial Polynomial::eek:perator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;

short biggest = std::max(mnTerms, rhs.mnTerms);
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients+rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}


I would add an operator+= and implement the two others by using it.
Something like:

Polynomal& Polynomal::eek:perator+=(double rhs)
{
mpCoefficients[0]+=rhs;
}

Polynomial Polynomial::eek:perator+ (double rhs) const
{
return Polynomal(*this)+=rhs;
}

Polynomial operator+ (double lhs,const Polynomial &rhs)
{
return Polynomal(rhs)+=lhs;
}

Same of course for - and *.
const Polynomial Polynomial::eek:perator- (const Polynomial &rhs) const
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients-rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=-rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::eek:perator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients*=rhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (double lhs,const Polynomial
&rhs)
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients*rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients*=rhs.mpCoefficients;
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{


if (term < 0 || term > mnTerms)
throw std::range_error();

return mpCoefficients[term] = coefficient;


Not sure why this function would have double return type instead of
void.
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];


I suppose that should look different. If there is a mpCoefficients[0],
mnTerms must already be 1. For mpCoefficients[1] to exist, mnTerms must
be 2.
 
R

Rolf Magnus

Chris said:
// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];

Attention, there might already be some memory allocated which
mpCoefficients points to.

Huh? Did you miss the fact that this is a constructor?
 
J

Jeff Flinn

Rolf Magnus said:
I didn't find many real errors, but I added a note where I would do it
different.


You probably should make the above constructor explicit to avoid
accidental conversion from short to Polynomal.


Why do all the above operators return const objects?
And you might want to add +=, -= and *=.


Does that operator need to be a friend? In fact, it would be better if
you could write all the nonmember operators in such a way that they
don't need to be friends. That should be easy.

std::vector<double> mCoefficients;

and get rid of mnTerms, use mCoefficients.size()
};
#endif

=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;

#include "Polynomial.h"

int main ()
{
Polynomial tmp1;
Polynomial tmp2;


tmp1.setTerm(1,1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout << endl;

No need to use endl here. '\n' would suffice. The difference is that
endl will flush the output buffer after sending a '\n', which isn't
really needed at that place. Same for the other endl's.
cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;

cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;

cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;

cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;

cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;

cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;

cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;

cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;

cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;

cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"


//constructor
Polynomial::polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}

Use initialization intead of assignment:

Polynomial::polynomial()
: mnTerms(3),
mpCoefficients(new double[mnTerms])

mCoefficients(3)

Note that the values are initialized to 0.0 by default

The original method is dangerous if the declaration order of the members
were ever reversed, ie: mnTerms would be used before it was initialized.
{
}

// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients=x.mpCoefficients;
}


Polynomial::polynomial(const Polynomial &x)
: mnTerms(x.mnTerms),
mpCoefficients(new double [mnTerms])


mCoefficients(x.mCoefficients)
{}

size and values are copied
{
// #include <algorithm> above
std::copy(x.mpCoefficients, x.mpCoefficients + mnTerms,
mpCoefficients);
}
: mCoefficients(std::max(nTerms,0))
{
if( nTerms<0 ) throw myexception();
}

Again, the values are initialized to 0.0 by default

{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}


exit() should be used very rarely, if at all. If you decide to use that
code in a bigger program, it's annoying if your code terminates the
program somewhere deep in some library code. So it's better to quit the
program by just returning from main.

Polynomial::polynomial(short nTerms)
: mnTerms(nTerms)
{
if (nTerms<0)
throw std::range_error("number of terms must be non-negative");

mpCoefficients=new double[mnTerms];

std::fill(mpCoefficients, mpCoefficients + nTerms, 0);
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}


Polynomial::~Polynomial()
{}

std::vector's destructor handles this.
const Polynomial &Polynomial::eek:perator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;

if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients = rhs.mpCoefficients;


std::copy(rhs.mpCoefficients, rhs+mpCoefficients + i, mpCoefficients);


mCoefficients = rhs.mCoefficients;


Uses std::vector's operator=

In fact the remaining operators can be accomplished safely and easily by
simply forwarding to
std::vector's operators.


.... Polynomial::setTerm( ... aTerm, ... aValue )
{
if( mCoefficients.size() <= aTerm )
{
mCoefficients.resize( aTerm + 1 );

mCoefficients[aTerm] = aValue;
}
}

The rest is left as an excercise to the op.

Jeff F
return *this;
}


const Polynomial Polynomial::eek:perator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;

short biggest = std::max(mnTerms, rhs.mnTerms);
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients+rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}


I would add an operator+= and implement the two others by using it.
Something like:

Polynomal& Polynomal::eek:perator+=(double rhs)
{
mpCoefficients[0]+=rhs;
}

Polynomial Polynomial::eek:perator+ (double rhs) const
{
return Polynomal(*this)+=rhs;
}

Polynomial operator+ (double lhs,const Polynomial &rhs)
{
return Polynomal(rhs)+=lhs;
}

Same of course for - and *.
const Polynomial Polynomial::eek:perator- (const Polynomial &rhs) const
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients-rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients=-rhs.mpCoefficients;
return answer;
}
const Polynomial Polynomial::eek:perator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::eek:perator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients*=rhs;
return answer;
}
const Polynomial Polynomial::eek:perator* (double lhs,const Polynomial
&rhs)
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients=mpCoefficients*rhs.mpCoefficients;
else if (i<mnTerms)
answer.mpCoefficients=mpCoefficients;
else
answer.mpCoefficients*=rhs.mpCoefficients;
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{


if (term < 0 || term > mnTerms)
throw std::range_error();

return mpCoefficients[term] = coefficient;


Not sure why this function would have double return type instead of
void.
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];


I suppose that should look different. If there is a mpCoefficients[0],
mnTerms must already be 1. For mpCoefficients[1] to exist, mnTerms must
be 2.
return stream;
}
 
C

Chris Theis

Rolf Magnus said:
Chris said:
// copy constructor
Polynomial::polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];

Attention, there might already be some memory allocated which
mpCoefficients points to.

Huh? Did you miss the fact that this is a constructor?

Oh darn..., I must have been dreaming with my eyes wide open. I guess this
is a sign not to touch anything sensitive anymore today :)

Thanks Rolf.

Chris
 
S

strotee76

Thanks to everyone who has replied. This is the 1st time I submitted
anything to this group, so thanks again.

I was able to update the code to get it to compile, however, it
immediately crashes. Here is a link to the updated code. I'll be
honest, this code is for an assignment in class, I cannot change or
add any declarations to the header file.

http://cpp.enisoc.com/pastebin/?3453

Anymore suggestions are welcome. Thanks for your time.
 
B

Brian M. Dean

What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.

One obvious point. You want to allocate one more element
for your polynomial than the degree of that polynomial.
For example: the quadratic equation
4.5x^2 - 3.6x + 7.1
would need to have a double array of 3 and not 2 to
store 4.5, 3.6 and 7.1 . The same goes for a polynomial
of any other degree.
 
B

Bill Shortall

Brian M. Dean said:
One obvious point. You want to allocate one more element
for your polynomial than the degree of that polynomial.
For example: the quadratic equation
4.5x^2 - 3.6x + 7.1
would need to have a double array of 3 and not 2 to
store 4.5, 3.6 and 7.1 . The same goes for a polynomial
of any other degree.

Hi Brian,
It's nice to define a () operator for
polynomial evaluation rather than eval()
Then you can do something like

Polynomial foo(6);
set the coeffs
double a = foo(1.23) ;

also you need to worry about complex
coeffs and evaluation at complex points

Bill
 
B

Brian M. Dean

Hi Brian,
It's nice to define a () operator for
polynomial evaluation rather than eval()
Then you can do something like

Polynomial foo(6);
set the coeffs
double a = foo(1.23) ;

also you need to worry about complex
coeffs and evaluation at complex points


The best way to handle that is with templates so
that instead of double, you can have a polynomial
with any coefficient you want. I would do that
myself except I am lazy, and double is good enough
for what I am doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top