Write a Modular C Programming Code for Identity of a 2D Matrix?
CODE:
#include<stdio.h> #define ROW 10 #define COL 10 void readMatrix(int [ROW][COL],int ,int); void displayMatrix(int [ROW][COL],int ,int); int identityMatrix(int a[ROW][COL],int r,int c); main() { int r,c,i; int a[ROW][COL]; int b[ROW][COL]; int res[ROW][COL]={0}; printf("Enter the order of matrix: "); scanf("%d%d",&r,&c); printf("\nEnter the Elements of matrix:\n"); readMatrix(a,r,c); printf("\nThe Given matrix is :\n"); displayMatrix(a,r,c); i=identityMatrix(res,r,c); if(i==1) printf("\nIt is Identity matrix"); else printf("\nNot Identity matrix"); } void readMatrix(int a[ROW][COL],int r ,int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) scanf("%d",&a[i][j]); } } void displayMatrix(int a[ROW][COL],int r,int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } } int identityMatrix(int a[ROW][COL],int r,int c) { int i,j,flag=1; for(i=0;i<r;i++) { for(j=0;j<c;j++) { if(i==j && a[i][j]!=1) flag=0; else if(i!=j && a[i][j]!=0) flag=0; } } return flag; }
OUTPUT
Enter the order of matrix: 3 3 Enter the Elements of matrix: 5 6 2 1 8 6 4 3 2 The Given matrix is : 5 6 2 1 8 6 4 3 2 Not Identity matrix Process returned 0 (0x0) execution time : 8.883 s Press any key to continue.
Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:
Top 100+ C Programming codes – KLE Technological University