code to determine whether the matrix is symmetric or not but either ways it give me symmetric
Dec 3, 2016 at 10:57am UTC
I have question
I wrote that code to determine whether the matrix is symmetric or not but either ways it give me symmetric and I see nothing's wrong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
using namespace std ;
bool SYM (int a[][3] , int size , int b[][3] , int length){
for (int i=0 ; i<size ; i++){
for (int j =0 ; j<3 ; j++){
if (a[i][j] == b[i][j] )
return true ;
else if (a[i][j] != b[i][j])
return false ;
}
}
}
void disp (int x[][3], int s){
for (int i=0 ; i<s ; i++){
for (int j =0 ; j<3 ; j++)
{
cout << x[i][j] << "\t" ;
}
cout<<endl;
}
}
int main (){
int a[3][3] ,t[3][3];
bool R;
cout <<"Enter your matrix values \"5*5\" " <<endl;
for (int i=0 ; i<3 ; i++)
for (int j =0 ; j<3 ; j++){
cin>> a[i][j];
t[j][i]=a[i][j];
}
cout << "The matrix : " <<endl;
disp(a,3);
cout<<"The transpose :" <<endl;
disp (t,3);
R =SYM (a , 3 , t, 3);
if (R )
cout<<"sym" ;
else
cout<< "not sym " ;
return 0;
}
Dec 3, 2016 at 2:11pm UTC
SYM should be like this:
1 2 3 4 5 6 7 8 9
for (int i=0 ; i<size ; i++)
{
for (int j =0 ; j<3 ; j++)
{
if (a[i][j] != b[i][j] )
return false ; // first time occurrence is good enough to fail
}
}
return true ; // must get all the way through to pass
Also, if you want to, you can write an additional line and ignore diagonal elements of the array where the row and column are the same
Dec 3, 2016 at 3:16pm UTC
ooh thank u that was helpful
Topic archived. No new replies allowed.