Write a Class Template by the name matrix and perform the following operations:
1. Sum of 2 Matrices
2. Product of 2 Matrices
3. Sum of each row of matrix and display them
4. Sum of each column of matrix and display them
#include <iostream>
using namespace std;
template<class T> //Declaring Template class
class matrix
{
T a[30][30],b[30][30];
int i,j,m,n;
public:
matrix() //Constructor to Input Matrix
{
cout<<"Enter No.of Rows and Columns of Matrices: ";
cin>>m>>n;
cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>a[i][j];
}
cout<<"Enter "<<(m*n)<<" Elements of Matrix 2: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>b[i][j];
}
}
void display()
{
cout<<"\nMatrix 1:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
cout<<"\nMatrix 2:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
}
void sum() //Sum of Matrices
{
T c[30][30];
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 Matrices is:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
}
}
void rowsum() //Display Sum of Rows
{
T sumr=0.0;
cout<<"\n\nMatrix 1:";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
sumr+=a[i][j];
cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
sumr=0;
}
cout<<"\n\nMatrix 2:";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
sumr+=b[i][j];
cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
sumr=0;
}
}
void colsum() //Display Sum of Columns
{
T sumc=0.0;
cout<<"\n\nMatrix 1:";
for(j=0; j<n; j++)
{
for(i=0; i<m; i++)
sumc+=a[i][j];
cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
sumc=0;
}
cout<<"\n\nMatrix 2:";
for(j=0; j<n; j++)
{
for(i=0; i<m; i++)
sumc+=b[i][j];
cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
sumc=0;
}
}
void product() //Function to find product
{
T prod[30][30]= {0.0}; //Matrix to store Product
int p,q,k;
cout<<"\n\nMatrix Product:-\n";
cout<<"Enter Rows and Columns of Matrix 1: ";
cin>>m>>n;
cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>a[i][j];
}
cout<<"Enter Rows and Columns of Matrix 2: ";
cin>>p>>q;
if(n!=p)
cout<<"\nMatrices Cannot be Multiplied!!";
else
{
cout<<"Enter "<<(p*q)<<" Elements of Matrix 2: ";
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
cin>>b[i][j];
}
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
prod[i][j]=0.0;
for(k=0; k<n; k++)
prod[i][j]+=a[i][k]*b[k][j];
}
}
cout<<"\nProduct of Matrices is:\n";
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
cout<<prod[i][j]<<"\t";
cout<<"\n";
}
}
}
};
int main()
{
matrix<int> m; //Declaring Template
m.display();
m.sum();
m.rowsum();
m.colsum();
m.product();
return 0;
}
1. Sum of 2 Matrices
2. Product of 2 Matrices
3. Sum of each row of matrix and display them
4. Sum of each column of matrix and display them
#include <iostream>
using namespace std;
template<class T> //Declaring Template class
class matrix
{
T a[30][30],b[30][30];
int i,j,m,n;
public:
matrix() //Constructor to Input Matrix
{
cout<<"Enter No.of Rows and Columns of Matrices: ";
cin>>m>>n;
cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>a[i][j];
}
cout<<"Enter "<<(m*n)<<" Elements of Matrix 2: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>b[i][j];
}
}
void display()
{
cout<<"\nMatrix 1:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
cout<<"\nMatrix 2:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
}
void sum() //Sum of Matrices
{
T c[30][30];
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 Matrices is:\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cout<<c[i][j]<<"\t";
cout<<"\n";
}
}
void rowsum() //Display Sum of Rows
{
T sumr=0.0;
cout<<"\n\nMatrix 1:";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
sumr+=a[i][j];
cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
sumr=0;
}
cout<<"\n\nMatrix 2:";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
sumr+=b[i][j];
cout<<"\nSum of Row "<<(i+1)<<" is "<<sumr;
sumr=0;
}
}
void colsum() //Display Sum of Columns
{
T sumc=0.0;
cout<<"\n\nMatrix 1:";
for(j=0; j<n; j++)
{
for(i=0; i<m; i++)
sumc+=a[i][j];
cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
sumc=0;
}
cout<<"\n\nMatrix 2:";
for(j=0; j<n; j++)
{
for(i=0; i<m; i++)
sumc+=b[i][j];
cout<<"\nSum of Column "<<(j+1)<<" is "<<sumc;
sumc=0;
}
}
void product() //Function to find product
{
T prod[30][30]= {0.0}; //Matrix to store Product
int p,q,k;
cout<<"\n\nMatrix Product:-\n";
cout<<"Enter Rows and Columns of Matrix 1: ";
cin>>m>>n;
cout<<"Enter "<<(m*n)<<" Elements of Matrix 1: ";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
cin>>a[i][j];
}
cout<<"Enter Rows and Columns of Matrix 2: ";
cin>>p>>q;
if(n!=p)
cout<<"\nMatrices Cannot be Multiplied!!";
else
{
cout<<"Enter "<<(p*q)<<" Elements of Matrix 2: ";
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
cin>>b[i][j];
}
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
prod[i][j]=0.0;
for(k=0; k<n; k++)
prod[i][j]+=a[i][k]*b[k][j];
}
}
cout<<"\nProduct of Matrices is:\n";
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
cout<<prod[i][j]<<"\t";
cout<<"\n";
}
}
}
};
int main()
{
matrix<int> m; //Declaring Template
m.display();
m.sum();
m.rowsum();
m.colsum();
m.product();
return 0;
}