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)


 
# syntax: GAWK -f IDENTITY_MATRIX.AWK size
BEGIN {
    size = ARGV[1]
    if (size !~ /^[0-9]+$/) {
      print("size invalid or missing from command line")
      exit(1)
    }
    for (i=1; i<=size; i++) {
      for (j=1; j<=size; j++) {
        x = (i == j) ? 1 : 0
        printf("%2d",x) # print
        arr[i,j] = x # build
      }
      printf("\n")
    }
    exit(0)
}
 