Thursday, October 20, 2011

C++ program to print matrix difference


//C++ program to subtract two matrices
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][4],matrix2[3][4],matrix3[3][4];
int i,j;
cout<<"\nEnter numbers to fill matrix 1\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix1[i][j];
  }
}
cout<<"\nEnter numbers to fill matrix 2\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cin>>matrix2[i][j];
  }
}

cout<<"Original Matrix 1:\n";
for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix1[i][j]<<" ";
  }
 cout<<"\n";
}
cout<<"\n\nOriginal Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   cout<<matrix2[i][j]<<" ";
  }
cout<<"\n";
}
cout<<"\n\nDifference of Matrix 1 and Matrix 2:\n";

for(i=0;i<3;i++)
{
  for(j=0;j<4;j++)
  {
   matrix3[i][j]=matrix1[i][j]-matrix2[i][j];
   cout<<matrix3[i][j]<<" ";
  }
cout<<"\n";
}

getch();
}