Thursday 31 January 2013

Addition of matrices in c++

#include<iostream.h>
#include<conio.h>
void main()
{

int r1,r2,c1,c2,i,j;
int matrix1[12][12];
int matrix2[12][12];
int add[12][12];

        cout<<"enter no of rows of 1st matrix       ";
        cin>>r1;
        cout<<"enter no of col of 1st matrix        ";
        cin>>c1;
        cout<<"\nenter no of rows of 2nd matrix       ";
        cin>>r2;
        cout<<"enter no of col of 2nd matrix        ";
        cin>>c2;

if(r1==r2&&c1==c2)
{
   cout<<"\n\n";
    for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
    {cout<<"matrix1["<<i<<"]["<<j<<"]";
    cin>>matrix1[i][j];}


   cout<<"\n\n";
    for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
    {cout<<"matrix2["<<i<<"]["<<j<<"]";
    cin>>matrix2[i][j];}

      cout<<"\n\n";
      cout<<"the first matrix is";
      cout<<"\n\n";
        for(i=0;i<r1;i++)
        {for(j=0;j<c1;j++)
        cout<<matrix1[i][j]<<"\t";
        cout<<endl;}

      cout<<"the 2nd matrix is";
      cout<<"\n\n";
        for(i=0;i<r2;i++)
        {for(j=0;j<c2;j++)
        cout<<matrix2[i][j]<<"\t";
        cout<<endl;}

   cout<<"\nTHE RESULT OF ADDITION IS\n";
   for(i=0;i<r2;i++)
    {for(j=0;j<c2;j++)
   {add[i][j]=matrix1[i][j]+matrix2[i][j];
   cout<<add[i][j]<<"\t";}
   cout<<endl;}

}                                                  //end of if
else
cout<<"\norder is not same\n";
getch();
}

0 comments:

Post a Comment