pascal's triangle

This is a discussion on pascal's triangle within the C Programming forums, part of the General Programming Boards category; How to create a pascal's triangle using a single dimension array with the last number specified?...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    pascal's triangle

    How to create a pascal's triangle using a single dimension array with the last number specified?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    It doesn't work out succesfully

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It should, unless you're doing it wrong

    Post your code and we'll help find the mistake, but we won't do it all for you.

  5. #5
    Shadow12345
    Guest
    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

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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.
    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.

    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.
    Last edited by Polymorphic OOP; 02-20-2003 at 01:02 AM.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    Not just (X + 1)^n but also (X + Y)^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
    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.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    can anyone submit a proper code please.. thnQ..!

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Dont bump old threads

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Triangle Function
    By w2look in forum C Programming
    Replies: 14
    Last Post: 11-13-2010, 01:31 PM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. Pascal's triangle algorithm
    By Zewu in forum C++ Programming
    Replies: 10
    Last Post: 06-17-2002, 12:59 PM
  5. Replies: 1
    Last Post: 01-30-2002, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21