Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pascal's Triangle

Status
Not open for further replies.

hetbrouwertje

Programmer
Oct 31, 2003
2
NL
Hello,

some time ago I started to learn Turbo Pascal.
Now I've got a complex question, for me that is.

I want to write a program that prints out Pascal's Triangle.
But I'm stuck since I started to figure out how to write this program.

The program has to ask the user how many rows it has to display, after that the program should give Pascal's Triangle up to the number of rows the user has determined.

I hope someone can help me with this problem.
I'm too stuck ;)

Thanks for helping me!


Pepijn de Brouwer
 
ok, and where are you stuck? Let's see what you have so far, and then we can fill in :)



Cheers,

Realm174
 
As I told you, I got stuck from the start.
This is what I've got so far...


PROGRAM Pascalstriangle;
VAR counter,ammount:INTEGER;

PROCEDURE GiveRow(i:INTEGER);
BEGIN
WRITELN('Row',i);
END;

BEGIN
WRITE('Give number of rows: ');
READLN(ammount);
FOR counter:=1 TO ammount DO
GiveRow(counter);
WRITE('Press ENTER to stop');
READLN;
END.


I hope you can help me out, because I don't know what to do next.

Thanks in advance!


Pepijn de Brouwer
 
Alright, not too much to start... :) I'm assuming you know how to determine the values of Pascal's triangle? Look at each row as an array of numbers. If all you want to do is print the result, then all you need to retain in memory is the current line and the previous line.

I'm sorry, I'm heading out for work, so I can't write the whole thing for you... I'll stop by tonight and see if anyone helped you, and if not, I'll be glad to assist. In the meantime, see if you can determine how to calculate the current row, based on the previous rows...

Things you know:

1st row has only one value of 1.

next rows always have one more value than the previous row.
(think of giving your program a limit in the number of row... you wouldn't want someone to enter 1 billion as a requested number of rows)

and instead of picturing your triangle as a triangle, look at it as a block... it will make it easier to figure out the calculations...

Code:
 ___
|_1_|___
|_1_|_1_|___
|_1_|_2_|_1_|___
|_1_|_3_|_3_|_1_|___
|_1_|_4_|_6_|_4_|_1_|

Shouldn't be too difficult :)



Cheers,

Realm174
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top