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

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

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


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

然后,它要求用户输入这些矩阵的元素,最后添加并显示结果。

要执行此任务,需要执行三个函数:

  1. enterData()从用户获取矩阵元素
  2. multiplyMatrices()将两个矩阵相乘
  3. display()在乘法后显示结果矩阵

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

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

输出

  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