Task

Produce a zig-zag array.



A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the

numbers increase sequentially as you zig-zag along the array's   anti-diagonals.

For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images).



For example, given   5,   produce this array:

 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



Related tasks

   Spiral matrix
   Identity matrix
   Ulam spiral (for primes)


See also

   Wiktionary entry:   anti-diagonals


 
# syntax: GAWK -f ZIG-ZAG_MATRIX.AWK [-v offset={0|1}] [size]
BEGIN {
# offset: "0" prints 0 to size^2-1 while "1" prints 1 to size^2
    offset = (offset == "") ? 0 : offset
    size = (ARGV[1] == "") ? 5 : ARGV[1]
    if (offset !~ /^[01]$/) { exit(1) }
    if (size !~ /^[0-9]+$/) { exit(1) }
    width = length(size ^ 2 - 1 + offset) + 1
    i = j = 1
    for (n=0; n<=size^2-1; n++) { # build array
      arr[i-1,j-1] = n + offset
      if ((i+j) % 2 == 0) {
        if (j < size) { j++ } else { i+=2 }
        if (i > 1) { i-- }
      }
      else {
        if (i < size) { i++ } else { j+=2 }
        if (j > 1) { j-- }
      }
    }
    for (row=0; row<size; row++) { # show array
      for (col=0; col<size; col++) {
        printf("%*d",width,arr[row,col])
      }
      printf("\n")
    }
    exit(0)
}
 