AbdulFathaah

matrix multiplication

Nov 13th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. /* program to read and display matrix multiplication */
  2.  
  3. #include<conio.h>
  4. #include<stdio.h>
  5. void main()
  6. {
  7. int m,n,p,q,a[5][5],b[5][5],c[5][5],i,j,k;
  8. clrscr();
  9. printf("Enter no of rows and columns of Array 1:\n");
  10. scanf("%d%d",&m,&n);
  11. printf("Enter no of rows and columns of Array 2:\n");
  12. scanf("%d%d",&p,&q);
  13. if(m==q && n==p)
  14. {
  15. for(i=0;i<m;i++)
  16. {
  17. printf("Enter Array 1 elements row %d\n",i+1);
  18. for(j=0;j<n;j++)
  19. {
  20. scanf("%d",&a[i][j]);
  21. }
  22. }
  23. for(i=0;i<p;i++)
  24. {
  25. printf("Enter Array 2 elements row %d\n",i+1);
  26. for(j=0;j<q;j++)
  27. {
  28. scanf("%d",&b[i][j]);
  29. }
  30. }
  31. printf("The Array 1 is:\n");
  32. for(i=0;i<m;i++)
  33. {
  34. for(j=0;j<n;j++)
  35. {
  36. printf(" %d",a[i][j]);
  37. }
  38. printf("\n");
  39. }
  40. printf("The Array 2 is:\n");
  41. for(i=0;i<p;i++)
  42. {
  43. for(j=0;j<q;j++)
  44. {
  45. printf(" %d",b[i][j]);
  46. }
  47. printf("\n");
  48. }
  49. for(i=0;i<m;i++)
  50. {
  51. for(j=0;j<n;j++)
  52. {
  53. c[i][j]=0;
  54. for(k=0;k<n;k++)
  55. {
  56. c[i][j]+=a[i][k]*b[k][j];
  57. }
  58.  
  59. }
  60. }
  61. printf("The Array Multiplied is:\n");
  62. for(i=0;i<m;i++)
  63. {
  64. for(j=0;j<n;j++)
  65. {
  66. printf(" %d",c[i][j]);
  67. }
  68. printf("\n");
  69. }
  70.  
  71. }
  72. else
  73. {
  74. printf("Array Multiplication NOT possible");
  75. }
  76. getch();
  77. }
  78. 
Advertisement
Add Comment
Please, Sign In to add comment