"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Monday, February 1, 2010

Write a program to check equality of two given matrices with pxq and mxn dimensions.


#include<iostream.h>
#include<conio.h>
#include <process.h>

void main()
{
int mat1[10][10] , mat2[10][10];

int m,n,p,q;

// Input elements (matrix 1 ) ------

cout<< "\n Enter no. of rows for matrix 1:";
cin >> m;
cout<< "\n Enter no. of columns :";

cin >> n;


for(int i = 0; i < m ; i++)
 for( int j = 0; j < n  ; j++)

{cout<<"\n Enter elements for "<< i+1 << "," << j+1 < <"location :";
cin >> mat1[i][j];
}


// Input elements (matrix 2 ) ------
cout<< "\n Enter no. of rows for matrix 2:";
cin >> p;
cout<< "\n Enter no. of columns :";
cin >> q;

for(i = 0; i < p ; i++)
for(j = 0; j < q ; j++)
{cout<<"\n Enter elements for "<< i+1 << "," << j+1 < <"location :";
cin >> mat2[i][j];
}

// check dimensions
if (( m ! = n ) || (p ! = q) )
{
cout<< "\n Two matrices are unequal . ";
getch ( );
exit (1);
}

//check individual elements

for( i = 0; i < m ; i++)

 for( j = 0; j < n; j++)
     if(mat1[i][j] ! = mat2[i][j])
      {
      cout<< "\n Two matrices are unequal . ";
      getch ( );
      exit (1);
       }


cout<< " \n Two matrices are exactly equal . "
//Print the array
cout<<"\n The matrix : \n\n";
for( i = 0; i < m ; i++)
{for( j = 0; j < n ; j++)
cout<< mat1[i][j]<<"\t";
cout<< "\n";
}
getch ( );
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.