fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6. // declare itertives and matrices
  7. int i, j, k, rows0, cols0, rows1, cols1, matrix0[10][10], matrix1[10][10], matrix2[10][10];
  8. //printf("Number of Rows and columns shouldn't exceed 10\n");
  9. //printf("Enter number of rows and columns for first Matrix\n");
  10. //printf("Enter number of rows: ");
  11. //scanf("%d", &rows0);
  12. //printf("Enter number of columns: ");
  13. //scanf("%d", &cols0);
  14. rows0 = cols0 = 3;
  15.  
  16. //printf("Enter number of rows and columns for second Matrix\n");
  17. //printf("Enter number of rows: ");
  18. //scanf("%d", &rows1);
  19. //printf("Enter number of columns: ");
  20. //scanf("%d", &cols1);
  21. rows1 = cols1 = 3;
  22.  
  23. if (cols0!=rows1)
  24. {
  25. printf("Number of columns in first matrix should be equal to number of rows in second matrix\n");
  26. return 0;
  27. }
  28.  
  29. //printf("Input values into first Matrix\n");
  30.  
  31. //for(i = 0; i < rows0; i++)
  32. //{
  33. // printf("Input values in row %d with each value separated with space\n", i);
  34. // for(j=0; j< cols0; j++)
  35. // {
  36. // scanf("%d", &matrix0[i][j]);
  37. // }
  38. //}
  39. memcpy(matrix0[0], (int[]){ 1, 2, -1}, 3*sizeof (int));
  40. memcpy(matrix0[1], (int[]){ 3, 2, 0}, 3*sizeof (int));
  41. memcpy(matrix0[2], (int[]){-4, 0, 2}, 3*sizeof (int));
  42.  
  43. //printf("Input values into second Matrix\n");
  44.  
  45. //for(i = 0; i < rows1; i++)
  46. //{
  47. // printf("Input values in row %d with each value separated with space\n", i);
  48. // for(j=0; j< cols1; j++)
  49. // {
  50. // scanf("%d", &matrix1[i][j]);
  51. // }
  52. //}
  53. memcpy(matrix1[0], (int[]){ 3, 4, 2}, 3*sizeof (int));
  54. memcpy(matrix1[1], (int[]){ 0, 1, 0}, 3*sizeof (int));
  55. memcpy(matrix1[2], (int[]){-2, 0, 1}, 3*sizeof (int));
  56.  
  57. // Matrix multiplication
  58.  
  59. for (i=0; i<rows0; i++)
  60. {
  61. for (j=0; j< cols1; j++)
  62. {
  63. matrix2[i][j]=0;
  64. for(k=0;k<cols1;k++)
  65. {
  66. matrix2[i][j]+=(matrix0[i][k]*matrix1[k][j]);
  67. }
  68. }
  69. }
  70.  
  71. printf("Resultant matrix after matrix multiplication is\n");
  72. for(i=0; i < rows0; i++)
  73. {
  74. for(j=0; j<cols1; j++)
  75. {
  76. printf("%d ", matrix2[i][j]);
  77. }
  78. printf("\n");
  79. }
  80. }
Success #stdin #stdout 0s 5384KB
stdin
Standard input is empty
stdout
Resultant matrix after matrix multiplication is
5 6 1 
9 14 6 
-16 -16 -6