Zig-zag matrix: Difference between revisions

Content added Content deleted
No edit summary
(Added solution for Action!)
Line 147: Line 147:
9 11 17 20 22
9 11 17 20 22
10 18 19 23 24
10 18 19 23 24
</pre>

=={{header|Action!}}==
<lang Action!>DEFINE MAX_SIZE="10"
DEFINE MAX_MATRIX_SIZE="100"

INT FUNC Index(BYTE size,x,y)
RETURN (x+y*size)

PROC PrintMatrix(BYTE ARRAY a BYTE size)
BYTE i,j,v
FOR j=0 TO size-1
DO
FOR i=0 TO size-1
DO
v=a(Index(size,i,j))
IF v<10 THEN
Print(" ")
ELSE
Print(" ")
FI
PrintB(v)
OD
PutE()
OD
RETURN

PROC FillMatrix(BYTE ARRAY a BYTE size)
BYTE start,end
INT dir,i,j

start=0 end=size*size-1
i=0 j=0 dir=1

DO
a(Index(size,i,j))=start
a(Index(size,size-1-i,size-1-j))=end
start==+1 end==-1
i==+dir j==-dir
IF i<0 THEN
i==+1 dir=-dir
ELSEIF j<0 THEN
j==+1 dir=-dir
FI
UNTIL start>=end
OD

IF start=end THEN
a(Index(size,i,j))=start
FI
RETURN

PROC Test(BYTE size)
BYTE ARRAY mat(MAX_MATRIX_SIZE)
PrintF("Matrix size: %B%E",size)
FillMatrix(mat,size)
PrintMatrix(mat,size)
PutE()
RETURN

PROC Main()
Test(5)
Test(6)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Zig-zag_matrix.png Screenshot from Atari 8-bit computer]
<pre>
Matrix size: 5
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

Matrix size: 6
0 1 5 6 14 15
2 4 7 13 16 25
3 8 12 17 24 26
9 11 18 23 27 32
10 19 22 28 31 33
20 21 29 30 34 35
</pre>
</pre>