0

Hello problem i am having is when trying to multiply polynomial 1 by polynomial 2

polynomial 1 gets mutliplied by the first term in polynomial 2 and not by each term in polynomial 2.

Below is my code:

/*********************************************************************************************
   *  Multiplies Polynomial 1 to Polynomial 2
   *  The method does not change the original polynomial.
**********************************************************************************************/


   public Polynomial multiply(Polynomial poly)
   {
      Polynomial res = clone();


for(Monomial tmp2 = poly.head; tmp2 != null; tmp2 = tmp2.next) //-------- LINE B

      for(Monomial tmp = res.head; tmp != null; tmp = tmp.next)
         res.addTerm(tmp.coeff *= poly.head.coeff, tmp.deg += poly.head.deg);


         double num = 0.5;
         for(Monomial tmp = res.head; tmp != null; tmp = tmp.next)
         tmp.coeff *= num;


    // Polynomial MultiplicationResult = res.add(res2); // add result of two multiplcations together
    // res + poly
      return res;
   }

And this is the result i'm getting

Multiply Polynomial 1 by Polynomial 2:
+ 6x^7 + 3x^4 + 5x^2 + 8 *
+ 5x^7 + 9
=
+ 300x^21 + 150x^18 + 250x^16 + 400x^14

(if i remove that LINE B from the code above thats when i would get the result of 5x^7 * 6x^7 + 3x^4 + 5x^2 + 8)

thanks

Edited by Bradoz: tidy code

2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by NormR1
0

How do you get access to each term in a polynomial?
Can you access only the first term in poly2 and use that with poly1.

This topic has been dead for over six months. 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.