Addition of matrices
The addition of two matrices of the same order is obtained by the addition of corresponding elements. Only the matrices of the same order can be added and the sum also has the same order (as that of the matrices which are added). Example:
Write a program in C++ to input two matrices of order m*n and display its sum.
In this program, we input two matrices by using nested for loop and store the matrices with the help of two-dimensional array. Then we perform the addition of their corresponding elements. Finally, we display the sum of two matrices using another nested for loop.
A two-dimensional array is those array which contains a pair of the square bracket. A two dimensional is declared as:
datatype a[m][n];
Where m is the number of rows and n is the number of columns.
The array a[2][2] contains four elements which are: a[0][0] , a[0][1], a[1][0], a[1][1].
Program
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n;
clrscr();
cout<<"Enter the number of rows of the matrix=";
cin>>m;
cout<<"\nEnter the number of colomns of the matrix=";
cin>>n;
if(m>20||n>20)
{
cout<<"\nInvalid size of the matrix";
goto x;
}
cout<<"\nEnter values for first matrix:\n";
for (i=0;i<m;i++) //Input of first matrix
{
for(j=0;j<n;j++)
{
cout<<"Enter the element a["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];
}
}
cout<<"\nEnter values for second matrix:\n";
for (i=0;i<m;i++) //Input of second matrix
{
for(j=0;j<n;j++)
{
cout<<"Enter the element b["<<i<<"]["<<j<<"]= ";
cin>>b[i][j];
}
}
for (i=0;i<m;i++) //Summation of two matrices
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\nYour result is:\n";
for (i=0;i<m;i++) //Printing of sum
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
x:getch();
}
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n;
clrscr();
cout<<"Enter the number of rows of the matrix=";
cin>>m;
cout<<"\nEnter the number of colomns of the matrix=";
cin>>n;
if(m>20||n>20)
{
cout<<"\nInvalid size of the matrix";
goto x;
}
cout<<"\nEnter values for first matrix:\n";
for (i=0;i<m;i++) //Input of first matrix
{
for(j=0;j<n;j++)
{
cout<<"Enter the element a["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];
}
}
cout<<"\nEnter values for second matrix:\n";
for (i=0;i<m;i++) //Input of second matrix
{
for(j=0;j<n;j++)
{
cout<<"Enter the element b["<<i<<"]["<<j<<"]= ";
cin>>b[i][j];
}
}
for (i=0;i<m;i++) //Summation of two matrices
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\nYour result is:\n";
for (i=0;i<m;i++) //Printing of sum
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
x:getch();
}
Output
MATRIX ADDITION |
Explanation
In the program, we performed the addition of two matrices of order 2*2. The input is done using
nested for loop as it assign the value of the elements (a[0][0], a[0][1], a[1][0], a[1][1] ) individually.The addition is performed as follows:
nested for loop as it assign the value of the elements (a[0][0], a[0][1], a[1][0], a[1][1] ) individually.The addition is performed as follows:
i(<2) j(<2) c[i][j]=a[i][j]+b[i][j]
0 0 c[0][0]=a[0][0]+b[0][0]=4+4=8
1 c[0][1]=a[0][1]+b[0][1]=3+3=6
1 0 c[1][0]=a[1][0]+b[1][0]=2+5=7
1 c[1][1]=a[1][1]+b[1][1]=6+9=15.
In this way, we obtain the addition of two matrices and it is displayed using nested for loop.
No comments:
Post a Comment