Followers

Saturday, December 3, 2011

SUM OF TWO MATRICES : SIMPLE

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

void main()
{
 clrscr();
 int x[10][10], y[10][10], z[10][10] , n, m, i, j;
 char dec;
 cout<<"The number of rows and columns shoud be equal in both the matrices for any \narithmentic operation ";
 do{
    cout<<"\nEnter the number of rows :";
    cin>>n;
    cout<<"Enter the number of columns :";
    cin>>m;
    cout<<"Enter the matrix X :\n";
    for(i=0 ; i<n ; i++)
    {
      for(j=0 ; j<m ; j++)
      cin>>x[i][j];
    }

    cout<<"The matrix X :\n";
    for(i=0 ; i<n ; i++)
    {
      cout<<"\n";
      for(j=0 ; j<m ; j++)
      cout<<" "<<x[i][j];
    }
    cout<<"\nIs the matrix correct? (Y/N) : ";
    cin>>dec;
 }while(dec=='N'|| dec=='n');
do{
   cout<<"\nEnter the matrix Y :\n";
   for(i=0 ; i<n ; i++)
   {
     for(j=0 ; j<m ; j++)
       cin>>y[i][j];
   }

   cout<<"The matrix Y :\n";
   for(i=0 ; i<n ; i++)
   {
     cout<<"\n";
     for(j=0 ; j<m ; j++)
       cout<<" "<<y[i][j];
   }
   cout<<"\nIs the matrix correct? (Y/N) : ";
      cin>>dec;

}while(dec=='N'|| dec=='n');

 for(i=0 ; i<n ; i++)
 {
   for(j=0 ; j<m ; j++)
     z[i][j]= x[i][j] + y[i][j];
 }
 cout<<"\nThe sum of the two given matrices : ";

 for(i=0 ; i<n ; i++)
 {
   cout<<"\n";
   for(j=0 ; j<m ; j++)
     cout<<" "<<z[i][j];
 }

 getch();
}
.................................................................
OUTPUT:(click to enlarge the image)


No comments:

Post a Comment