Write a program to check whether a matrix(use dynamic memory allocation
to create the matrix) is
i)Identity
ii)Diagonal
#include<stdio.h> #include<conio.h> #include<alloc.h> int chkIdentity(int**,int,int); int chkDiagonal(int**,int,int); void main() { int **a; int noOfRows,noOfCols; int i,j; int identity,diagonal; clrscr(); printf("\nEnter Number Of Rows: "); scanf("%d",&noOfRows); printf("\nEnter Number Of Columns: "); scanf("%d",&noOfCols); if(noOfRows!=noOfCols) { printf("\nThe Matrix Is Not A Square Matrix So It Cannot Be Verified"); printf("\nPress Any Key To Exit.."); getch(); return; }/*End of if*/ a=(int**)calloc(noOfRows,sizeof(int*)); for(i=0;i<noOfRows;i++) a[i]=(int *)calloc(noOfCols,sizeof(int *)); printf("\nEnter Elements Of %d X %d Matrix: \n",noOfRows,noOfCols); for(i=0;i<noOfRows;i++) for(j=0;j<noOfCols;j++) scanf("%d",a[i]+j); printf("\nThe Matrix You Entered Is: \n"); for(i=0;i<noOfRows;i++) { for(j=0;j<noOfCols;j++) { printf("%d ",a[i][j]); }/*End of j for loop*/ printf("\n"); }/*End of i for loop*/ identity=chkIdentity(a,noOfRows,noOfCols); diagonal=chkDiagonal(a,noOfRows,noOfCols); if(identity==1) printf("\nThe Matrix Is An Identity Matrix."); else printf("\nThe Matrix Is Not An Identity Matrix."); if(diagonal) printf("\nThe Matrix Is A Diagonal Matrix."); else printf("\nThe Matrix Is Not A Diagonal Matrix."); getch(); }/*End of main()*/ int chkIdentity(int**a,int rows,int cols) { int i,j; int count=0; for(i=0;i<rows;i++) { for(j=0;j<cols;j++) { if(i!=j) { if(a[i][j]!=0) return 0; }/*End of if*/ if(i==j) { if(a[i][j]==1) count++; }/*End of if*/ }/*End of j for loop*/ }/*End of i for loop*/ if(count==rows) return 1; else return 0; }/*End of function int chkIdentity(int**,int,int)*/ int chkDiagonal(int**a,int rows,int cols) { int i,j; for(i=0;i<rows;i++) { for(j=0;j<cols;j++) { if(i!=j) { if(a[i][j]!=0) return 0; }/*End of if*/ if(i==j) { if(a[i][j]==0) return 0; }/*End of if*/ }/*End of j for loop*/ }/*End of i for loop*/ return 1; }/*End of function int chkDiagonal(int**,int,int)*/