Task

Build an   identity matrix   of a size known at run-time.



An identity matrix is a square matrix of size n × n,

where the diagonal elements are all 1s (ones),

and all the other elements are all 0s (zeroes).







I

n


=


[



1


0


0


⋯


0




0


1


0


⋯


0




0


0


1


⋯


0




⋮


⋮


⋮


⋱


⋮




0


0


0


⋯


1



]




{\displaystyle I_{n}={\begin{bmatrix}1&0&0&\cdots &0\\0&1&0&\cdots &0\\0&0&1&\cdots &0\\\vdots &\vdots &\vdots &\ddots &\vdots \\0&0&0&\cdots &1\\\end{bmatrix}}}





Related tasks

   Spiral matrix
   Zig-zag matrix 
   Ulam_spiral_(for_primes)


100 PROGRAM "Identity.bas"
110 INPUT PROMPT "Enter size of matrix: ":N
120 NUMERIC A(1 TO N,1 TO N)
130 CALL INIT(A)
140 CALL WRITE(A)
150 DEF INIT(REF T)
160   FOR I=LBOUND(T,1) TO UBOUND(T,1)
170     FOR J=LBOUND(T,2) TO UBOUND(T,2)
180       LET T(I,J)=0
190     NEXT
200     LET T(I,I)=1
210   NEXT
220 END DEF
230 DEF WRITE(REF T)
240   FOR I=LBOUND(T,1) TO UBOUND(T,1)
250     FOR J=LBOUND(T,2) TO UBOUND(T,2)
260       PRINT T(I,J);
270     NEXT
280     PRINT
290   NEXT
300 END DEF