# what am i doing wrong here?

• 03-21-2010, 03:18 PM
GPB
what am i doing wrong here?
I'm using an arraylist to multiply polynomials, but it doesn't work correctly:

Code:

```    public void times (Poly b) {         for ( int p_a = 0 ; p_a < polynomial.size() ; p_a++ ) {             Term temp = polynomial.get(p_a);             for ( int p_b = 0 ; p_b < b.polynomial.size() ; p_b++ ) {                 polynomial.set(p_a,temp.times(b.polynomial.get(p_b)));             }         }     }```
Code:

```    public Term times (Term term) {         Term temp = new Term(coefficient *= term.coefficient, exponent += term.exponent);         return temp;     }```
ex. (x^2+3x-1)*(7-x^2+2x^3)
The correct answer would be 2x^5+5x^4-5x^3+8x^2+21x-7,
but in stead it gives me -14x^7-42x^6+14x^5

I believe this is because it updates polynomial(p_a) after each loop and uses that new value in the next loop. Which is odd, because I put in a temp Term for exactly that reason.
• 03-21-2010, 03:29 PM
GPB
Oh I think I got it already, I never add the terms, I just change them.
• 03-21-2010, 04:08 PM
GPB
Hmm no, I'm still stuck.
• 03-21-2010, 05:04 PM
GPB
Solved it :D