Powered by Blogger.

Addition Of Two Matrix.

C++ program And Algorithm for addition of two matrices using arrays source code. Matrix addition in c++ language:
















Algorithm:


Addition of two matrices:

Rule: Addition of two matrices is only possible if both matrices are of same size.

Suppose two matrices A and B is of same size m X n

Sum of two matrices is defined as

(A + B)ij  = Aij + Bij
Where 1 ≤ i  m and 1 ≤ j  n

For example:
Suppose two matrices A and B of size of 2 X 3 is as follow:
















             
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>    // for setw()
int main()
{
int m1[10][10],m2[10][10],add[10][10],i,j,r1,c1,c2,r2;
clrscr();
start:
cout<<"Enter how row and column you wants in matrix 1:\n";
cin>>r1>>c1;
cout<<"Enter how row and column you wants in matrix 2:\n";
cin>>r2>>c2;
if(r1!=r2 || c1!=c2)
{
cout<<"Enter same m*n matrixs.\n";
goto start;
}
cout<<"Enter for first matrix:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter for sencond matrix:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>m2[i][j];
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]+m2[i][j];
}
}
cout<<"The addition of matrix is:\n";
for(i=0;i<r1;i++)
{
cout<<endl;
for(j=0;j<c1;j++)
{
cout<<setw(5)<<add[i][j];
}
}
getch();
return 0;

}

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment