Zig-zag matrix: Difference between revisions

m
→‎{{header|REXX}}: added a matrix that starts with unity, another with negative increment, added/changed comments and whitespace, added a subroutine, used templates for the output sections.
(→‎{{header|Octave}}: A solution inspired by Rascal)
m (→‎{{header|REXX}}: added a matrix that starts with unity, another with negative increment, added/changed comments and whitespace, added a subroutine, used templates for the output sections.)
Line 5,029:
<lang rexx>/*REXX program produces and displays a zig─zag matrix (a square array). */
parse arg n start inc . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n=5 5 /*Not specified? Then use the default.*/
if start=='' | start=="," then start=0 0 /* " " " " " " */
if inc=='' | inc=="," then inc=1 1 /* " " " " " " */
row= 1; col=1 col= 1; size= n**2 /*start: with1st therow & 1stcolumn; row, array 1st columnsize.*/
do j=start by inc for size; @.row.col= j
size=n**2 /*the size of array. */
if (row+col)//2==0 then do; j=start if bycol<n inc for sizethen col= col+1; @. else row.col=j row+2
if (row+col)//2==0 then do; if col<n then col if row\==col+1; elsethen row= row+2-1
if row\==1 then row=row-1end
else do; if row<n endthen row= row+1; else col= col+2
else do; if row<n if then rowcol\==row+1; elsethen col= col+2-1
if col\==1 then col=col-1end
end /*j*/ end /* [↑] // is REXX ÷ remainder.*/
call show end /*j*/ /*display [↑] // is REXX ÷a (square) remaindermatrix──►terminal.*/
size=n**2exit /*the size of array. /*stick a fork in it, we're all done. */
 
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: w= max(length(start), length(start + size*inc) ) /*maximummax width of any matrix element. elements,*/
 
do do r=1 for n ; _= right(@.r.1, w) /*show all the rows of the matrix. */
do c=2 for n-1; _= _ right(@.r.c, w) /*build a line for the output forof a row.*/
end /*c*/; say _ /* [↑] align the matrix elements are aligned. */
say _ end /*showr*/; the matrix row just constructed.* return</lang>
'''{{out|output''' |text=&nbsp; when using the default input ofinputs: &nbsp; <tt> 5 </tt>}}
end /*r*/ /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 5 </tt>
<pre>
0 1 5 6 14
Line 5,057 ⟶ 5,056:
9 11 17 20 22
10 18 19 23 24
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 5 &nbsp; 1 </tt}}
<pre>
1 2 6 7 15
3 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 5 &nbsp; -1000 &nbsp; -1 </tt}}
<pre>
-1000 -1001 -1005 -1006 -1014
-1002 -1004 -1007 -1013 -1015
-1003 -1008 -1012 -1016 -1021
-1009 -1011 -1017 -1020 -1022
-1010 -1018 -1019 -1023 -1024
</pre>