Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

19 Mar 2013

SUBTRACTION OF TWO MATRICES IN C++

Write a program for subtraction of two matrices
                      OR
Write a program to subtract two matrices

#include<iostream.h>
#include<conio.h>
void main()
{  
clrscr();
 int a[10][10],b[10][10],c[10][10];
 int i, j, n, m;
 int p,q;
 cout<<"enter no of rows of 1st matrix\n";
 cin>>m;
 cout<<"enter no of columns of 1st matrix\n";
 cin>>n;
  cout<<"enter no of rows of 2nd matrix\n";
 cin>>p;
 cout<<"enter no of column 2nd matrix\n";
 cin>>q;
 if(m==p && n==q)
 {
  cout<<"\n\nenter the elements of 1st matrix:=\n";
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
      cin>>a[i][j];
     }
   }
  cout<<"\n\n1st matrix u have entered is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<a[i][j];
     }
    cout<<endl;
   }
 cout<<"\n\nenter the elements of 2nd matrix:=\n";
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"b["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>b[i][j];
    }

  }
 cout<<"\n\n2nd matrix u have entered is:="<<endl;
 for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
 }
 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     c[i][j]=a[i][j]-b[i][j];
    }
  }
  cout<<"\n\nsubtraction of two matrices is:="<<endl;
  for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
     {
      cout<<"\t"<<c[i][j];
     }
    cout<<endl;
   }
  }
   else
    {
     cout<<"matrices cannot be subtracted";
     }

getch();
}

==OUTPUT==
enter no of rows of  1st matrix
2
enter no of columns of  1st matrix
2
enter no of rows of 2nd matrix
2
enter no of columns of 2nd matrix
2
enter the elements of 1st matrix:=
a[1][1]:=1
a[1][2]:=2
a[2][1]:=3
a[2][2]:=1

1st matrix you have entered is:=
    1    2
    3    1

enter the elements of 2nd matrix:=
b[1][1]:=2
b[1][2]:=5
b[2][1]:=0
b[2][2]:=5

2nd matrix you have entered is:=
    2    5
    0    5

subtraction of two matrices is:=
    -1   -3
     3   -4

No comments:

Post a Comment