How to resolve the algorithm Sierpinski triangle step by step in the PL/I programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sierpinski triangle step by step in the PL/I programming language
Table of Contents
Problem Statement
Produce an ASCII representation of a Sierpinski triangle of order N.
The Sierpinski triangle of order 4 should look like this:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sierpinski triangle step by step in the PL/I programming language
Source code in the pl/i programming language
sierpinski: procedure options (main); /* 2010-03-30 */
declare t (79,79) char (1);
declare (i, j, k) fixed binary;
declare (y, xs, ys, xll, xrr, ixrr, limit) fixed binary;
t = ' ';
xs = 40; ys = 1;
/* Make initial triangle */
call make_triangle (xs, ys);
y = ys + 4;
xll = xs-4; xrr = xs+4;
do k = 1 to 3;
limit = 0;
do forever;
ixrr = xrr;
do i = xll to xll+limit by 8;
if t(y-1, i) = ' ' then
do;
call make_triangle (i, y);
if t(y+3,i-5) = '*' then
t(y+3,i-4), t(y+3,ixrr+4) = '*';
call make_triangle (ixrr, y);
end;
ixrr = ixrr - 8;
end;
xll = xll - 4; xrr = xrr + 4;
y = y + 4;
limit = limit + 8;
if xll+limit > xs-1 then leave;
end;
t(y-1,xs) = '*';
end;
/* Finished generation; now print the Sierpinski triangle. */
put edit (t) (skip, (hbound(t,2)) a);
make_triangle: procedure (x, y);
declare (x, y) fixed binary;
declare i fixed binary;
do i = 0 to 3;
t(y+i, x-i), t(y+i, x+i) = '*';
end;
do i = x-2 to x+2; /* The base of the triangle. */
t(y+3, i) = '*';
end;
end make_triangle;
end sierpinski;
You may also check:How to resolve the algorithm Mouse position step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Combinations step by step in the Pure programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm SQL-based authentication step by step in the Raku programming language