Floyd's triangle: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added/changed comments and whitespace. |
|||
Line 2,646: | Line 2,646: | ||
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/ |
<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.*/ |
parse arg rows .; if rows=='' then rows=5 /*Not specified? Then use the default.*/ |
||
mx=rows * (rows+1) % 2 |
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/ |
||
say 'displaying a ' rows " row Floyd's triangle:" /*show header for the triangle.*/ |
say 'displaying a ' rows " row Floyd's triangle:" /*show header for the triangle.*/ |
||
say |
say |
||
#=1; do r=1 for rows; i=0; |
#=1; do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/ |
||
do #=# for r; i=i+1 |
do #=# for r; i=i+1 /*start to construct a row of triangle.*/ |
||
_=_ right(#, length(mx |
_=_ right(#, length( mx+i) ) /*build a row of the Floyd's triangle. */ |
||
end /*#*/ |
end /*#*/ |
||
say substr(_,2) |
say substr(_, 2) /*remove 1st leading blank in the line.*/ |
||
end /*r*/ /* |
end /*r*/ /*stick a fork in it, we're all done. */</lang> |
||
/*stick a fork in it, we're all done. */</lang> |
|||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,694: | Line 2,693: | ||
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of rows in base 16.*/ |
<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.*/ |
parse arg rows .; if rows=='' then rows=6 /*Not specified? Then use the default.*/ |
||
mx=rows * (rows+1) % 2 |
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/ |
||
say 'displaying a ' rows " row Floyd's triangle in base 16:"; |
say 'displaying a ' rows " row Floyd's triangle in base 16:"; say /*show triangle hdr*/ |
||
#=1 |
#=1 |
||
do r=1 for rows; i=0; |
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/ |
||
do #=# for r; i=i+1 |
do #=# for r; i=i+1 /*start to construct a row of triangle.*/ |
||
_=_ right(d2x(#),length(d2x(mx |
_=_ right( d2x(#), length( d2x(mx+i) ) ) /*build a row of the Floyd's triangle. */ |
||
end /*#*/ |
end /*#*/ |
||
say substr(_,2) |
say substr(_, 2) /*remove 1st leading blank in the line.*/ |
||
end /*r*/ /* |
end /*r*/ /*stick a fork in it, we're all done. */</lang> |
||
/*stick a fork in it, we're all done. */</lang> |
|||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,750: | Line 2,748: | ||
if rows=='' | rows=="," then rows= 5 /*Not specified? Then use the default.*/ |
if rows=='' | rows=="," then rows= 5 /*Not specified? Then use the default.*/ |
||
if radx=='' | radx=="," then radx=10 /* " " " " " " */ |
if radx=='' | radx=="," then radx=10 /* " " " " " " */ |
||
mx=rows * (rows+1) % 2 |
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/ |
||
say 'displaying a ' rows " row Floyd's triangle in base" radx':'; say /*display hdr*/ |
say 'displaying a ' rows " row Floyd's triangle in base" radx':'; say /*display hdr*/ |
||
#=1 |
#=1 |
||
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/ |
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.*/ |
do #=# for r; i=i+1 /*start to construct a row of triangle.*/ |
||
_=_ right(base(#, radx), length(base(mx |
_=_ right(base(#, radx), length( base(mx+i, radx) ) ) /*build triangle row.*/ |
||
end /*#*/ |
end /*#*/ |
||
say substr(_,2) |
say substr(_, 2) /*remove 1st leading blank in the line,*/ |
||
end /*r*/ /* [↑] introduced by first abutment. */ |
end /*r*/ /* [↑] introduced by first abutment. */ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
base: procedure; parse arg x 1 ox,toB,inB /*obtain number, toBase, inBase. */ |
base: procedure; parse arg x 1 ox,toB,inB /*obtain number, toBase, inBase. */ |
||
@abc='abcdefghijklmnopqrstuvwxyz' |
@abc= 'abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */ |
||
@abcU=@abc; upper @abcU |
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */ |
||
@@@='0123456789'@abc || @abcU |
@@@= '0123456789'@abc || @abcU /*prefix 'em with numeric digits.*/ |
||
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/ |
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/ |
||
/*handles up to base 90, all chars must be viewable.*/ |
/*handles up to base 90, all chars must be viewable.*/ |
||
numeric digits |
numeric digits 3000 /*what the hey, support gihugeics*/ |
||
mxB=length(@@@) /*max base (radix) supported here*/ |
mxB=length(@@@) /*max base (radix) supported here*/ |
||
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/ |
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/ |
||
Line 2,774: | Line 2,772: | ||
if toB<2 | tob>mxB then call erb 'toBase',toB /* " " " toBase. */ |
if toB<2 | tob>mxB then call erb 'toBase',toB /* " " " toBase. */ |
||
if x=='' then call erm /* " " " number. */ |
if x=='' then call erm /* " " " number. */ |
||
sigX=left(x, 1) /*obtain a possible leading sign.*/ |
|||
if pos(sigX,'-+')\==0 then x=substr(x,2) |
if pos(sigX, '-+')\==0 then x=substr(x, 2) /*X number has a leading sign? */ |
||
else sigX= |
else sigX= /* ··· no leading sign.*/ |
||
#=0; do j=1 for length(x) |
#=0; do j=1 for length(x); _=substr(x, j, 1) /*convert X, base inB ──► base 10*/ |
||
_=substr(x, j, 1) /*pick off a numeral from X. */ |
|||
v=pos(_, @@@) /*get the value of this "digit". */ |
v=pos(_, @@@) /*get the value of this "digit". */ |
||
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */ |
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */ |
||
#=#*inB+v- |
#=# * inB + v - 1 /*construct new num, dig by dig. */ |
||
end /*j*/ |
end /*j*/ |
||
y= |
y= |
||
do while # >= toB /*convert #, base 10 ──► base toB*/ |
do while # >= toB /*convert #, base 10 ──► base toB*/ |
||
y=substr(@@@, (#//toB)+1, 1)y |
y=substr(@@@, (# // toB) + 1, 1)y /*construct the number for output*/ |
||
#=#% |
#=# % toB /* ··· and whittle # down also.*/ |
||
end /*while*/ |
end /*while*/ |
||
Line 2,792: | Line 2,789: | ||
return y /*return the number in base toB.*/ |
return y /*return the number in base toB.*/ |
||
/*───────────────────────────────────────────────────────────────────────────────────────*/ |
/*───────────────────────────────────────────────────────────────────────────────────────*/ |
||
erb: call ser 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" mxB |
erb: call ser 'illegal' arg(2) "base: " arg(1) "must be in range: 2──► " mxB |
||
erd: call ser 'illegal "digit" in' x":" _ |
erd: call ser 'illegal "digit" in' x":" _ |
||
erm: call ser 'no argument specified.' |
erm: call ser 'no argument specified.' |
||
ser: say; say '*** |
ser: say; say '***error***'; say arg(1); say; exit 13</lang> |
||
'''output''' when using the input of: <tt> 6 2 </tt> |
'''output''' when using the input of: <tt> 6 2 </tt> |
||
<pre> |
<pre> |