How to resolve the algorithm Pascal's triangle step by step in the XPL0 programming language
How to resolve the algorithm Pascal's triangle step by step in the XPL0 programming language
Table of Contents
Problem Statement
Pascal's triangle is an arithmetic and geometric figure often associated with the name of Blaise Pascal, but also studied centuries earlier in India, Persia, China and elsewhere. Its first few rows look like this: where each element of each row is either 1 or the sum of the two elements right above it. For example, the next row of the triangle would be: So the triangle now looks like this: Each row n (starting with row 0 at the top) shows the coefficients of the binomial expansion of (x + y)n.
Write a function that prints out the first n rows of the triangle (with f(1) yielding the row consisting of only the element 1). This can be done either by summing elements from the previous rows or using a binary coefficient or combination function. Behavior for n ≤ 0 does not need to be uniform, but should be noted.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pascal's triangle step by step in the XPL0 programming language
Source code in the xpl0 programming language
include c:\cxpl\codes;
proc Pascal(N); \Display the first N rows of Pascal's triangle
int N; \if N<=0 then nothing is displayed
int Row, I, Old(40), New(40);
[for Row:= 0 to N-1 do
[New(0):= 1;
for I:= 1 to Row do New(I):= Old(I-1) + Old(I);
for I:= 1 to (N-Row-1)*2 do ChOut(0, ^ );
for I:= 0 to Row do
[if New(I)<100 then ChOut(0, ^ );
IntOut(0, New(I));
if New(I)<10 then ChOut(0, ^ );
ChOut(0, ^ );
];
New(Row+1):= 0;
I:= Old; Old:= New; New:= I;
CrLf(0);
];
];
Pascal(13)
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Dart programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the Rascal programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Sather programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the OCaml programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the ooRexx programming language