Answer to Question #230628 in C for sky

Question #230628

write a program for Strassen’s matrix multiplication algorithm using divide and conquer approach in c language.


1
Expert's answer
2021-08-29T00:57:16-0400


//Using Strassen's algorithm to multiply  two matrices in C 


#include<stdio.h>
int main(){
  int matrix_a[2][2], matrix_b[2][2], matrix_c[2][2], x, y;
  int answer_m1, answer_m2, answer_m3, answer_m4 , answer_m5, answer_m6, answer_m7;
 
  printf("Enter 4 items of the first matrix: ");
  for(x = 0;x < 2; x++)
      for(y = 0;y < 2; y++)
       
           scanf("%d", &matrix_a[x][y]);
 
  printf("Enter 4 items of the second matrix: ");
  for(x = 0; x < 2; x++)
      for(y = 0;y < 2; y++)
           scanf("%d", &matrix_b[x][y]);
 
  printf("\nFirst matrix: \n");
  for(x = 0; x < 2; x++){
      printf("\n");
      for(y = 0; y< 2; y++)
           printf("%d\t", matrix_a[x][y]);
  }
 
  printf("\nSecond matrix:\n");
  for(x = 0;x < 2; x++){
      printf("\n");
      for(y = 0;y < 2; y++)
           printf("%d\t", matrix_b[x][y]);
  }
 
  answer_m1= (matrix_a[0][0] + matrix_a[1][1]) * (matrix_b[0][0] + matrix_b[1][1]);
  answer_m2= (matrix_a[1][0] + matrix_a[1][1]) * matrix_b[0][0];
  answer_m3= matrix_a[0][0] * (matrix_b[0][1] - matrix_b[1][1]);
  answer_m4= matrix_a[1][1] * (matrix_b[1][0] - matrix_b[0][0]);
  answer_m5= (matrix_a[0][0] + matrix_a[0][1]) * matrix_b[1][1];
  answer_m6= (matrix_a[1][0] - matrix_a[0][0]) * (matrix_b[0][0]+matrix_b[0][1]);
  answer_m7= (matrix_a[0][1] - matrix_a[1][1]) * (matrix_b[1][0]+matrix_b[1][1]);
 
  matrix_c[0][0] = answer_m1 + answer_m4- answer_m5 + answer_m7;
  matrix_c[0][1] = answer_m3 + answer_m5;
  matrix_c[1][0] = answer_m2 + answer_m4;
  matrix_c[1][1] = answer_m1 - answer_m2 + answer_m3 + answer_m6;
 
   printf("\nThe Product Matrix is:  \n");
   for(x = 0; x < 2 ; x++){
      printf("\n");
      for(y = 0;y < 2; y++)
           printf("%d\t", matrix_c[x][y]);
   }
 
   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