Hello all. I was hoping for some input on this program I have that is supposed to take two inputed polynomials and add them together (also multiply by scalar) and return the result. It SHOULD look like this:
"
Enter a polynomial.
How many coefficients will be in the first polynomial? 3
Enter the coefficients, in order of increasing power of x:
1.0 2.0 3.0
Enter a value at which to evaluate the polynomial: 2.0
1 + 2 x + 3 x^2
at x=2 evaluates to 17
Enter a second polynomial, in the same format
(# of coefficients, then each coefficient in order of increasing power of x).
4 2 3 4 2
Enter a scaling factor (a floating point number): 0.5
(1 + 2 x + 3 x^2) * 0.5 = 0.5 + 1 x + 1.5 x^2
1 + 2 x + 3 x^2
+
(2 + 3 x + 4 x^2 + 2 x^3)
= 3 + 5 x + 7 x^2 + 2 x^3
1 + 2 x + 3 x^2
+
0.5 * (2 + 3 x + 4 x^2 + 2 x^3)
= 2 + 3.5 x + 5 x^2 + 1 x^3
This polynomial has order 3 and, at x=2, evaluates to 37"
However, my output when using the same inputs looks like this:
"
Enter a polynomial.
How many coefficients will be in the first polynomial? 3
Enter the coefficients, in order of increasing power of x:
Enter a value at which to evaluate the polynomial: 2.0
5.8851e-315 + 2x
at x = 2 evaluates to 4
Enter a second polynomial, in the same format
(# of coefficients, then each coefficient in order of increasing power of x).
4 2 3 4 2
Enter a scaling factor <a floating point number>: 0.5
<8.08389e-308 + 1 x> * 0.5 = 8.0839e-308 + 1x
8.0839e-308 + 1x + <8.01092e-307 + 6.67536e-308 x + 2.22521e-306 x^2 + 2.08601e-308 x^2> = 0
"
Followed by a lovely program crash. Below are my header / cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
/**
This class implements a polynomial of the form
c0 + c1 x + c2 x^2 + c3 x^3 + ...
As a simplification, this class will be limited to polynomials
of order 99 or less. (The order of a polynomial is the largest
power of x having a non-zero coefficient, or zero if the polynomial
has all zero coefficients.) Note that adding polynomials
together or scaling polynomials (multiplying by a constant) could
reduce the order of the result.
*/
class Polynomial {
public:
// Create a simple polonomial
Polynomial ();
// Create a polynomial with nCoeff coefficients,
// drawn from the coeff array.
//
// E.g.,
// double c[3] = {1.0, 2.0, 3.0};
// Polynomial p (3, c);
// creates a polynomial p representing: 1.0 + 2.0*x + 3.0*x^2
//
Polynomial (int nCoeff, double coeff[]);
// Create a polynomial with nCoeff coefficients,
// all set equal to c.
//
// E.g.,
// Polynomial p (3, 1.0);
// creates a polynomial p representing: 1.0 + 1.0*x + 1.0*x^2
//
Polynomial (int nCoeff, double c);
// Compute the value of this polynomial at a given value of x.
// E.g.,
// double c[3] = {1.0, 2.0, 3.0};
// Polynomial p (3, c);
// cout << p.evaluate(2.0);
// will print 17.0
double evaluate (double x) const;
// The order of a polynomial is the largest exponent of x with a
// non-zero coefficient. E.g., for the sample polynomial used
// above, the order is 2.
int getOrder() const;
// Add two polynomials, returning the polynomial representing
// their sum.
Polynomial operator+ (const Polynomial& p) const;
// Multiply a polynomial by a floating point value.
Polynomial operator* (double scale) const;
private:
int order;
double* coefficients; // array of coefficients.
// Positions [0..order] are significant
// All positions > order are ignored and
// assumed to be zero.
friend std::istream& operator>> (std::istream&, Polynomial&);
friend std::ostream& operator<< (std::ostream&, const Polynomial&);
// Internal utility function - coputes and remembers the order
// of a polynomial. Use this at the end of any function that can change
// the coefficients of the polynomial.
void checkOrder();
};
// Read a polynomial from an istream.
// Polynomials are input in the format:
// #coefficients c0 c1 ...
// where the ci are floating point coefficients of x^i
std::istream& operator>> (std::istream&, Polynomial&);
// Write a polynomial to an ostream
std::ostream& operator<< (std::ostream&, const Polynomial&);
inline
Polynomial operator* (double scale, const Polynomial& p)
{ return p * scale; }
/* Note: a member function of the Polynomial class must have a polynomial
as the first argument (e.g., poly.operator*(x) <===> poly * x). This
function simply allows for the multiplication to be written with the
polynomial on the right. */
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include "polynomial.h"
using namespace std;
Polynomial::Polynomial ()
: order(0)
{
coefficients = new double[1];
coefficients[0] = 0.0;
}
Polynomial::Polynomial (int nCoeff, double coeff[])
: order(1)
{
checkOrder();
}
Polynomial::Polynomial (int nCoeff, double c)
: order(nCoeff-1)
{
coefficients = new double[nCoeff];
for (int i = 0; i < nCoeff; ++i)
coefficients[i] = c;
checkOrder();
}
void Polynomial::checkOrder ()
{
while (order > 0 && coefficients[order] == 0.0)
--order;
}
double Polynomial::evaluate (double x) const
{
double sum = 0.0;
for (int i = 0; i <= order; ++i)
sum = x * sum + coefficients[order - i];
return sum;
}
int Polynomial::getOrder() const
{
return order;
}
Polynomial Polynomial::operator+ (const Polynomial& p) const
{
Polynomial result (1, 0.0);
result.checkOrder();
return result;
}
Polynomial Polynomial::operator* (double scale) const
{
Polynomial result (*this);
for (int i = 0; i <= order; ++i)
result.coefficients[i] = scale * coefficients[i];
return result;
}
std::istream& operator>> (std::istream& in, Polynomial& p)
{
int r;
in >> r;
Polynomial result(r, 1.0);
result.order = r - 1;
for (int i = 0; i < r; ++i)
in >> result.coefficients[i];
result.checkOrder();
p = result;
return in;
}
std::ostream& operator<< (std::ostream& out, const Polynomial& p)
{
for (int i = 0; i <= p.order; ++i)
{
if (i > 0)
{
out << " + ";
}
out << p.coefficients[i];
if (i > 0)
{
out << " x";
if (i > 1)
{
out << "^" << i;
}
}
}
return out;
}
|
Now, I can plainly see that some of these class declarations are not implemented so we can avoid that smack in the face lol. What I would like is some suggestions / examples so that I may properly implement this code. I would also appreciate links to other discussions that relate to this matter if you would prefer to do that instead.
I will also put in my two cents of what I think I should do.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// Create a polynomial with nCoeff coefficients,
// drawn from the coeff array.
//
// E.g.,
// double c[3] = {1.0, 2.0, 3.0};
// Polynomial p (3, c);
// creates a polynomial p representing: 1.0 + 2.0*x + 3.0*x^2
Polynomial::Polynomial (int nCoeff, double coeff[]): order(1)
{
coefficients = new double [nCoeff]; //will allocate some heap memory for the new polynomial (i think)
for (int i = 0; i < nCoeff; i++)
coefficients [i] = coeff[];
checkOrder ();}
|
I believe I should also throw in a ~Polynomial and delete [] coefficients somewhere to avoid memory leaks. Though I have the basic ides I struggle where to begin because I did not generate this code from scratch. It boggles me to look through the code and try to figure out what everything is as it didn't come from my head. Again, any guidance would be appreciated. Thanks.