Okay, so I have created and array of a maximum degree, which the user defines, and then inputs their own coefficients according to the size of the polynomial. For example, I ask for degree, they type 2, and then input 1,3 and 5 to create a polynomial:

1 + 3x + 5x^2

We have to initialize a function which takes an int and evaluates the expression, the easiest way I have found is using <cmath> and the pow(a,b) function. Below is my code to evaluate, but I keep getting errors. I know I have to convert my "int x" into a double, and then back into an int when I return a value. The function has to remain an int due to the nature of the assignment:

``````int Poly::evaluate(int x)
{
int total = 0;
double(x);
for(int i = 0; i <= degree; i++){
total += coefs[i]*pow(x,i); //coefs[] array is the array in which the coef's are stored
}
}``````

Ok in the first instance, using power is plain ugly and inefficient. The typical route for a polynomial in the positive sense, is to do something like this:

``````int total(0);
int xpow(1);
for(int i=0;i<=degree;i++)
{
total+=coef[i]*xpow;
xpow*=x;
}``````

You can also do it via an internal bracket …

## All 4 Replies

Also, We need to be able to multiply 2 polynomials together, I have written what I think is correct, but when I run in in a client, I get some funky numbers

``````Poly::Poly operator*(const Poly& p)
{
Poly temp;
temp.degree = degree + p.degree;
if(temp.degree > 99)
cout << "cannot evaluate";
for(int i = 0; i < degree; i++){
for(int j =0; j < p.degree; j++){
temp.coefs[i+j] += coefs[i]*p.coefs[j];
}
}
return temp;
}
``````

here is my display function, in case something went wacky there

``````void Poly::display(ostream& out) const
{
out<<coefs << "+"';
for(int i = 0; i <= (degree-1); i++)
out<< coefs[i]<< "x^" << (i) << " + ";
out << coefs[degree] << "x^" << degree;
}
``````

First part: for the pow() function issue. I would suggest you just avoid it altogether by a simple multiplying accumulator:

``````int Poly::evaluate(int x)
{
int total = 0;
int x_pow_term = 1;
for(int i = 0; i <= degree; i++){
total += coefs[i]*x_pow_term; //coefs[] array is the array in which the coef's are stored
x_pow_term *= x; //just multiply the previous term by x, so: x^i = x^(i-1) * x (with x^0 == 1)
};
};``````

Second part: all that I can see that could be wrong is if you didn't initialize your array temp.coefs to zero. Try adding this to see if the output is better:

``````Poly::Poly operator*(const Poly& p)
{
Poly temp;
temp.degree = degree + p.degree; //this resizes coefs right?
if(temp.degree > 99)
cout << "cannot evaluate";
for(int i = 0; i < temp.degree; ++i)
temp.coefs[i] = 0; //set all the sum values to their initial zero.
for(int i = 0; i < degree; i++){
for(int j =0; j < p.degree; j++){
temp.coefs[i+j] += coefs[i]*p.coefs[j];
}
}
return temp;
}``````

Ok in the first instance, using power is plain ugly and inefficient. The typical route for a polynomial in the positive sense, is to do something like this:

``````int total(0);
int xpow(1);
for(int i=0;i<=degree;i++)
{
total+=coef[i]*xpow;
xpow*=x;
}``````

You can also do it via an internal bracket expansion: [this is effectively `(((coef[N]*x+coef[N-1])*x+coef[N-2])*x+.... )))` ]

``````int total(0);
for(int i=degree;i>0;i--)
{
total+=coef[i];
total*=x;
}
total+=coef;``````

I like the second one slightly more.... but not alot in it. [Note the fact you are counting down in the loop, AND you cannot save a step by initializing the total to coef before the loop. -- I only emphasize the last point as ran a 2000 CPU hour simulation with that bug in it :(. ]

Your display function is wrong because you write coef twice, run the loop `for(int i=1;i<degree;i++)` . Note that it is normal to write polynomials high power to constant.

Your multiply: If you want to limit the degree to less than 100, then as well as a warning exit as well. Also have your resized coefs??? in temp do you zero coefs in temp ???

Note: Mike_2000_17 beat me to the post [must learn to type faster], sorry for the repeat, the second version of the evaluation code might be of merit, however.

p.s. Thanks for posting a good first post question to the C++ forum, e.g. code blocks, and sufficient information about your problem and what you have tried.