原文: https://www.programiz.com/cpp-programming/examples/matrix-multiplication

该程序分别采用两个阶为r1 * c1r2 * c2的矩阵。 然后,程序将这两个矩阵相乘(如果可能),并将其显示在屏幕上。

要理解此示例,您应该了解以下 C++ 编程主题:


若要将两个矩阵相乘,第一个矩阵的列数应等于第二个矩阵的行数。 该程序显示错误,直到第一矩阵的列数等于第二矩阵的行数。

示例:不使用函数将两个矩阵相乘

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
  6. cout << "Enter rows and columns for first matrix: ";
  7. cin >> r1 >> c1;
  8. cout << "Enter rows and columns for second matrix: ";
  9. cin >> r2 >> c2;
  10. // If column of first matrix in not equal to row of second matrix,
  11. // ask the user to enter the size of matrix again.
  12. while (c1!=r2)
  13. {
  14. cout << "Error! column of first matrix not equal to row of second.";
  15. cout << "Enter rows and columns for first matrix: ";
  16. cin >> r1 >> c1;
  17. cout << "Enter rows and columns for second matrix: ";
  18. cin >> r2 >> c2;
  19. }
  20. // Storing elements of first matrix.
  21. cout << endl << "Enter elements of matrix 1:" << endl;
  22. for(i = 0; i < r1; ++i)
  23. for(j = 0; j < c1; ++j)
  24. {
  25. cout << "Enter element a" << i + 1 << j + 1 << " : ";
  26. cin >> a[i][j];
  27. }
  28. // Storing elements of second matrix.
  29. cout << endl << "Enter elements of matrix 2:" << endl;
  30. for(i = 0; i < r2; ++i)
  31. for(j = 0; j < c2; ++j)
  32. {
  33. cout << "Enter element b" << i + 1 << j + 1 << " : ";
  34. cin >> b[i][j];
  35. }
  36. // Initializing elements of matrix mult to 0.
  37. for(i = 0; i < r1; ++i)
  38. for(j = 0; j < c2; ++j)
  39. {
  40. mult[i][j]=0;
  41. }
  42. // Multiplying matrix a and b and storing in array mult.
  43. for(i = 0; i < r1; ++i)
  44. for(j = 0; j < c2; ++j)
  45. for(k = 0; k < c1; ++k)
  46. {
  47. mult[i][j] += a[i][k] * b[k][j];
  48. }
  49. // Displaying the multiplication of two matrix.
  50. cout << endl << "Output Matrix: " << endl;
  51. for(i = 0; i < r1; ++i)
  52. for(j = 0; j < c2; ++j)
  53. {
  54. cout << " " << mult[i][j];
  55. if(j == c2-1)
  56. cout << endl;
  57. }
  58. return 0;
  59. }

输出

  1. Enter rows and column for first matrix: 3
  2. 2
  3. Enter rows and column for second matrix: 3
  4. 2
  5. Error! column of first matrix not equal to row of second.
  6. Enter rows and column for first matrix: 2
  7. 3
  8. Enter rows and column for second matrix: 3
  9. 2
  10. Enter elements of matrix 1:
  11. Enter elements a11: 3
  12. Enter elements a12: -2
  13. Enter elements a13: 5
  14. Enter elements a21: 3
  15. Enter elements a22: 0
  16. Enter elements a23: 4
  17. Enter elements of matrix 2:
  18. Enter elements b11: 2
  19. Enter elements b12: 3
  20. Enter elements b21: -9
  21. Enter elements b22: 0
  22. Enter elements b31: 0
  23. Enter elements b32: 4
  24. Output Matrix:
  25. 24 29
  26. 6 25

在该程序中,首先要求用户输入两个矩阵的大小。

为了乘法,第一矩阵的列应等于第二矩阵的行。 如果不满足此条件,则使用while循环再次询问矩阵的大小。

然后,要求用户输入两个矩阵,最后计算并显示两个矩阵的输出。

由于该程序冗长且难以调试,因此最好通过将其传递给函数来解决该程序。

访问此页面以了解通过将数组传递给函数来乘以矩阵的信息