Zig-zag matrix: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
No edit summary
Line 1,781: Line 1,781:
9 11 17 20 22
9 11 17 20 22
10 18 19 23 24
10 18 19 23 24
</pre>

=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun zigzag = List by int n
List matrix = List[].with(n)
for int y = 0; y < n; y++ do matrix[y] = int[].with(n) end
int y, x = 1
for int value = 0; value < n * n; value++
matrix[y - 1][x - 1] = value
if (y + x) % 2 == 0
if x < n do x++
else do y += 2 end
if y > 1 do y-- end
else
if y < n do y++
else do x += 2 end
if x > 1 do x-- end
end
end
return matrix
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()
end
end
dump(zigzag(5))
writeLine()
dump(zigzag(10))
</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

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
</pre>
</pre>