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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Identity matrix step by step in the VBScript 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 VBScript programming language

Source code in the vbscript programming language

build_matrix(7)

Sub build_matrix(n)
	Dim matrix()
	ReDim matrix(n-1,n-1)
	i = 0
	'populate the matrix
	For row = 0 To n-1
		For col = 0 To n-1
			If col = i Then
				matrix(row,col) = 1
			Else
				matrix(row,col) = 0
			End If	
		Next
		i = i + 1
	Next
	'display the matrix
	For row = 0 To n-1
		For col = 0 To n-1
			If col < n-1 Then
				WScript.StdOut.Write matrix(row,col) & " "
			Else
				WScript.StdOut.Write matrix(row,col)
			End If
		Next
		WScript.StdOut.WriteLine
	Next
End Sub

n = 8

arr = Identity(n)

for i = 0 to n-1
    for j = 0 to n-1
        wscript.stdout.Write arr(i,j) & " "
    next
    wscript.stdout.writeline
next

Function Identity (size)
    Execute Replace("dim a(#,#):for i=0 to #:for j=0 to #:a(i,j)=0:next:a(i,i)=1:next","#",size-1)
    Identity = a
End Function

  

You may also check:How to resolve the algorithm Secure temporary file step by step in the C++ programming language
You may also check:How to resolve the algorithm Subleq step by step in the Swift programming language
You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the Python programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the COBOL programming language