Write a program for addition of two matrices
OR
Write a program to add 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\nsum 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 added";
}
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 1st 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
sum of two matrices is:=
3 7
3 6
OR
Write a program to add 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\nsum 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 added";
}
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 1st 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
sum of two matrices is:=
3 7
3 6
No comments:
Post a Comment