title: Centering Pascal's Triangle Output in C++
body: <p>I have successfully written a code to output Pascal's Triangle in a somewhat triangular shape using cout.width(total_rows - current_row), but it looks like this:</p>
  
  <pre><code>               1 
                1 1 
               1 2 1 
              1 3 3 1 
             1 4 6 4 1 
            1 5 10 10 5 1 
           1 6 15 20 15 6 1 
          1 7 21 35 35 21 7 1 
         1 8 28 56 70 56 28 8 1 
        1 9 36 84 126 126 84 36 9 1 
       1 10 45 120 210 252 210 120 45 10 1 
      1 11 55 165 330 462 462 330 165 55 11 1 
     1 12 66 220 495 792 924 792 495 220 66 12 1 
    1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 
   1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 
  1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
  </code></pre>
  
  <p>I would like it to be completely centered. I figured out that I can take the number or characters in the bottom row, subtract the number of characters in the current row, and divide this by two to get the desired number of spaces per row [which would look like: cout.width((bottow_row_characters - current_row_characters) / 2) ], but I'm having trouble actually implementing this idea.</p>
  
  <p>I've tried computing just the bottom row and storing in it a string or array and then using string.length() or sizeof(array), but neither has worked. (sizeof always returns 4, which is incorrect)</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  // Forward declaration of a function.
  int Pascal (int row, int column);
  
  
  
  
   /* The main function.
    *
    * Parameters:
    *    none
    *
    * Return value:
    *    0 if we complete successfully, 1 if there was an error.
    */
  
   int main ()
   {
  
    // introduction
  
    cout &lt;&lt; "\nPascal's Triangle!\n";
    cout &lt;&lt; "(Pascal's triangle is made by taking the sum of two numbers\n";
    cout &lt;&lt; "and placing that number directly underneath the two numbers.\n";
    cout &lt;&lt; "This creates a triangular array of binomial coefficients)\n\n";
  
  
    // for loops to calculate and print out pascal's triangle
  
       for (int row = 0; row &lt;= 15; row++)
      {
  
        cout.width(16 - row);
  
        for (int column = 0; column &lt;= row; column++)
          {
            cout &lt;&lt; Pascal(row, column) &lt;&lt; " ";
          }
  
        cout &lt;&lt; endl;
      }
  
      cout &lt;&lt; endl;
  }
  
  
  
  
  /* This function calculates Pascal's triangle based on row and column position.
   *
   * Parameters:
   *    row, column
   *
   * Return value:
   *    the numbers in Pascal's triangle
   */
  
  int Pascal (int row, int column)
  {
  
    // if statements to calculate pascal's triangle through recursion
  
    if (column == 0)
      return 1;
  
    else if (row == column)
      return 1;
  
    else
      return Pascal(row - 1, column - 1) + Pascal(row - 1, column);
  }
  </code></pre>