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)


include c:\cxpl\codes;
def IntSize = 4;                        \number of bytes in an integer
int Matrix, Size, I, J;
 
[Text(0, "Size: ");  Size:= IntIn(0);
Matrix:= Reserve(Size*IntSize);         \reserve memory for 2D integer array
for I:= 0 to Size-1 do
        Matrix(I):= Reserve(Size*IntSize);
for J:= 0 to Size-1 do                  \make array an identity matrix
    for I:= 0 to Size-1 do
        Matrix(I,J):= if I=J then 1 else 0;
for J:= 0 to Size-1 do                  \display the result
    [for I:= 0 to Size-1 do
        [IntOut(0, Matrix(I,J));  ChOut(0, ^ )];
    CrLf(0);
    ];
]