Zig-zag matrix: Difference between revisions

m
No edit summary
 
(12 intermediate revisions by 10 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 1,708 ⟶ 1,702:
return array
}</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
type TMatrix = array of array of double;
 
 
 
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat[0]) do
begin
S:=S+'[';
for X:=0 to High(Mat) do
S:=S+Format('%4.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
procedure ZigzagMatrix(Memo: TMemo);
var Mat: TMatrix;
var X,Y,Inx,Dir: integer;
const Size = 10;
 
procedure Toggle(var I: integer);
{Toggle Direction and increment I}
begin
Dir:=-Dir;
Inc(I);
end;
 
 
procedure Step(var X,Y: integer);
{Take one step "Dir" direction}
begin
X:=X+Dir;
Y:=Y-Dir;
end;
 
begin
SetLength(Mat,Size,Size);
Inx:=0; X:=0; Y:=0; Dir:=1;
repeat
begin
Mat[X,Y]:=Inx;
if (X+Dir)>=Size then Toggle(Y)
else if (Y-Dir)>=Size then Toggle(X)
else if (X+Dir)<0 then Toggle(Y)
else if (Y-Dir)<0 then Toggle(X)
else Step(X,Y);
Inc(Inx);
end
until Inx>=Size*Size;
DisplayMatrix(Memo,Mat);
end;
</syntaxhighlight>
{{out}}
<pre>
[ 0 1 5 6 14 15 27 28 44 45]
[ 2 4 7 13 16 26 29 43 46 63]
[ 3 8 12 17 25 30 42 47 62 64]
[ 9 11 18 24 31 41 48 61 65 78]
[ 10 19 23 32 40 49 60 66 77 79]
[ 20 22 33 39 50 59 67 76 80 89]
[ 21 34 38 51 58 68 75 81 88 90]
[ 35 37 52 57 69 74 82 87 91 96]
[ 36 53 56 70 73 83 86 92 95 97]
[ 54 55 71 72 84 85 93 94 98 99]
 
 
Elapsed Time: 1.576 ms.
 
</pre>
 
 
=={{header|Elena}}==
Line 1,804 ⟶ 1,882:
end
fun dump = void by List matrix
int max = length([text] !(matrix.length ** 2)) + 1
for each List row in matrix
for each int value in row
write(" " * (max - length([text] !value)) + value)
end
writeLine()
Line 2,666 ⟶ 2,744:
10 18 19 23 24</syntaxhighlight>
 
(Also, of course, <code>($ [: /:@; <@|.`</.@i.)@,~0</code> creates a result with 0 rows and 0 columns. And, with an argument of 1, the result has one row and one column with the value <code>0</code>. And, the other expressions behave the same.)
By the way, all the edge cases are handled transparently,
 
without any special checks.
Furthermore, by simply ''removing'' the trailing <tt>@,~</tt>
from the solutions, they automatically generalize
Line 3,354 ⟶ 3,432:
 
End</pre>
 
=={{header|K}}==
 
'''Works with:''' [[ngnk|ngn/k]]
 
<syntaxhighlight>
f:{grid:+x#<<a,'(!#a)*- 2!a:+/!x,:x
padded:(-#$-1+*/x)$$grid
`0:" "/'padded}
f 5
</syntaxhighlight>
 
{{out}}
<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|Kotlin}}==
Line 3,623 ⟶ 3,721:
 
print(zigzag.new(5))
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
Module Lib1 {
Module Global PrintArray(&Ar()) {
if dimension(Ar())<>2 then Error "This is for 2D arrays"
integer i, j, n=dimension(Ar(),1), n1=dimension(Ar(),2)
for i=1 to n
for j=1 to n1
print Ar(i, j),
next
print
next
}
Function Global MakeArray(n as integer=5) {
dim a(1 to n, 1 to n) as integer=0
integer i=1, j=1, z, t1=1
boolean ch=true
for z=0 to n*n-1
if ch then a(i,j)=z else a(j,i)=z
j++
if j>t1 then t1++: j=1:i=t1: ch~ else i--
if i<1 then i=t1 else.if i>n then i=n: j++
if j>n then j=i+2: i=n:ch~
next
=a() // return array (as a pointer)
}
}
Module Zig_Zag_Matrix (n as integer=5) {
Pen 15 {Report "matrix "+n+"x"+n}
integer old_column=tab
Print $(,4) // set column to 4 chars
if random(1,2)=2 then
dim ret()
ret()=makeArray(n) // this get a copy
else
object a=makeArray(n) // but this get the copy of pointer
link a to ret() // ret() is reference to a, to array
end if
PrintArray &ret()
Print $(,old_column)
}
Inline Code Lib1 // just execute the code from module lib1 like was here
Form 60, 36 \\ console 60x36 characters
Report 2, "Zig-zag matrix" // 2 for center
Pen 14 {Zig_Zag_Matrix 1}
Pen 11 {Zig_Zag_Matrix 2}
Pen 14 {Zig_Zag_Matrix 3}
Pen 11 {Zig_Zag_Matrix 4}
Pen 14 {Zig_Zag_Matrix 5}
Pen 11 {Zig_Zag_Matrix 10}
</syntaxhighlight>
 
Line 4,948 ⟶ 5,099:
 
=={{header|Prolog}}==
{{Works with|SWiSWI-Prolog}}
<syntaxhighlight lang="prolog">zig_zag(N) :-
zig_zag(N, N).
Line 5,697 ⟶ 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,742 ⟶ 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,817 ⟶ 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,409 ⟶ 6,643:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var zigzag = Fn.new { |n|
1,150

edits