Pascal's Triangle
Objective: This
program provides practice with two-dimensional array manipulation.
|
||||
|
Pascal's triangle is an arithmetic triangle that can be used in both Algebra and Probability/Combinatorics. It is commonly used in algebra to give the coefficients of (x+1)n. Here is a version of Pascal's Triangle. Note that each entry is the sum of the two entries to the above left and above right.Entries in Pascal's Triangle are usually referred to by a row number and a place in that row, beginning with row 0 and place 0. For instance, the number 20 appears in row 6, place 3. Algebra Assume that you are given the polynomial x+1, and you want to raise it to some powers, like 1,2,3,4,5,.... If you make a chart of these power-raisings, you'll get something like this:
If you just look at the coefficients of the polynomials that you get, you'll see Pascal's Triangle! Because of this connection, the entries in Pascal's Triangle are called the binomial coefficients. The formula for figuring out the binomial coefficients can be written:
...where n is the row number (recall that the first row is row 0) and k is the position of the coefficient that you want to find (remember that the first coefficient in a row is always place 0). For example, to find coefficient 2 (k = 2) in row 5 (n = 5) the formula is Thus, the coefficient in question is 10. Probability/Combinatorics Another area in which Pascal's Triangle can be used is in Probability, where it can be used to find Combinations. For example, you have five puppies in a box, and you want to know how many different ways you can pick two of them. It doesn't matter to you how the puppies are positioned in the box, it just matters which two puppies you pick. This problem amounts to the question "how many different ways can you pick two objects from a set of five objects?" The answer is coefficient 2 in the fifth row, i.e. 10. (Remember that the first number in the row, 1, is always place 0.)
. . . Technique: How to construct Pascal's Triangle
When constructing the triangle, it might help to envision the entire triangle inside a two-dimensional array:
The above representation is a figurate number triangle, attributed to Jakob Bernoulli. This triangle is simply Pascal’s triangle in a square grid with zeroes entered into the empty cells. |
||||
|
Exercise
This program requires you to construct a Pascal's Triangle of a user-specified size (not to exceed 13). Use a two-dimensional array to store the elements of the triangle. With the exception of the first row, calculate the value of each triangle element by summing the values of the above-left element and the above-right element. Examples of the user interface appear below. Use a text box for user input, two buttons (Go and Quit), and a picture box to display the triangle. Be sure to clear the picture box between runs. You can restrict your triangle to a maximum of 9 rows (to avoid coefficients larger than two digits), but if you do so then you should not allow the user to enter values greater than 9. Note that the demo allows up to 13 rows and can handle three digit coefficients. You are not required to resize the picture box for different size triangles as is done in the demo. Note that one of the incidental tasks is also one of the most difficult -- positioning the output results in triangle form so that corresponding triangle elements line up correctly.
See the demo program for more examples. Advanced
Assignment:
Provide the option of calculating the values in the triangle either by adding
the values of the elements to the above left and above right (positional) or by
using the formula explained above (formula). Recall that the formula for
an element is Use option buttons to select Positional or Formula.
This is not a trivial exercise, as your solution must work with BOTH approaches, and must provide functions for the formula above as well as for calculating factorials. Use of the formula is not as straightforward as it may seem.... Information for this project was obtained from the Dr.Math web site. To see some interesting patterns that can be found in Pascal's Triangle, visit the Math Forum. |