Create a two-dimensional array at runtime: Difference between revisions

→‎{{header|REXX}}: added whitespace and comments, changed indentations and comments, changed default, added boxed comments.
m (→‎{{header|REXX}}: removed STYLE from the PRE html tag.)
(→‎{{header|REXX}}: added whitespace and comments, changed indentations and comments, changed default, added boxed comments.)
Line 1,398:
 
=={{header|REXX}}==
<lang rexx>/*REXX program to allocateallocates/populatepopulates/displaydisplays a two-dimensional array. */
call bloat /*the BLOAT procedure does all allocations.*/
call bloat
/*no more array named A. @ at this point. */
exit /*stick a fork in it, we're all done honey. */
/*─────────────────────────BLOAT subroutine─────────────────────────────*/
bloat: procedure; say /*"PROCEDURE" makes this a ··· procedure. */
say 'Enter two positive integers (a 2-dimensional array will be created).'
pull n m . /*thereelements isare noallocated wayas tothey're pre-allocate an array,defined*/
/*REXXN willand allocateM eachshould elementbe whenverified it'sat this point.*/
@.=' · ' /*assigned. Initial value for all @ array elements,*/
/*defined,this andensures the memoryevery that the arrayelement has a value.*/
 
a.='~' do j =1 for n /*default value fortraipse allthrough elementsthe sofirst far. dimension [N]*/
do k=1 for m /* " /*this ensures" " second every" element has a value. [M]*/
if random()//7==0 then a@.j.k=j'_~'k /*populate every 7th random*/
do j =1 for n
do k=1 for m
if random()//7==0 then a.j.k=j'_'k /*populate every 7th random*/
end /*k*/
end /*j*/
/*PROCEDURE in[↓] this case), thedisplay array to A.console: row,col is*/
 
do r=1 for n; _= /*displayconstruct arrayone torow the(or consoleline) at (row,col)a time. */
do c=1 for m /*construct row one column at a time. */
_=
_=_ right(@.r.c,4) /*append a nice-aligned column to the line.*/
do c=1 for m
end /*kk*/ _=_ right(a.r.c,4) /*display one[↑] row at aan time,nicely alignaligned line is built. the vals*/
say _ /*Thedisplay sameone effectrow isat performeda by atime DROP.to the terminal*/
end /*kk*/
end do c=1 for m/*jj*/
say _
/*╔════════════════════════════════════════════════════════════════════╗
end /*jj*/
When the RETURN /*when the RETURN is executed (from a PROCEDURE in this case), and */
║ array @ is "de─allocated", that is, it's no longer defined, and ║
/*PROCEDURE in this case), the array A. is*/
║ the array's storage is now free for other REXX variables. If the ║
/*"de-allocated", that is, it's no longer */
║ BLOAT subroutine didn't have a "PROCEDURE" on that statement,║
/*defined, and the memory that the array */
the array @ would've been left intact. The same /*haseffect is now free for other REXX variables.*/
║ performed by a DROP statement (an example is shown below). ║
 
╚════════════════════════════════════════════════════════════════════╝*/
/*If the BLOAT subroutine didn't have */
drop @. /*abecause of "PROCEDURE"the onPROCEDURE that statement, the */
return /*array [↑] "a."DROP statement is would be left intactsuperfluous. */</lang>
 
/*The same effect is performed by a DROP. */
drop a. /*because of the PROCEDURE statement, */
return /* the DROP statement is superfluous. */</lang>
'''output''' when the following input is entered after the prompt message: &nbsp; <tt> 30 15 </tt>
<pre>
· ~ 1_2 · ~ · ~ · ~ · ~ · ~ · 1_8 1_9 1_10· 1_11 · ~ · ~ · 1~12 · ~ · ·
2_1 · 2_2 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ 2_13 · 2~13 · ~ ·
~ · 3~2 3_3 · 3_4 · ~ · ~ · ~ · ~ · ~ 3_10 · ~ · 3~11 · ~ · ~ · 3~15
· ~ · ~ · ~ · ~ · 4~6 · ~ 4_7 · ~ · ~ · ~ 4_11· ~· ~· ~· ~·
· 5~2 · ~ · ~ · ~ 5_5 · ~ · ~ · 5~9 · ~ · ~ · ~ · ~ · ~ ~ 5_15·
· ~ · ~ 6_3 · 6~4 · ~ · ~ 6_7 · ~ · ~ 6_10· ~· ~· ~· ~· ~· ·
7_1 · 7~2 7~3 7_4 · ~ · ~ · ~ · ~ · ~ · ~ · 7~11 · ~ ~ · 7~14 ~·
· ~ 8_2 · 8~3 · ~ · ~ · ~ 8_7 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ ·
· ~ 9_2 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ 9_15· ·
· ~ · ~ · ~ · ~ · ~ · ~ ~ · 10~8 · ~ · ~ ~ · 0~12 · ~ · ~ ~·
11_1 · ~ · ~ · ~ 11_5 · ~ · · 11~7 11_8 11_9 · ~ · ~ · ~ · ~ · ~ · ~ · ·
12~1 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ ~ · 2~15
· ~ 13_2 · ~ 13_4 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · · · 3~13 3_13 3_14 · ~ ·
14~1 · ~ · ~ · ~ ~ · 14~6 · ~ · ~ · ~ · ~ 4_11 · ~ · ~ · 4~14 ~·
15~1 15~2 15_2 · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · 5~12 · ~ · ~ ~ ~·
16~1 · ~ 16_2 · ~ 16_4 · ~ · ~ · ~ · ~ · ~ · ~ 6_11· ~· ~· ~· ~· ·
17_1 · ~ 17_3 · ~ · ~ 17_6 · ~ · ~ · ~ 7_10· ~· · 7~10 · 7~12 · 7~14 7_15·
· ~ · ~ · ~ · ~ 18_5 · ~ · ~ · ~ · ~ 8_10 · · 8~11 · ~ · ~ 8_14 · ~ ·
· ~ 19_2 · ~ · ~ · ~ · 19~6 · ~ · ~ · ~ · 9~11 9_11 · ~ · ~ · ~ ~·
20~1 · ~ · ~ · ~ · ~ · ~ 20_7 · 20~8 · ~ ~ · 0~11 · ~ · ~ · ~ ~·
· ~ · ~ · ~ · ~ 21_5 · ~ · ~ · ~ · ~ 1_10 · ~ · ~ · ~ · ~ 1_15· · ·
· ~ 22_2 · ~ · ~ · ~ · 22~6 · ~ · ~ 22_9 · ~ 2_11 2_12· ~· ~· · 2~14 ·
· ~ · ~ · ~ · ~ 23_5 · ~ · ~ · 23~8 · ~ · ~ 3_11 3_12 · ~ 3_14· · ~ · ·
· ~ · ~ · ~ · 24~5 24~6 ~· 24_6 · ~ 24_8 24_9 · ~ · ~ 4_12· · 4~13 · ~ ~·
25_1 · ~ · ~ · ~ · ~ 25_6 25_7 · ~ · ~ 5_10· ~· · 5~10 · ~ · ~ · ~ · ·
· ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ ~·
27~1 · 27~3 · ~ · ~ · ~ · ~ · ~ 27_7 · ~ 27_9 · ~ · ~ · ~ 7_13· ~ · 7~15
· ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ · ~ 8_10 · ~ · ~ · ~ · ~ · 8~15
· ~ 29_2 · ~ · ~ 29_5 · ~ · ~ · ~ · ~ 9_10· ~· ~· ~· ~· ~· · ·
· ~ 30_2 30_3 · ~ 30_5· ~· · 30~6 30_8 30_9 · 30~8 30~9 · ~ · ~ 0_13 · ~ · ~ · ·
</pre>