In: Computer Science
Program :
#include <iostream>
using namespace std;
void Read_a_Matrix();
void Add_two_matrices();
void Subtract_two_matrices();
void multiplication_two_matrices();
void display_a_matrice();
int main()
{
Read_a_Matrix();
cout<<"\n====== Add two matrices=======\n";
Add_two_matrices();
cout<<"\n====== Subtraction of two matrices=======\n";
Subtract_two_matrices();
cout<<"\n====== Multiplication of two
matrices=======\n";
multiplication_two_matrices();
cout<<"\n=============== Display_a_matrice======\n";
display_a_matrice();
return 0;
}
void Read_a_Matrix()
{
int i,j, r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter column size:";
cin>>c;
int m1[r][c];
cout<<"Enter "<<r<<"*"<< c <<" values
:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>m1[i][j];
}
}
void Add_two_matrices()
{
int r1,c1,i,j,r2,c2;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int add[r1][c1];
int m1[r1][c1];
cout<<"Enter "<<r1<<"*"<< c1 <<"
values of matrix one:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}
cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
cout<<"Enter "<<r2<<"*"<< c2 <<"
values of matrix2 :\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<<"\n Addtion of two matrixces is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<add[i][j]<<" ";
cout<<"\n";
}
}
void Subtract_two_matrices()
{
int r1,c1,i,j,r2,c2;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int m1[r1][c1];
int sub[r1][c1];
cout<<"Enter "<<r1<<"*"<< c1 <<"
values of matrix one:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}
cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
cout<<"Enter "<<r2<<"*"<< c2 <<"
values of matrix two:\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++)
{
sub[i][j]=m1[i][j]-m2[i][j];
}
}
cout<<"\n Subtraction of two matrixces is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<sub[i][j]<<" ";
cout<<"\n";
}
}
void multiplication_two_matrices()
{
int r1,c1,i,j,r2,c2,sum=0,k;
cout<<"Enter 1st matrix row size:";
cin>>r1;
cout<<"Enter 1st matrix column size:";
cin>>c1;
int m1[r1][c1];
cout<<"\nEnter "<<r1<<"*"<< c1 <<"
values of matrix one :\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cin>>m1[i][j];
}
cout<<"Enter 2nd matrix row size:";
cin>>r2;
cout<<"Enter 2nd matrix column size:";
cin>>c2;
int m2[r2][c2];
int mul[r1][c2];
cout<<"\nEnter "<<r2<<"*"<< c2 <<"
values of matrix two:\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<c2;j++)
{
for(k=0;k<c1;k++)
{
sum = sum + m1[i][k]*m2[k][j];
}
mul[i][j] = sum;
sum = 0;
}
}
cout<<"\n multiplication two matrices of two matrixces
is:\n";
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
cout<<mul[i][j]<<" ";
cout<<"\n";
}
}
void display_a_matrice()
{
int i,j, r,c;
cout<<"Enter row size:";
cin>>r;
cout<<"Enter column size:";
cin>>c;
int m[r][c];
cout<<"Enter "<<r<<"*"<< c <<" values
:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>m[i][j];
}
cout<<"\ndisplay a matrice:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<m[i][j]<<" ";
cout<<"\n";
}
}