Home > Backend Development > C++ > body text

Identity matrix program in C language

WBOY
Release: 2023-08-30 10:45:05
forward
620 people have browsed it

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
Copy after login

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
Copy after login

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;
}
Copy after login

Output

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!