Wednesday, April 3, 2024

Matrix multiplication in C

 


Matrix multiplication in C: We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row number, column number, first matrix elements and second matrix elements. Then we are performing multiplication on the matrices entered by the user.

In matrix multiplication first matrix one row element is multiplied by second matrix all column elements.

Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by the figure given below:

matrix multiplication program in c

Let's see the program of matrix multiplication in C.

  1. #include<stdio.h>    
  2. #include<stdlib.h>  
  3. int main(){  
  4. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    
  5. system("cls");  
  6. printf("enter the number of row=");    
  7. scanf("%d",&r);    
  8. printf("enter the number of column=");    
  9. scanf("%d",&c);    
  10. printf("enter the first matrix element=\n");    
  11. for(i=0;i<r;i++)    
  12. {    
  13. for(j=0;j<c;j++)    
  14. {    
  15. scanf("%d",&a[i][j]);    
  16. }    
  17. }    
  18. printf("enter the second matrix element=\n");    
  19. for(i=0;i<r;i++)    
  20. {    
  21. for(j=0;j<c;j++)    
  22. {    
  23. scanf("%d",&b[i][j]);    
  24. }    
  25. }    
  26.     
  27. printf("multiply of the matrix=\n");    
  28. for(i=0;i<r;i++)    
  29. {    
  30. for(j=0;j<c;j++)    
  31. {    
  32. mul[i][j]=0;    
  33. for(k=0;k<c;k++)    
  34. {    
  35. mul[i][j]+=a[i][k]*b[k][j];    
  36. }    
  37. }    
  38. }    
  39. //for printing result    
  40. for(i=0;i<r;i++)    
  41. {    
  42. for(j=0;j<c;j++)    
  43. {    
  44. printf("%d\t",mul[i][j]);    
  45. }    
  46. printf("\n");    
  47. }    
  48. return 0;  
  49. }  

Output:

enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure given below:

matrix multiplication in c

---------------------------------------------------




2×2 Matrix Multiplication

Let’s consider a simple 2 × 2 matrix multiplication

=[3749]  =[6258]

Now each of the elements of product matrix AB can be calculated as follows:

  • AB11 = 3 × 6 + 7 ×5 = 53
  • AB12 = 3 × 2 + 7 × 8 = 62
  • AB21 = 4 × 6 + 9 × 5 = 69
  • AB22 = 4 × 2 + 9 × 8 = 80

Therefore,

=[53626980]

3×3 Matrix Multiplication

To understand the multiplication of two 3 × 3 matrices, let us consider two 3 × 3 matrices A and B.

=[1284317149810], =[519361597816]

Each element of the Product matrix AB can be calculated as follows:

  • AB11 = 12×5 + 8×6 + 4×7 = 136
  • AB12 = 12×19 + 8×15 + 4×8 = 380
  • AB13 = 12×3 + 8×9+4×16 = 172
  • AB21 = 3×5 + 17×6 + 14×7 = 215
  • AB22 = 3×19 + 17×15 + 14×8 = 424
  • AB23 = 3×3 + 17×9 + 14×16 = 386
  • AB31 = 9×5 + 8×6 + 10×7 = 163
  • AB32 = 9×19 + 8×15 + 10×8 = 371
  • AB33 = 9×3 + 8×9 + 10×16 = 259

Therefore,


No comments:

Post a Comment

Matrix multiplication in C

  Matrix multiplication  in C: We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from the user for row num...