title: multiplication of polynomials using structs and pointers
body: <p>I've been trying for a while to implement the long multiplication for polynomials and I still haven't figured out how to do it. I keep getting garbage numbers.</p>
  
  <p>here's my code: </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct Poly
  {
      int degree;
      int *coeffs; // array of coefficients from lowest degree to highest degree
  };
  
  Poly* readPoly();
  void outputPoly(const Poly* p, char x);
  Poly* multPoly(const Poly* a, const Poly* b);
  
  int main()
  {
  
      cout &lt;&lt; "First polynomial" &lt;&lt; endl;
      Poly* a = readPoly();
      cout &lt;&lt; "Second polynomial" &lt;&lt; endl;
      Poly* b = readPoly();
  
  
      cout &lt;&lt; "a = ";
      outputPoly(a, 'x');
      cout &lt;&lt; "b = ";
      outputPoly(b, 'x');
  
      Poly* prodOfPoly = multPoly(a,b);
      cout &lt;&lt; "d = ";
      outputPoly(prodOfPoly, 'x');
  
      return 0;
  }
  
  Poly* readPoly()
  {
      int deg;
      int* A;
      cout &lt;&lt; "Input the degree: ";
      cin &gt;&gt; deg;
      A = new int[deg+1]; //add 1 for constants
  
      cout &lt;&lt; "Input coefficients: ";
  
      //assign coefficients
      for (int i = 0; i &lt;= deg; i++){
          cin &gt;&gt; A[i];
      }
  
      cout &lt;&lt; endl;
  
      //create the polynomial structure and assign values to the fields
      Poly* p;
      p = new Poly;
      p-&gt;degree = deg;
      p-&gt;coeffs = A;
  
      return p;
  }
  void outputPoly(const Poly* p, char x)
  {
      for (int i = 0; i &lt;= p-&gt;degree; i++){
          //coefficient is non-zero
          if ( p-&gt;coeffs[i] != 0){
              //exponent is 0
              if (i == 0) 
                  cout &lt;&lt; *(p-&gt;coeffs);
              //exponent is 1 and coefficient is 1
              else if ( i == 1 &amp;&amp; p-&gt;coeffs[i] == 1) 
                  cout &lt;&lt; " + " &lt;&lt;  x;
              //exponent is 1 and coefficient is NOT 1
              else if ( i == 1 &amp;&amp; p-&gt;coeffs[i] != 1) 
                  cout &lt;&lt; " + " &lt;&lt; p-&gt;coeffs[i] &lt;&lt; "*" &lt;&lt; x;
              //exponent is &gt; 1 and coefficient is 1
              else if ( p-&gt;coeffs[i] == 1) 
                  cout &lt;&lt; " + " &lt;&lt; x &lt;&lt; "^" &lt;&lt; i;
              //coefficient is positive
              else if ( p-&gt;coeffs[i] &gt; 0) 
                  cout  &lt;&lt; " + " &lt;&lt; p-&gt;coeffs[i] &lt;&lt; "*" &lt;&lt; x &lt;&lt; "^" &lt;&lt; i;
              //coefficient is negative
              else if ( p-&gt;coeffs[i] &lt; 0)
                  cout  &lt;&lt; " - " &lt;&lt; -p-&gt;coeffs[i] &lt;&lt; "*" &lt;&lt;  x &lt;&lt; "^" &lt;&lt; i;
          }
          //polynomial is a constant
          else if( p-&gt;coeffs[i] == 0 &amp;&amp; p-&gt;degree == 0)
              cout &lt;&lt; p-&gt;coeffs[i];
      }
      cout &lt;&lt; endl;
  }
  Poly* multPoly(const Poly* a, const Poly* b)
  {
      Poly* finalProduct;
      finalProduct = new Poly;
      finalProduct-&gt;degree = a-&gt;degree + b-&gt;degree; 
  
      int* productCoeffs = new int[finalProduct-&gt;degree - 1];
  
      //solve for product of polynomials
      for (int i = 0; i != finalProduct-&gt;degree; i++){
          for (int j = 0; j != finalProduct-&gt;degree; j++)
              productCoeffs[i + j] += a-&gt;coeffs[i] * b-&gt;coeffs[j];
      }
  
      finalProduct-&gt;coeffs = productCoeffs;
  
      return finalProduct;
  }
  </code></pre>
  
  <p>I know that I get the garbage numbers because at the line: </p>
  
  <pre><code>for (int i = 0; i != finalProduct-&gt;degree; i++){
              for (int j = 0; j != finalProduct-&gt;degree; j++)
                  productCoeffs[i + j] += a-&gt;coeffs[i] * b-&gt;coeffs[j];
  </code></pre>
  
  <p>If the input I have is </p>
  
  <pre><code>a: degree 3, coeffs, 0 1 2 3
  b: degree 2, coeffs, 1 2 -3
  </code></pre>
  
  <p>degree of the product would then be 7-1 (a: 3+1 &amp; b: 2+1) = 6
  then <code>a-&gt;coeffs[i]</code> will have garbage numbers because <code>a-&gt;coeffs</code> will only have 3+1  maximum monomials.</p>
  
  <p>The output should be: <code>d = 4x^2 + 4x^3 - 9x^5,</code>
  but I get: <code>d = 9398672 + x + 9371988*x^2 + 4*x^3 - 1112350759*x^4 - 2063433372*x^5</code>
  If anyone could help and lead me to fixing the code that would be greatly appreciated. Thanks!</p>