Question:

Cplusplus program for Pascal's triangle

by  |  earlier

0 LIKES UnLike

I need to write a program for class that displays Pascal's triangle; however, I do not recall binomial coefficients from math class very well.

Can you please explain how to derive a mathematical formula for determining the numbers to display in each row of Pascal's triangle?

 Tags:

   Report

1 ANSWERS


  1. You maybe thinking about this too, trying to work this into a complete  binomial coefficient math problem. But if you really think about this logically it is very simple.

    First you will need to tell your program that it needs to spawn 1 column for every row it makes.

    So, 1st row has 1 column, 2nd row 2, 3rd row 3, etc all the way down, for as long as you want to carry this out. (or required).

    All first and last positions of every row will be = to what ever variable selected. So if user wants the Pascals triangle for 3, it would look like this

              3

             33

            3X3

           3XX3

    Now to fill in you start at the top and work your way down.

    The application will need to add the colums above each empty column below, and that creates the value.

    Example

              3

             33          

            363       3 and 3 sit directly above, 3+3 = 6

           3993     Next values would 9 and 9    

        31218123

    Each row cell is equal to 2 cells added in the previous row, with the exception of the 1st and last cell in each row. So as you move along, your formula for programming will need to see if it is the first or the last cell in the row, if it is, the value would be equal to the original value.

    If it is not the first or last cell, then as you move left to right, each cell would be the sum of the previous row  currentcell location - 1 + the cell to the right of it.

    So if you are working on the 4th row of this example

           3 [9] [9] 3  

        3[12][18][12]3

    the first 12 = previous row, (2nd cell - current position, -1 = 1st cell + the cell to the right = 2nd cell)

    the 18 = previous row, (3rd cell - current position - 1 = 2nd cell + the cell to the right = the 3rd cell.

    The 12 = the previous row, (4th cell - current positon - 1 = 3rd cell + the cell to the right = the 4th cell)

    To sum it up.

    Value1 = PreviousRow, Cell.CurrentColumn - 1

    Value2 = Value1.Row, Value1.Column + 2

    Cell = Value1 + Value2  

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions