English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn how to multiply two matrices and display them using user-defined functions.
To understand this example, you should be familiar with the followingC Programming LanguageTopic:
The program requires the user to input the size of two matrices (rows and columns).
In order to multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second matrix
The following program requires the number of rows and columns of two matrices, until the above conditions are met.
Then, perform the multiplication of two matrices, and the result is displayed on the screen.
For this, we created three functions:
enterData() - Obtain matrix elements from the user.
multiplyMatrices() - 将两个矩阵相乘。
display() - 在乘法后显示结果矩阵。
#include <stdio.h> void enterData(int first[10], int second[10], int r1, int c1, int r2, int c2); void multiplyMatrices(int first[10], int second[10], int multResult[10], int r1, int c1, int r2, int c2); void display(int mult[][10], int r1, int c2); int main() { int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2; printf("输入第一个矩阵的行和列: "); scanf("%d %d", &r1, &c1); printf("输入第二个矩阵的行和列: "); scanf("%d %d", &r2, &c2); //进行输入直到第一个矩阵的列等于第二个矩阵的行 while (c1 != r2) { printf("错误! 再次输入行和列。\n"); printf("输入第一个矩阵的行和列: "); scanf("%d%d", &r1, &c1); printf("输入第二个矩阵的行和列: "); scanf("%d%d", &r2, &c2); } //取得矩阵数据的函数 enterData(first, second, r1, c1, r2, c2); //用于将两个矩阵相乘的函数。 multiplyMatrices(first, second, mult, r1, c1, r2, c2); //显示相乘后的合成矩阵的函数。 display(mult, r1, c2); return 0; } void enterData(int first[10], int second[10], int r1, int c1, int r2, int c2) { printf("\n输入矩阵元素 1:\n"); for (int i = 0; i < r1; ++i) { for (int j = 0; j < c1; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%d", &first[i][j]); } } printf("\n输入矩阵元素 2:\n"); for (int i = 0; i < r2; ++i) { for (int j = 0; j < c2; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%d", &second[i][j]); } } } void multiplyMatrices(int first[10], int second[10], int mult[][10], int r1, int c1, int r2, int c2) { // 矩阵的初始化元素多为0。 for (int i = 0; i < r1; ++i) { for (int j = 0; j < c2; ++j) { mult[i][j] = 0; } } // 第一矩阵和第二矩阵相乘并存储在mult中。 for (int i = 0; i < r1; ++i) { for (int j = 0; j < c2; ++j) { for (int k = 0; k < c1; ++k) { mult[i][j] += first[i][k] * second[k][j]; } } } } void display(int mult[][10], int r1, int c2) { printf("\n输出矩阵:\n"); for (int i = 0; i < r1; ++i) { for (int j = 0; j < c2; ++j) { printf("%d ", mult[i][j]); if (j == c2 - 1) printf("\n"); } } }
Output Result
Enter the Rows and Columns of the First Matrix: 2 3 Enter the Rows and Columns of the Second Matrix: 3 2 Enter Matrix Elements 1: Enter a11: 3 Enter a12: -2 Enter a13: 5 Enter a21: 3 Enter a22: 0 Enter a23: 4 Enter Matrix Elements 2: Enter b11: 2 Enter b12: 3 Enter b21: -9 Enter b22: 0 Enter b31: 0 Enter b32: 4 Output Matrix: 24 29 6 25