I've been unsuccessfully trying to write a program to generate pascal triangle numbers and insert them into an 2D flexible array, but i still not getting it; any sugestions?
I've been unsuccessfully trying to write a program to generate pascal triangle numbers and insert them into an 2D flexible array, but i still not getting it; any sugestions?
Last edited by bljonk; 03-21-2002 at 03:28 PM.
The formula for a Pascal Triangle element is:
Whereas n is the row and r is the element(left to right). ! is a factoral sign, which means to multiply that number times every number below it to 1. Like: 6! = 6*5*4*3*2*1 = 720. Puting this in code shouldn't be too hard. Putting it in an array sounds easy, because you could just take a two dimention array and have the row and element as the array subscript numbers. If you still need some coding help just reply back.Code:n! -------- r!(n-r)!
Another way to generate pascal's numbers is to look at
1
1 2 1
1 3 3 1
1 4 6 4 1
Look at the 4 and the 6. It is clear that
4 = 1 + 3
6 = 3+3
Every number in pascal's triangle except for the boundary 1's are such that
pascal(row, col) = pascal(row-1, col-1) + pascal(row-1, col). You can implement a straitforward recursive soultion or by using the array not need recursion.
Just observe that 0! = 1, and not 0
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>2D flexible array
A 2D flexible array?
He probably means dynamic, it should be larger the higher number he inputs.Originally posted by Shiro
>2D flexible array
A 2D flexible array?
PS: How do you represent pascals triangle in a "square" 2D array? hmmmmmmmm...