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


 
Public Sub zigzag(n)
Dim a() As Integer
'populate a (1,1) to a(n,n) in zigzag pattern
 
'check if n too small
If n < 1 Then
  Debug.Print "zigzag: enter a number greater than 1"
  Exit Sub
End If
 
'initialize
ReDim a(1 To n, 1 To n)
i = 1       'i is the row
j = 1       'j is the column
P = 0       'P is the next number
a(i, j) = P 'fill in initial value
 
'now zigzag through the matrix and fill it in
Do While (i <= n) And (j <= n)
  'move one position to the right or down the rightmost column, if possible
  If j < n Then
    j = j + 1
  ElseIf i < n Then
    i = i + 1
  Else
    Exit Do
  End If
  'fill in
  P = P + 1: a(i, j) = P
  'move down to the left
  While (j > 1) And (i < n)
    i = i + 1: j = j - 1
    P = P + 1: a(i, j) = P
  Wend
  'move one position down or to the right in the bottom row, if possible
  If i < n Then
    i = i + 1
  ElseIf j < n Then
    j = j + 1
  Else
    Exit Do
  End If
  P = P + 1: a(i, j) = P
  'move back up to the right
  While (i > 1) And (j < n)
    i = i - 1: j = j + 1
    P = P + 1: a(i, j) = P
  Wend
Loop
 
'print result
Debug.Print "Result for n="; n; ":"
For i = 1 To n
  For j = 1 To n
    Debug.Print a(i, j),
  Next
  Debug.Print
Next
End Sub
 