Zig-zag matrix: Difference between revisions

m
No edit summary
 
(4 intermediate revisions by 4 users not shown)
Line 401:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<syntaxhighlight lang="algol68">PROC zig zag = (INT n)[,]INT: (
PROC zig zag = (INT n)[,]INT: (
PROC move = (REF INT i, j)VOID: (
IF j < n THEN
Line 435 ⟶ 436:
[,]INT result = zig zag(dim);
FOR i TO dim DO
print((result[IF i,], new= 1 THEN "((" ELSE " (" lineFI));
FOR j TO dim DO
print(( whole( result[i,j], -3 ), IF j /= dim THEN "," ELSE "" FI ))
OD;
print((IF i = dim THEN "))" ELSE ")," FI, new line))
OD
#FI#
#FI#</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
{|border="1" style="border-collapse: collapse; border: 5px double grey;"
|align=center width=50%| With formatted transput possible, e.g. [[ALGOL 68G]]
|align=center| '''not''' formatted transput possible, e.g. [[ELLA ALGOL 68]]
|-
|align=center|<pre>
(( 0, 1, 5, 6, 14),
( 2, 4, 7, 13, 15),
Line 450 ⟶ 452:
( 10, 18, 19, 23, 24))
</pre>
||<pre>
+0 +1 +5 +6 +14
+2 +4 +7 +13 +15
+3 +8 +12 +16 +21
+9 +11 +17 +20 +22
+10 +18 +19 +23 +24
</pre>
|}
 
=={{header|ALGOL W}}==
Line 5,854 ⟶ 5,848:
=={{header|REXX}}==
This REXX version allows the optional specification of the &nbsp; '''start''' &nbsp; and &nbsp; '''increment''' &nbsp; values.
===Version 1===
<syntaxhighlight 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*/
Line 5,899 ⟶ 5,894:
-1009 -1010 -1014 -1015
</pre>
 
===Version 2 - simplified logic===
<syntaxhighlight lang="rexx">/*REXX program produces and displays a zig-zag matrix (a square array) */
Parse Arg n start inc . /* obtain optional arguments from command line */
if n=='' | n=="," then n= 5 /*Not specified? use the default*/
if start=='' | start=="," then start= 0 /* " " " " " */
if inc=='' | inc=="," then inc= 1 /* " " " " " */
Parse Value 1 1 n**2 With row col size
Do x=start By inc For size
m.row.col=x
If (row+col)//2=0 Then do /* moving upward */
Select
when row=1 Then Do /* at upper bound */
If col<n Then
col=col+1 /* move right */
Else
row=2 /* move down */
End
when col=n Then /* at right border */
row=row+1 /* move down */
Otherwise Do /* in all other cases */
row=row-1 /* move up */
col=col+1 /* and to the right */
End
End
End
Else Do /* moving downward */
Select
When col=1 Then Do /* at lower bound */
If row=n Then /* in bottom row */
col=2 /* move right */
Else /* otherwise */
row=row+1 /* move down */
End
When row=n Then /* at lower bound */
col=col+1 /* move right */
Otherwise Do /* in all other cases */
row=row+1 /* move down */
col=col-1 /* and to the left */
End
End
End
End
Call show
Exit
/*-----------------------------------------------------------------------*/
show:
w=length(start+size*inc) /* max width of any matrix element */
Do row=1 To n /* loop over rows */
line=right(m.row.1,w) /* first element */
Do column=2 To n /* loop over other elements */
line=line right(m.row.column,w) /* build output line */
End
Say line
End /* display the line */
Return</syntaxhighlight>
 
=={{header|Ring}}==
Line 5,974 ⟶ 6,025:
 
[http://kepkezelo.com/images/kk86ng7p4gcl7z3p7vo1.jpg Zig-Zag matrix]
 
=={{header|RPL}}==
{{works with|RPL|HP-48}}
Turtle's way.
« 1 -1 → n way val
« n DUP 2 →LIST 0 CON
2 n DUP + '''FOR''' s
n s 1 - MIN s OVER -
'''IF''' way 0 > '''THEN''' SWAP '''END'''
'''FOR''' j
j s OVER - 2 →LIST 'val' INCR PUT
way '''STEP'''
'way' SNEG
'''NEXT'''
» » '<span style="color:blue">ZIGZAG</span>' STO
 
6 <span style="color:blue">ZIGZAG</span>
{{out}}
<pre>
1: [[0 2 3 9 10 20]
[1 4 8 11 19 21]
[5 7 12 18 22 29]
[6 13 17 23 28 30]
[14 16 24 27 31 34]
[15 25 26 32 33 35]]
</pre>
 
=={{header|Ruby}}==
Line 6,566 ⟶ 6,643:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var zigzag = Fn.new { |n|
1,150

edits