原文: https://www.programiz.com/c-programming/examples/matrix-multiplication-function
在此示例中,您将学习将两个矩阵相乘并使用用户定义的函数进行显示。
要理解此示例,您应该了解以下 C 编程主题:
该程序要求用户输入矩阵的大小(行和列)。
然后,它要求用户输入这些矩阵的元素,最后添加并显示结果。
要执行此任务,需要执行三个函数:
enterData()从用户获取矩阵元素multiplyMatrices()将两个矩阵相乘display()在乘法后显示结果矩阵
示例:通过将矩阵传递给函数来相乘
#include <stdio.h>void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);void display(int mult[][10], int rowFirst, int columnSecond);int main(){int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;printf("Enter rows and column for first matrix: ");scanf("%d %d", &rowFirst, &columnFirst);printf("Enter rows and column for second matrix: ");scanf("%d %d", &rowSecond, &columnSecond);// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.while (columnFirst != rowSecond){printf("Error! column of first matrix not equal to row of second.\n");printf("Enter rows and column for first matrix: ");scanf("%d%d", &rowFirst, &columnFirst);printf("Enter rows and column for second matrix: ");scanf("%d%d", &rowSecond, &columnSecond);}// Function to take matrices dataenterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);// Function to multiply two matrices.multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);// Function to display resultant matrix after multiplication.display(mult, rowFirst, columnSecond);return 0;}void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond){int i, j;printf("\nEnter elements of matrix 1:\n");for(i = 0; i < rowFirst; ++i){for(j = 0; j < columnFirst; ++j){printf("Enter elements a%d%d: ", i + 1, j + 1);scanf("%d", &firstMatrix[i][j]);}}printf("\nEnter elements of matrix 2:\n");for(i = 0; i < rowSecond; ++i){for(j = 0; j < columnSecond; ++j){printf("Enter elements b%d%d: ", i + 1, j + 1);scanf("%d", &secondMatrix[i][j]);}}}void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond){int i, j, k;// Initializing elements of matrix mult to 0.for(i = 0; i < rowFirst; ++i){for(j = 0; j < columnSecond; ++j){mult[i][j] = 0;}}// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.for(i = 0; i < rowFirst; ++i){for(j = 0; j < columnSecond; ++j){for(k=0; k<columnFirst; ++k){mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];}}}}void display(int mult[][10], int rowFirst, int columnSecond){int i, j;printf("\nOutput Matrix:\n");for(i = 0; i < rowFirst; ++i){for(j = 0; j < columnSecond; ++j){printf("%d ", mult[i][j]);if(j == columnSecond - 1)printf("\n\n");}}}
输出
Enter rows and column for first matrix: 32Enter rows and column for second matrix: 32Error! column of first matrix not equal to row of second.Enter rows and column for first matrix: 23Enter rows and column for second matrix: 32Enter elements of matrix 1:Enter elements a11: 3Enter elements a12: -2Enter elements a13: 5Enter elements a21: 3Enter elements a22: 0Enter elements a23: 4Enter elements of matrix 2:Enter elements b11: 2Enter elements b12: 3Enter elements b21: -9Enter elements b22: 0Enter elements b31: 0Enter elements b32: 4Output Matrix:24 296 25
