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)


 
MODULE Algebras;
IMPORT StdLog,Strings;
 
TYPE
	Matrix = POINTER TO ARRAY OF ARRAY OF INTEGER;
 
PROCEDURE NewIdentityMatrix(n: INTEGER): Matrix;
VAR
	m: Matrix;
	i: INTEGER;
BEGIN
	NEW(m,n,n);
	FOR i := 0 TO n - 1 DO
		m[i,i] := 1;
	END;
	RETURN m;
END NewIdentityMatrix;
 
PROCEDURE Show(m: Matrix);
VAR
	i,j: INTEGER;
BEGIN
	FOR i := 0 TO LEN(m,0) - 1 DO
		FOR j := 0 TO LEN(m,1) - 1 DO
			StdLog.Int(m[i,j]);
		END;
		StdLog.Ln
	END
END Show;
 
PROCEDURE Do*;
BEGIN
	Show(NewIdentityMatrix(5));
END Do;
END Algebras.
 