Anonymous user
Floyd's triangle: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace.
m (→{{header|REXX}}: added/changed comments and whitespace.) |
|||
Line 2,646:
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/
parse arg rows .; if rows=='' then rows=5 /*Not specified? Then use the default.*/
mx=rows * (rows+1) % 2 -
say 'displaying a ' rows " row Floyd's triangle:" /*show header for the triangle.*/
say
#=1; do r=1 for rows; i=0;
do #=# for r; i=i+1
_=_ right(#, length( mx
end /*#*/
say substr(_, 2)
end /*r*/ /*stick
'''output''' when using the default input:
<pre>
Line 2,694 ⟶ 2,693:
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of rows in base 16.*/
parse arg rows .; if rows=='' then rows=6 /*Not specified? Then use the default.*/
mx=rows * (rows+1) % 2 -
say 'displaying a ' rows " row Floyd's triangle in base 16:";
#=1
do r=1 for rows; i=0;
do #=# for r; i=i+1
_=_ right( d2x(#), length( d2x(mx
end /*#*/
say substr(_, 2)
end /*r*/ /*stick
'''output''' when using the default input:
<pre>
Line 2,750 ⟶ 2,748:
if rows=='' | rows=="," then rows= 5 /*Not specified? Then use the default.*/
if radx=='' | radx=="," then radx=10 /* " " " " " " */
mx=rows * (rows+1) % 2 -
say 'displaying a ' rows " row Floyd's triangle in base" radx':'; say /*display hdr*/
#=1
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
_=_ right(base(#, radx), length( base(mx
end /*#*/
say substr(_, 2)
end /*r*/ /* [↑] introduced by first abutment. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure; parse arg x 1 ox,toB,inB /*obtain number, toBase, inBase. */
@abc= 'abcdefghijklmnopqrstuvwxyz'
@abcU=@abc; upper @abcU
@@@= '0123456789'@abc || @abcU
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
/*handles up to base 90, all chars must be viewable.*/
numeric digits
mxB=length(@@@) /*max base (radix) supported here*/
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/
Line 2,774 ⟶ 2,772:
if toB<2 | tob>mxB then call erb 'toBase',toB /* " " " toBase. */
if x=='' then call erm /* " " " number. */
sigX=left(x, 1)
if pos(sigX, '-+')\==0 then x=substr(x, 2)
else sigX=
#=0; do j=1 for length(x); _=substr(x, j,
v=pos(_, @@@) /*get the value of this "digit". */
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */
#=# * inB + v -
end /*j*/
y=
do while # >= toB /*convert #, base 10 ──► base toB*/
y=substr(@@@, (# // toB) + 1, 1)y
#=# %
end /*while*/
Line 2,792 ⟶ 2,789:
return y /*return the number in base toB.*/
/*───────────────────────────────────────────────────────────────────────────────────────*/
erb: call ser 'illegal' arg(2) "base: " arg(1) "must be in range: 2──► " mxB
erd: call ser 'illegal "digit" in' x":" _
erm: call ser 'no argument specified.'
ser: say; say '***
'''output''' when using the input of: <tt> 6 2 </tt>
<pre>
|