This C++ program finds the transpose of a matrix and checks whether it is symmetric or not. A matrix is symmetric if both the matrix and its transpose are equal. The program reads the row and column values of the matrix into the variable m and n. Then the elements are read one by one into the 2D array a[][] using a nested for loop and at the same time the address of the first element of each row is stored to the pointer array p[]. Then the transpose of the matrix a[][] is found and stored in the array b[][]. Next the elements of the arrays a[][] and b[][] are compared to find any dissimilarity. If there is any dissimilarity, the matrix is not symmetric, otherwise the matrix is symmetric.
Program
#include<iostream.h> #include<conio.h> #include<process.h> void main() { int a[10][10],b[10][10],f=0,*p1[10],*p2[10],m,n,r,c,i,j; cout<<"Enter the order of matrics: "; cin>>m>>n; cout<<"\n\nEnter the elements\n\n"; for(i=0;i<m;i++) { p1[i]=&a[i][0]; for(j=0;j<n;j++) { cin>>a[i][j]; } } r=m;r c=n; for(i=0;i<c;i++) { p2[i]=&b[i][0]; for(j=0;j<r;j++) { b[i][j]=*(p1[j]+i); } } cout<<"\n\nTranspose Matrics\n\n"; for(i=0;i<c;i++) { for(j=0;j<r;j++) { cout<<b[i][j]<<"\t"; } cout<<"\n"; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if((*(*(a+i)+j))!=(*(*(b+i)+j))) { f=1; break; } } } if(f==0) { cout<<"\n\nThe matrix is symmetric"; } else cout<<"\n\nThe matrix is not symmetric"; getch(); }
Related post:- c program to find the transpose of a matrix
No comments:
Post a Comment