RosettaCodeData/Task/Zig-zag-matrix/Agena/zig-zag-matrix.agena
2016-12-05 22:15:40 +01:00

48 lines
1 KiB
Text

# zig-zag matrix
makeZigZag := proc( n :: number ) :: table is
local move := proc( x :: number, y :: number, upRight :: boolean ) is
if y = n then
upRight := not upRight;
x := x + 1
elif x = 1 then
upRight := not upRight;
y := y + 1
else
x := x - 1;
y := y + 1
fi;
return x, y, upRight
end ;
# create empty table
local result := [];
for i to n do
result[ i ] := [];
for j to n do result[ i, j ] := 0 od
od;
# fill the table
local x, y, upRight := 1, 1, true;
for i to n * n do
result[ x, y ] := i - 1;
if upRight then
x, y, upRight := move( x, y, upRight )
else
y, x, upRight := move( y, x, upRight )
fi
od;
return result
end;
scope
local m := makeZigZag( 5 );
for i to size m do
for j to size m do
printf( " %3d", m[ i, j ] )
od;
print()
od
epocs