PROGRAM FOR MATRIX ADDDITION,SUBTRACTION&MULTIPLICATION USING C++

#include <iostream>
#include <stdlib>
using namespace std;

int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,r1,c1,r2,c2;
cout<<"Enter the row size of the first matrix";
cin>>r1;
cout<<"Enter the column size of the first matrix";
cin>>c1;
cout<<"Enter the row size of the second matrix";
cin>>r2;
cout<<"Enter the column size of the  second matrix";
cin>>c2;
cout<<"Enter the A matrix";
for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        cin>>a[i][j];
    }
}
cout<<"Enter the B matrix";
for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        cin>>b[i][j];
    }
}
// performing  Matrix Addition
for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        c[i][j]=a[i][j]+b[i][j];
    }
}
cout<<"Addition Matrix ";

for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        cout<<c[i][j]<<"\t";
    }
    cout<<"\n";
}
// performing  Matrix Subtraction
for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        c[i][j]=a[i][j]-b[i][j];
    }
}
cout<<"Subtraction Matrix ";

for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        cout<<c[i][j]<<"\t";
    }
    cout<<"\n";
}
// performing  Matrix multiplication
if (c1!=r2)
{
   cout<<"matrix multiplication cannot be prformed";
   exit(0);
}
for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c1;j++)
    {
        c[i][j]=0;
        for(k=1;k<=c1;k++)
        {
        c[i][j]=c[i][j]+a[i][k]+b[k][j];
        }
    }
}
cout<<"Multiplication  Matrix ";

for(i=1 ;i<=r1;i++)
{
    for (j=1;j<=c2;j++)
    {
        cout<<c[i][j]<<"\t";
    }
    cout<<"\n";
}
return 0;
}

No comments:

Post a Comment