RosettaCodeData/Task/Zig-zag-matrix/Elena/zig-zag-matrix.elena
2019-09-12 10:33:56 -07:00

44 lines
837 B
Text

import extensions;
extension op : IntNumber
{
zigzagMatrix()
{
auto result := new IntMatrix(self, self);
int i := 0;
int j := 0;
int d := -1;
int start := 0;
int end := self*self - 1;
while (start < end)
{
result.setAt(i, j, start); start += 1;
result.setAt(self - i - 1, self - j - 1, end); end -= 1;
i := i + d;
j := j - d;
if (i < 0)
{
i:=i+1; d := d.Negative
}
else if (j < 0)
{
j := j + 1; d := d.Negative
}
};
if (start == end)
{
result.setAt(i, j, start)
};
^ result
}
}
public program()
{
console.printLine(5.zigzagMatrix()).readChar()
}