Skip to content

Commit e89665c

Browse files
authored
Merge pull request #451 from mirajkarsaurabh/mirajkarsaurabh-patch-1
Added Strassens Algorithm
2 parents 56a0471 + edc44fc commit e89665c

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Strassen’s Algorithm.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
using namespace std;
3+
void multiply(int[5][5], int[5][5], int, int, int);
4+
int display(int[5][5], int, int);
5+
int main()
6+
{
7+
8+
int a[5][5], b[5][5], r1, c1, r2, c2;
9+
cout<<"\n Enter rows for first matrix: ";
10+
cin>>r1;
11+
cout<<"\n Enter columns for second matrix: ";
12+
cin>>c1;
13+
14+
cout<<"\n Enter rows for first matrix: ";
15+
cin>>r2;
16+
cout<<"\n Enter columns for second matrix: ";
17+
cin>>c2;
18+
19+
// To check if columns of first matrix are equal to rows of second matrix
20+
21+
if (c1 != r2)
22+
return 0;
23+
24+
// Storing elements of first matrix.
25+
26+
cout<<"\n Enter elements of first matrix \n";
27+
28+
for(int i=0; i<r1; i++)
29+
{
30+
for(int j=0; j<c1; j++)
31+
cin>>a[i][j];
32+
33+
}
34+
// Storing elements of second matrix.
35+
cout<<"\n Enter elements of second matrix\n";
36+
37+
for(int i=0; i<r2; i++)
38+
{
39+
for(int j=0; j<c2; j++)
40+
cin>>b[i][j];
41+
}
42+
display(a,r1,c1);
43+
display(b,r2,c2);
44+
//calling the function to multiply a and b. passing number of rows
45+
//and columns in both of them
46+
multiply(a, b, r1, c2, c1);
47+
return 0;
48+
}
49+
50+
void multiply(int a[5][5], int b[5][5], int row, int col, int c1)
51+
{
52+
int c[5][5];
53+
//input 0 for all values of c, in order to remove
54+
//the garbage values assigned earlier
55+
for(int i=0; i<row; i++)
56+
{
57+
for(int j=0; j<col; j++)
58+
c[i][j]=0;
59+
}
60+
//we apply the same formula as above
61+
for(int i=0; i<row; i++)
62+
{
63+
for(int j=0; j<col; j++)
64+
{
65+
for(int k=0; k<c1; k++)//columns of first matrix || rows of second matrix
66+
c[i][j]+=a[i][k]*b[k][j];
67+
}
68+
}
69+
//to display matrix
70+
cout<<"\n Matrix c after matrix multiplication is:\n";
71+
display(c, row, col);
72+
}
73+
int display(int c[5][5], int row, int col)
74+
{
75+
cout<<"\n Matrix is:\n";
76+
for(int i=0; i<row; i++)
77+
{
78+
for(int j=0; j<col; j++)
79+
cout<<c[i][j]<<" ";
80+
cout<<"\n";
81+
}
82+
return 0;
83+
}

0 commit comments

Comments
 (0)