Task

Produce a spiral array.



A   spiral array   is a square arrangement of the first    N2   natural numbers,   where the

numbers increase sequentially as you go around the edges of the array spiraling inwards.



For example, given   5,   produce this array:

 0  1  2  3  4
15 16 17 18  5
14 23 24 19  6
13 22 21 20  7
12 11 10  9  8



Related tasks

   Zig-zag matrix 
   Identity_matrix
   Ulam_spiral_(for_primes)


100 PROGRAM "SpiralMa.bas"
110 TEXT 80
120 INPUT PROMPT "Enter size of matrix (max. 10): ":N
130 NUMERIC A(1 TO N,1 TO N)
140 CALL INIT(A)
150 CALL WRITE(A)
160 DEF INIT(REF T)
170   LET BCOL,BROW,COL,ROW=1:LET TCOL,TROW=N:LET DIR=0
180   FOR I=0 TO N^2-1
190     LET T(COL,ROW)=I
200     SELECT CASE DIR
210     CASE 0
220       IF ROW<TROW THEN
230         LET ROW=ROW+1
240       ELSE
250         LET DIR=1:LET COL=COL+1:LET BCOL=BCOL+1
260       END IF
270     CASE 1
280       IF COL<TCOL THEN
290         LET COL=COL+1
300       ELSE
310         LET DIR=2:LET ROW=ROW-1:LET TROW=TROW-1
320       END IF
330     CASE 2
340       IF ROW>BROW THEN
350         LET ROW=ROW-1
360       ELSE
370         LET DIR=3:LET COL=COL-1:LET TCOL=TCOL-1
380       END IF 
390     CASE 3
400       IF COL>BCOL THEN
410         LET COL=COL-1
420       ELSE
430         LET DIR=0:LET ROW=ROW+1:LET BROW=BROW+1
440       END IF
450     END SELECT
460   NEXT
470 END DEF
480 DEF WRITE(REF T)
490   FOR I=LBOUND(T,1) TO UBOUND(T,1)
500     FOR J=LBOUND(T,2) TO UBOUND(T,2)
510       PRINT USING " ##":T(I,J);
520     NEXT
530     PRINT
540   NEXT
550 END DEF