- **C**
(*http://www.go4expert.com/forums/c/*)

- - **Pascal triangle**
(*http://www.go4expert.com/forums/pascal-triangle-t16804/*)

Pascal triangleI 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? |

Re: Pascal triangleBuild 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. |

Re: Pascal triangleLooking 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. |

Re: Pascal triangleQuote:
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... |

Re: Pascal triangleOkay, I found a code for doing exactly this at:
http://www.c.happycodings.com/Beginn...ts/code45.html |

Re: Pascal triangleThat'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[i]=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. |

Re: Pascal triangleQuote:
(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. |

Re: Pascal triangleOf 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. |

Re: Pascal triangle> 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. |

All times are GMT +5.5. The time now is 03:33. |