C++ Program to multiply matrix using Strassen's
Multiplication method. This program calculates the multiplication of 2 matrices by Strassen's
Multiplication method. We define a 3 arrays : 'a' , 'b' & 'c' , all
of int type. All 3 are input by the user. The number of rows &
columns are made fix to 2. Then the multiplication is calculated by
using strassen's method and the new multiplied matrix is printed on the
screen.
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{int a[2][2],b[2][2],c[2][2];
int m1,m2,m3,m4,m5,m6,m7,i,j;
clrscr();
cout<<"Matrix Multiplication Strassrn's method by-Tarun Rawat\n";
cout<<"Enter the elements of 2x2 Matrix 1:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the elements of 2x2 Matrix 2:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}
clrscr();
cout<<"\nFirst matrix is:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"\nSecond matrix is\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<b[i][j];
}
cout<<"\n";
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
cout<<"\nProduct of both is:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j];
}
cout<<"\n";
}
getch();
}
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{int a[2][2],b[2][2],c[2][2];
int m1,m2,m3,m4,m5,m6,m7,i,j;
clrscr();
cout<<"Matrix Multiplication Strassrn's method by-Tarun Rawat\n";
cout<<"Enter the elements of 2x2 Matrix 1:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the elements of 2x2 Matrix 2:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}
clrscr();
cout<<"\nFirst matrix is:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
cout<<"\nSecond matrix is\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<b[i][j];
}
cout<<"\n";
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
cout<<"\nProduct of both is:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j];
}
cout<<"\n";
}
getch();
}
output to dal
ReplyDelete