Home  >  Article  >  Backend Development  >  Identity matrix program in C language

Identity matrix program in C language

WBOY
WBOYforward
2023-08-30 10:45:05677browse

Identity matrix program in C language

Given a square matrix M[r][c], where "r" is a certain number of rows and "c" is the column such that r = c, we have to check " Whether M" is the identity matrix.

Identity matrix

The identity matrix is ​​also called the identity matrix of size nxn square matrix, in which the integer value of the diagonal elements is 1 and the integer value of the non-diagonal elements is 0 p>

Like the example given below-

$$I1=\begin{bmatrix}1 \end{bmatrix},\ I2=\begin{bmatrix}1 & 0 \0 & 1 \end{bmatrix},\ I3=\begin{bmatrix}1 &0 & 0 \0 &1 & 0 \0 &0 &1 \end{bmatrix},\In=\begin{bmatrix}

1 &0 &0 &...&0 \

0 &1 &0 &...&0\

0 &0 &1 &...&0\

. &. &. &...&.\

. &. &. &...&.\

0 &0 &0 &...&1\

\end{bmatrix} $$

Example

Input: m[3][3] = { {1, 0, 0},
   {0, 1, 0},
   {0, 0, 1}}
Output: yes
Input: m[3][3] == { {3, 0, 1},
   {6, 2, 0},
   {7, 5, 3} }
Output: no

Algorithm

Start
Step 1 -> declare function for finding identity matrix
   int identity(int num)
      declare int row, col
      Loop For row = 0 and row < num and row++
         Loop For col = 0 and col < num and col++
            IF (row = col)
               Print 1
            Else
               Print 0
      End
   End
Step 2 -> In main()
   Declare int size = 4
   Call identity(size)
Stop

Example

#include<stdio.h>
int identity(int num){
   int row, col;
   for (row = 0; row < num; row++){
      for (col = 0; col < num; col++){
         if (row == col)
            printf("%d ", 1);
         else
            printf("%d ", 0);
      }
      printf("</p><p>");
   }
   return 0;
}
int main(){
   int size = 4;
   identity(size);
   return 0;
}

Output

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

The above is the detailed content of Identity matrix program in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete