1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Pascal triangle

Discussion in 'C' started by ShaiAdar, Apr 5, 2009.

  1. ShaiAdar

    ShaiAdar New Member

    I need to create a console program in C that prints out a Pascal triangle in a size given by the user. I can only use one array for this.

    Any tips?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Build the program up bit by bit. Start by generating line p of a q line triangle (e.g. line 5 of a 10 line one).
    Use a few fixed constants to get that part debugged.
    Then loop from 1 to q again using q as a fixed constant and get that part debugged.
    Then it should just be a case of asking the user what q is.

    I don't know what you would need an array for though. Each line of a triangle can be computed, so just compute each number and display it. No storage is needed except p and q and a couple of loop variables. But if you want to use an array you could compute each line, store the numbers in the array as you go, then display the array to the screen, but that seems unnecessary.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Looking at the Wiki article on Pascal's triangle it could be a lot easier to calculate each line from the previous line as per the standard construction.
    So get the input from the user and that will tell you (indirectly) the size of the array needed.
    This size must be odd, of course.
    Place a 1 in the middle and set all other elements to 0.
    Then display that line and that will be the top line.
    Then calculate the next row just by looking at the array entries and updating them as you go along. You don't need a second array for this as the numbers don't overlap.
     
  4. ShaiAdar

    ShaiAdar New Member

    Should I make a rectangle and fill it with zeros, which won't be displayed, and place the appropriate numbers to make a triangle out of it?

    From what I checked, for a triangle of size n, you need n rows and 2n-1 colums. for ex. a triangle of 3 rows would need 5 colums, like so:
    00100
    01010
    10201

    and when the zeros are elimintated you will have the triangle.
    But to tell you the truth, I have no idea how to implement this...
     
  5. ShaiAdar

    ShaiAdar New Member

  6. xpi0t0s

    xpi0t0s Mentor

    That's how I would have done it, yes. Except that I would only use a single row. So when calculating the 2 in the above, I would calculate that with arr=arr[i-1]+arr[i+1].
    Then as I would be looping over the string from left to right I would be able to zero the values that are no longer any use (after calculating the 2 for example, there is no further need for the first 1, i.e. on the row 01010 the one where the asterisk is in this: 0*010).

    > But to tell you the truth, I have no idea how to implement this...

    OK. Well, suppose I would ask for a 5 line triangle. How long would the array need to be? You already know for a 3 line triangle you need an array 5 long.

    Think of a few example row counts, then work out the array length for each, and you should be able to determine from that a general expression that will give you the array size for any row count.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Yes, but there are two problems with this:

    (1) you learn absolutely NOTHING about programming just by copying that. Far better to work it out yourself.

    (2) if this is a course assignment, your teacher may well spot this, and plagiarism could score you zero even if the code works perfectly. Even if they give you full marks, you still learn nothing.

    Programming is learnt by doing, unfortunately, which is the whole point of you being given exercises to do rather than example programs to look at.
     
  8. ShaiAdar

    ShaiAdar New Member

    Of course,
    I was not planning on copying it word by word. I just wanted to use it to understand how to program it myself.

    Thanks for your help.
     
  9. xpi0t0s

    xpi0t0s Mentor

    > I just wanted to use it to understand how to program it myself.

    If you *really* want to understand how to program something yourself then you have to work through it yourself and solve the problems yourself. Downloading examples and looking through them doesn't do that for you.
     

Share This Page