C++ program to add two matrix. Here I have given three programs to add two matrix like using function, using pointers, using operator overloading etc.
C++ to add two matrices
#include<iostream>
using namespace std;
int main()
{
int c[10][10],a[10][10],b[10][10],row,col;
cout<<"Enter row and column of matrix (max 10*10): ";
cin>>row>>col;
cout<<"Enter matrix a1 : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>a[i][j];
cout<<"Enter matrix a2 : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>b[i][j];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"Addition of two matrix : "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int c[10][10],a[10][10],b[10][10],row,col;
cout<<"Enter row and column of matrix (max 10*10): ";
cin>>row>>col;
cout<<"Enter matrix a1 : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>a[i][j];
cout<<"Enter matrix a2 : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>b[i][j];
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"Addition of two matrix : "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
return 0;
}
C++ to add two matrices using pointers
#include<iostream>
using namespace std;
int main()
{
int *p,*q,row,col;
cout<<"Enter row and column of matrix : ";
cin>>row>>col;
p=new int[row*col];
q=new int[row*col];
cout<<"Enter first matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>p[i*col+j];
cout<<"Enter Second matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>q[i*col+j];
cout<<"Sum of two matrix "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<p[i*col+j]+q[i*col+j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
int main()
{
int *p,*q,row,col;
cout<<"Enter row and column of matrix : ";
cin>>row>>col;
p=new int[row*col];
q=new int[row*col];
cout<<"Enter first matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>p[i*col+j];
cout<<"Enter Second matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>q[i*col+j];
cout<<"Sum of two matrix "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<p[i*col+j]+q[i*col+j]<<" ";
cout<<endl;
}
return 0;
}
C++ to add two matrices using functions
#include<iostream>
using namespace std;
void add(int (&)[10][10],int (&)[10][10],int,int);
int main()
{
int row,col,a[10][10],b[10][10];
cout<<"Enter rows and columns of matrices : ";
cin>>row>>col;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>b[i][j];
add(a,b,row,col);
cout<<"Addition of two matrices : "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
void add(int (&a)[10][10],int (&b)[10][10],int row,int col)
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j]+=b[i][j];
}
using namespace std;
void add(int (&)[10][10],int (&)[10][10],int,int);
int main()
{
int row,col,a[10][10],b[10][10];
cout<<"Enter rows and columns of matrices : ";
cin>>row>>col;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>b[i][j];
add(a,b,row,col);
cout<<"Addition of two matrices : "<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
void add(int (&a)[10][10],int (&b)[10][10],int row,int col)
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j]+=b[i][j];
}
C++ to add two matrices using operator overloading
#include<iostream>
using namespace std;
struct add
{
int a[3][3];
};
add operator+(add mat1,add mat2)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
mat1.a[i][j]+=mat2.a[i][j];
return mat1;
}
int main()
{
add mat1,mat2,mat3;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat1.a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat2.a[i][j];
mat3=mat1+mat2;
cout<<"Addition of two matrices : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<mat3.a[i][j]<<" ";
cout<<endl;
}
return 0;
}
using namespace std;
struct add
{
int a[3][3];
};
add operator+(add mat1,add mat2)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
mat1.a[i][j]+=mat2.a[i][j];
return mat1;
}
int main()
{
add mat1,mat2,mat3;
cout<<"Enter elements of first matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat1.a[i][j];
cout<<"Enter elements of second matrix : "<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>mat2.a[i][j];
mat3=mat1+mat2;
cout<<"Addition of two matrices : "<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<mat3.a[i][j]<<" ";
cout<<endl;
}
return 0;
}
No comments:
Post a Comment