跳到主要内容

C++ 编程:通过函数传递矩阵来乘两个矩阵

为了理解这个示例,你应该具备以下 C++ 编程 主题的知识:

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

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

为了执行这个任务,我们制作了三个函数:

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

示例:通过向函数传递矩阵来实现矩阵相乘

#include <iostream>
using namespace std;

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;

cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;

cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;

// 如果第一个矩阵的列数不等于第二个矩阵的行数,要求用户重新输入矩阵大小。
while (columnFirst != rowSecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
}

// 函数获取矩阵数据
enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);

// 函数对两个矩阵进行乘法运算。
multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);

// 函数显示乘法后的结果矩阵。
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;
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
cin >> firstMatrix[i][j];
}
}

cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> 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;

// 初始化结果矩阵 mult 的元素为 0。
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}

// 将矩阵 firstMatrix 和 secondMatrix 相乘,并将结果存储在数组 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;

cout << "Output Matrix:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << mult[i][j] << " ";
if(j == columnSecond - 1)
cout << endl << endl;
}
}
}

```**输出**

```cpp
Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

输出矩阵:
24 29

6 25