C Tutorial

Identity Matrix is the matrix equivalent of the number 1 and represented by I always.

Identity matrix is a square and has same number of rows and columns, then all diagonal place value is 1's and remaining place 0's.

Same matrix is the result when any matrix multiplied by identity matrix.

Let us see example 4x4 identity matrix,

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

This c program is used to validate the user input matrix is identity matrix or not.

This program is implemented using two dimensional array for matrix data and validating logic is done using nested for loops.

#include<stdio.h> #define COLUMN 10 #define ROW 10 void main() { int identity_matrix[ROW][COLUMN]; int i,j, rows, columns, validate=1; printf("\nEnter Matrix Rows: "); scanf("%d", &rows); printf("\nEnter Matrix Columns: "); scanf("%d", &columns); printf("\nEnter Matrix: \n"); for(i=0;i<rows;i++) { for(j=0;j<columns;j++) { scanf("%d", &identity_matrix[i][j]); } } for(i=0;i<rows;i++) { for(j=0;j<columns;j++) { if(i==j && identity_matrix[i][j]!=1) { validate = 0; break; } else { if(i!=j && identity_matrix[i][j] != 0) { validate =0; break; } } } /*if(validate==0) { break; } */ } if(validate == 1) { printf("\nUser input matrix is a identity matrix!\n"); } else { printf("\nUser input matrix is not a Identity Matrix!\n"); } }

Output:

$ cc identity_matrix.c $ ./a.out Enter Matrix Rows: 3 Enter Matrix Columns: 3 Enter Matrix: 1 1 1 1 1 0 0 0 1 User input matrix is not a Identity Matrix! $ ./a.out Enter Matrix Rows: 3 Enter Matrix Columns: 3 Enter Matrix: 1 0 0 0 1 0 0 0 1 User input matrix is a identity matrix!

C Program Examples

C Tutorial

Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page

^