Skip to content

Instantly share code, notes, and snippets.

@MShirazAhmad
Last active February 2, 2020 16:33
Show Gist options
  • Save MShirazAhmad/0d576849df069819ac23a5dea71c9593 to your computer and use it in GitHub Desktop.
Save MShirazAhmad/0d576849df069819ac23a5dea71c9593 to your computer and use it in GitHub Desktop.
2x2 Matrices Multiplication (C++)
///2x2 Matrices Multiplication
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
cout<<"Enter rows and columns of first matrix:";
cin>>m>>n;
cout<<"Enter rows and columns of second matrix:";
cin>>p>>q;
if(n==p)
{
cout<<"\nEnter first matrix:\n";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
cin>>a[i][j];
}
cout<<"\nEnter second matrix:\n";
for(i=0;i<p;++i)
for(j=0;j<q;++j)
{cin>>b[i][j];}
cout<<"\nThe new matrix is:\n";
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
else
cout<<"\nSorry!!!! Matrix multiplication can’t be done";
getch();
}
output
Enter rows and columns of first matrix:2 2
Enter rows and columns of second matrix:2 2
Enter first matrix:
1 1
1 1
Enter second matrix:
2 2
2 2
The new matrix is:
4 4
4 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment