32. Pascal's triangle

Pascal's triangle is a construction representing binomial coefficients. The triangle starts with a row that has a single value of 1. Elements of each row are constructed by summing the numbers above, to the left and right, and treating blank entries as 0. Here is an example of the triangle with five rows:

        1      1   1    1   2   1  1   3   3   11   4   6   4   1

To print the triangle, we must:

  • Shift the output position to the right with an appropriate number of spaces, so that the top is projected on the middle of the triangle's base.
  • Compute each value by summing the above left and right values. A simpler formula is that for a row i and column j, each new value x is equal to the previous value of x multiplied by (i - j) / (j + 1), where x starts ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.