0

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
}
return total;
}
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by peterman.k
Featured Replies
  • 1

    [QUOTE=;][/QUOTE] 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: [code=c++] int total(0); int xpow(1); for(int i=0;i<=degree;i++) { total+=coef[i]*xpow; xpow*=x; } [/code] You can also do it via an internal bracket … Read More

0

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

Mult overload:

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[0] << "+"';
for(int i = 0; i <= (degree-1); i++)
  out<< coefs[i]<< "x^" << (i) << " + ";
out << coefs[degree] << "x^" << degree;
}

Edited by mike_2000_17: Fixed formatting

0

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)
  };
  return total;
};

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;
}
1

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[0];

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[0] 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[0] 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.

Edited by StuXYZ: n/a

Comments
Very helpful
0

Thank you both for your quick responses, they both have helped out immensely with the assignment, and it is good for me to gain some other methods other than what I see in class and from my peers. I hate using the pow() function, I figured out how to change the double/int situation, but it was ugly, and I will be going back and replacing it with the code you guys suggested.

StuXYZ, thanks for the feedback on my post, as I am still unsure of all the forum protocol and etiquette, so anything helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.