Answer to Question #231051 in C++ for Sky

Question #231051
Write strassen matrix multiplication for nxn matrix in c++.
1
Expert's answer
2021-08-31T00:53:20-0400
#include<iostream>
using namespace std;
int main(){
  int matrix_first[2][2], matrix_second[2][2], matrix_third[2][2], x, y;
  int element_m1, element_m2, element_m3, element_m4 , element_m5, element_m6, element_m7;
 
  cout<<"Enter 4 elements of First matrix: \n";
  for(x = 0;x < 2; x++)
      for(y = 0;y < 2; y++)
        cin>>matrix_first[x][y];
           
 
  cout<<"Enter 4 elements of  second matrix: \n";
  for(x = 0; x < 2; x++)
      for(y = 0;y < 2; y++)
           cin>>matrix_second[x][y];
 
 cout<<"\nFirst matrix: \n";
  for(x = 0; x < 2; x++){
      cout<<"\n";
      for(y = 0; y< 2; y++)
           cout<<matrix_first[x][y];
  }
 
  cout<<"\nSecond matrix:\n";
  for(x = 0;x < 2; x++){
      cout<<"\n";
      for(y = 0;y < 2; y++)
          cout<<matrix_second[x][y];
  }
 
  element_m1= (matrix_first[0][0] + matrix_first[1][1]) * (matrix_second[0][0] + matrix_second[1][1]);
  element_m2= (matrix_first[1][0] + matrix_first[1][1]) * matrix_second[0][0];
  element_m3= matrix_first[0][0] * (matrix_second[0][1] - matrix_second[1][1]);
  element_m4= matrix_first[1][1] * (matrix_second[1][0] - matrix_second[0][0]);
  element_m5= (matrix_first[0][0] + matrix_first[0][1]) * matrix_second[1][1];
  element_m6= (matrix_first[1][0] - matrix_first[0][0]) * (matrix_second[0][0]+matrix_second[0][1]);
  element_m7= (matrix_first[0][1] - matrix_first[1][1]) * (matrix_second[1][0]+matrix_second[1][1]);
 
  matrix_third[0][0] = element_m1 + element_m4- element_m5 + element_m7;
  matrix_third[0][1] = element_m3 + element_m5;
  matrix_third[1][0] = element_m2 + element_m4;
  matrix_third[1][1] = element_m1 - element_m2 + element_m3 + element_m6;
 
  cout<<"\nProduct Matrix:  \n";
   for(x = 0; x < 2 ; x++){
      cout<<"\n";
      for(y = 0;y < 2; y++)
           cout<< matrix_third[x][y]<<endl;
   }
 
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS