fork download
  1. // Let's print out matrix one and matrix 2.
  2.  
  3. // Multiplication My Version
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. void print_matrix(int M[10][10], int m, int n)
  9. /**
  10.  Print out a square matrix of size s, max size 10.
  11.  */
  12. {
  13. for (int i = 0; i < m; ++i)
  14. {
  15. for (int j = 0; j < n; ++j)
  16. {
  17. cout << M[i][j] << "\t";
  18. }
  19. cout << endl;
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. int m, n, c, d, o, p, first[10][10], second[10][10], mult[10][10];
  26.  
  27. cout << "Enter the number of rows and columns of matrix 1 ";
  28. cin >> m >> n;
  29. cout << "Enter the number of rows and columns of matrix 2 ";
  30. cin >> o >> p;
  31.  
  32. cout << "Enter the elements of first matrix\n";
  33.  
  34. for ( c = 0 ; c < m ; c++ )
  35. {
  36. for ( d = 0 ; d < n ; d++ )
  37. {
  38. cin >> first[c][d];
  39. }
  40. }
  41.  
  42. cout << "Enter the elements of second matrix\n";
  43.  
  44. for ( c = 0 ; c < o ;c++ )
  45. {
  46. for ( d = 0 ; d < p ; d++ )
  47. {
  48. cin >> second[c][d];
  49. }
  50. }
  51. cout << "First Matrix:" << endl;
  52. print_matrix(first, m, n);
  53. cout << endl << "Second Matrix:" << endl;
  54. print_matrix(second, o, p);
  55. //mult
  56. if(n==o)
  57. {
  58.  
  59. for ( c = 0 ; c < m ; c++ )
  60. {
  61.  
  62. for ( d = 0 ; d < p ; d++ )
  63. {
  64. mult[c][d]=0;
  65.  
  66. for(int k=0;k<n;k++)
  67. {
  68.  
  69. mult[c][d] += first[c][k] * second[k][d];
  70. }
  71. }
  72. }
  73. }
  74. else {
  75. cout<<"Multiplication not possible";
  76. }
  77. cout << "Product of entered matrices:-\n";
  78.  
  79. print_matrix(mult, m, o);
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 2732KB
stdin
2
2
2
2
3
3
3
3
1
0
0
1
stdout
Enter the number of rows and columns of matrix 1 Enter the number of rows and columns of matrix 2 Enter the elements of first matrix
Enter the elements of second matrix
First Matrix:
3	3	
3	3	

Second Matrix:
1	0	
0	1	
Product of entered matrices:-
3	3	
3	3