How to resolve the algorithm Identity matrix step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Identity matrix step by step in the Action! programming language

Table of Contents

Problem Statement

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}}}

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Identity matrix step by step in the Action! programming language

Source code in the action! programming language

PROC CreateIdentityMatrix(BYTE ARRAY mat,BYTE size)
  CARD pos
  BYTE i,j

  pos=0
  FOR i=1 TO size
  DO
    FOR j=1 TO size
    DO
      IF i=j THEN 
        mat(pos)=1
      ELSE
        mat(pos)=0
      FI
      pos==+1
    OD
  OD
RETURN

PROC PrintMatrix(BYTE ARRAY mat,BYTE size)
  CARD pos
  BYTE i,j,v

  pos=0
  FOR i=1 TO size
  DO
    FOR j=1 TO size
    DO
      v=mat(pos)
      IF j=size THEN
        PrintF("%I%E",v)
      ELSE
        PrintF("%I ",v)
      FI
      pos==+1
    OD
  OD
RETURN

PROC Main()
  BYTE size
  BYTE ARRAY mat(400)
  BYTE LMARGIN=$52,old

  old=LMARGIN
  LMARGIN=0 ;remove left margin on the screen

  DO
    Print("Get size of matrix [1-20] ")
    size=InputB()
  UNTIL size>=1 AND size<=20
  OD

  CreateIdentityMatrix(mat,size)
  PrintMatrix(mat,size)

  LMARGIN=old ;restore left margin on the screen
RETURN

  

You may also check:How to resolve the algorithm Substring/Top and tail step by step in the Objeck programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Fortran programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the Phix programming language
You may also check:How to resolve the algorithm Function definition step by step in the CLU programming language