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 "seed7_05.s7i";
 
const type: matrix is array array integer;
 
const func matrix: identity (in integer: size) is func
  result
    var matrix: identity is matrix.value;
  local
    var integer: index is 0;
  begin
    identity := size times size times 0;
    for index range 1 to size do
      identity[index][index] := 1;
    end for;
  end func;
 
const proc: writeMat (in matrix: a) is func
  local
    var integer: i is 0;
    var integer: num is 0;
  begin
    for key i range a do
      for num range a[i] do
        write(num lpad 2);
      end for;
      writeln;
    end for;
  end func;
 
const proc: main is func
  begin
    writeMat(identity(6));
  end func;