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


*        Zig-zag matrix            15/08/2015
ZIGZAGMA CSECT
         USING  ZIGZAGMA,R12       set base register
         LR     R12,R15            establish addressability 
         LA     R9,N               n : matrix size
         LA     R6,1               i=1
         LA     R7,1               j=1
         LR     R11,R9             n
         MR     R10,R9             *n
         BCTR   R11,0              R11=n**2-1
         SR     R8,R8              k=0
LOOPK    CR     R8,R11             do k=0 to n**2-1
         BH     ELOOPK             k>limit
         LR     R1,R6              i
         BCTR   R1,0               -1
         MR     R0,R9              *n
         LR     R2,R7              j
         BCTR   R2,0               -1
         AR     R1,R2              (i-1)*n+(j-1)
         SLA    R1,1               index=((i-1)*n+j-1)*2
         STH    R8,T(R1)           t(i,j)=k
         LR     R2,R6              i
         AR     R2,R7              i+j
         LA     R1,2               2
         SRDA   R2,32              shift right r1 to r2
         DR     R2,R1              (i+j)/2
         LTR    R2,R2              if mod(i+j,2)=0
         BNZ    ELSEMOD
         CR     R7,R9              if j<n
         BNL    ELSE1
         LA     R7,1(R7)           j=j+1
         B      EIF1
ELSE1    LA     R6,2(R6)           i=i+2
EIF1     CH     R6,=H'1'           if i>1
         BNH    NOT1
         BCTR   R6,0               i=i-1
NOT1     B      NOT2
ELSEMOD  CR     R6,R9              if i<n
         BNL    ELSE2
         LA     R6,1(R6)           i=i+1
         B      EIF2
ELSE2    LA     R7,2(R7)           j=j+2
EIF2     CH     R7,=H'1'           if j>1
         BNH    NOT2
         BCTR   R7,0               j=j-1
NOT2     LA     R8,1(R8)           k=k+1
         B      LOOPK
ELOOPK   LA     R6,1               end k; i=1
LOOPI    CR     R6,R9              do i=1 to n
         BH     ELOOPI             i>n
         LA     R10,0              ibuf=0  buffer index
         MVC    BUFFER,=CL80' '
         LA     R7,1               j=1
LOOPJ    CR     R7,R9              do j=1 to n
         BH     ELOOPJ             j>n
         LR     R1,R6              i
         BCTR   R1,0               -1
         MR     R0,R9              *n
         LR     R2,R7              j
         BCTR   R2,0               -1
         AR     R1,R2              (i-1)*n+(j-1)
         SLA    R1,1               index=((i-1)*n+j-1)*2
         LH     R2,T(R1)           t(i,j)
         LA     R3,BUFFER
         AR     R3,R10
         XDECO  R2,XDEC            edit t(i,j) length=12
         MVC    0(4,R3),XDEC+8     move in buffer length=4
         LA     R10,4(R10)         ibuf=ibuf+1
         LA     R7,1(R7)           j=j+1
         B      LOOPJ
ELOOPJ   XPRNT  BUFFER,80          end j
         LA     R6,1(R6)           i=i+1
         B      LOOPI
ELOOPI   XR     R15,R15            end i; return_code=0
         BR     R14                return to caller
N        EQU    5                  matrix size
BUFFER   DS     CL80
XDEC     DS     CL12
T        DS     (N*N)H             t(n,n) matrix 
         YREGS
         END    ZIGZAGMA