How to create a pascal's triangle using a single dimension array with the last number specified?
Printable View
How to create a pascal's triangle using a single dimension array with the last number specified?
if you want to display the whole triangle up to that point then just start with 1 and build the rest of the rows from adding the appropriate columns from the previous rows (and 1's on the ends). If your just want a row then use combinations ( r! / ( c! * ( r - c )! ).
If you're storing all the rows of pascals triangle up to the one specified, just store the columns one by one in the array. When refering to the array you know that there is always 1 plus the row number amount of columns in that row so you can just use that knowledge to figure out where each row starts.
It doesn't work out succesfully
It should, unless you're doing it wrong :p
Post your code and we'll help find the mistake, but we won't do it all for you.
What is pascal's triangle? when you do the ! that means factorial right? i.e you start at the number and then recursively add one minus the number and add it to another variable until you get to zero?
5! = 5 + 4 + 3 + 2 + 1 right? I think I did this in 8th grade
Pascals triangle has got nothing do with factorials......
it is something like this:-
1
1 2 1
1 3 3 1
1 4 6 4 1
......
and it goes on and on......it works on the principle that a numer is the sum of digit exactly above it in the previous row and and the no before it.
Actually, it does, you just apparently only learned how pascal's triangle is formed through recursion (which was the other option I gave in my post), not through the concept of combinations. Combinations (the one with factorial which you foolishly disregarded) are what allow you to calculate a row without having to know all the previous rows.Quote:
Originally posted by Priyank
Pascals triangle has got nothing do with factorials......
it is something like this:-
1
1 2 1
1 3 3 1
1 4 6 4 1
......
and it goes on and on......it works on the principle that a numer is the sum of digit exactly above it in the previous row and and the no before it.
EDIT: Shadow: No, factorial is multiplication not addition. You can use rows of pascals triagnle to figure out the coefficients of terms in expanded polynomials of the form (x + 1)^n.
Not just (X + 1)^n but also (X + Y)^nQuote:
Originally posted by Polymorphic OOP
You can use rows of pascals triagnle to figure out the coefficients of terms in expanded polynomials of the form (x + 1)^n.
Code:1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
(X + Y)^5 =
1 * X^5 * Y^0 +
5 * X^4 * Y^1 +
10 * X^3 * Y^2 +
10 * X^2 * Y^3 +
5 * X^1 * Y^4 +
1 * X^0 * Y^5
can anyone submit a proper code please.. thnQ..!
Dont bump old threads