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

在此示例中,您将学习将两个矩阵相乘并使用用户定义的函数进行显示。

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


该程序要求用户输入矩阵的大小(行和列)。

然后,它要求用户输入两个矩阵的元素,最后将两个矩阵相乘并显示结果。

要执行此任务,需要执行三个步骤:

  1. 从用户那里获取矩阵元素
  2. 乘以两个矩阵
  3. 在乘法后显示结果矩阵

示例:通过将矩阵传递给函数来相乘

  1. #include <iostream>
  2. using namespace std;
  3. void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
  4. void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
  5. void display(int mult[][10], int rowFirst, int columnSecond);
  6. int main()
  7. {
  8. int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
  9. cout << "Enter rows and column for first matrix: ";
  10. cin >> rowFirst >> columnFirst;
  11. cout << "Enter rows and column for second matrix: ";
  12. cin >> rowSecond >> columnSecond;
  13. // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
  14. while (columnFirst != rowSecond)
  15. {
  16. cout << "Error! column of first matrix not equal to row of second." << endl;
  17. cout << "Enter rows and column for first matrix: ";
  18. cin >> rowFirst >> columnFirst;
  19. cout << "Enter rows and column for second matrix: ";
  20. cin >> rowSecond >> columnSecond;
  21. }
  22. // Function to take matrices data
  23. enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
  24. // Function to multiply two matrices.
  25. multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);
  26. // Function to display resultant matrix after multiplication.
  27. display(mult, rowFirst, columnSecond);
  28. return 0;
  29. }
  30. void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
  31. {
  32. int i, j;
  33. cout << endl << "Enter elements of matrix 1:" << endl;
  34. for(i = 0; i < rowFirst; ++i)
  35. {
  36. for(j = 0; j < columnFirst; ++j)
  37. {
  38. cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
  39. cin >> firstMatrix[i][j];
  40. }
  41. }
  42. cout << endl << "Enter elements of matrix 2:" << endl;
  43. for(i = 0; i < rowSecond; ++i)
  44. {
  45. for(j = 0; j < columnSecond; ++j)
  46. {
  47. cout << "Enter elements b" << i + 1 << j + 1 << ": ";
  48. cin >> secondMatrix[i][j];
  49. }
  50. }
  51. }
  52. void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
  53. {
  54. int i, j, k;
  55. // Initializing elements of matrix mult to 0.
  56. for(i = 0; i < rowFirst; ++i)
  57. {
  58. for(j = 0; j < columnSecond; ++j)
  59. {
  60. mult[i][j] = 0;
  61. }
  62. }
  63. // Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
  64. for(i = 0; i < rowFirst; ++i)
  65. {
  66. for(j = 0; j < columnSecond; ++j)
  67. {
  68. for(k=0; k<columnFirst; ++k)
  69. {
  70. mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
  71. }
  72. }
  73. }
  74. }
  75. void display(int mult[][10], int rowFirst, int columnSecond)
  76. {
  77. int i, j;
  78. cout << "Output Matrix:" << endl;
  79. for(i = 0; i < rowFirst; ++i)
  80. {
  81. for(j = 0; j < columnSecond; ++j)
  82. {
  83. cout << mult[i][j] << " ";
  84. if(j == columnSecond - 1)
  85. cout << endl << endl;
  86. }
  87. }
  88. }

输出

  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