# C++ 程序：使用多维数组将两个矩阵相乘

> 原文： [https://www.programiz.com/cpp-programming/examples/matrix-multiplication](https://www.programiz.com/cpp-programming/examples/matrix-multiplication)

#### 该程序分别采用两个阶为`r1 * c1`和`r2 * c2`的矩阵。 然后，程序将这两个矩阵相乘（如果可能），并将其显示在屏幕上。

要理解此示例，您应该了解以下 [C++ 编程](/cpp-programming "C++ tutorial")主题：

*   [C++ 多维数组](/cpp-programming/multidimensional-arrays)
*   [C++ 数组](/cpp-programming/arrays)

* * *

若要将两个矩阵相乘，第一个矩阵的列数应等于第二个矩阵的行数。 该程序显示错误，直到第一矩阵的列数等于第二矩阵的行数。

## 示例：不使用函数将两个矩阵相乘

```cpp
#include <iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

    cout << "Enter rows and columns for first matrix: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for second matrix: ";
    cin >> r2 >> c2;

    // If column of first matrix in not equal to row of second matrix,
    // ask the user to enter the size of matrix again.
    while (c1!=r2)
    {
        cout << "Error! column of first matrix not equal to row of second.";

        cout << "Enter rows and columns for first matrix: ";
        cin >> r1 >> c1;

        cout << "Enter rows and columns for second matrix: ";
        cin >> r2 >> c2;
    }

    // Storing elements of first matrix.
    cout << endl << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c1; ++j)
        {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

    // Storing elements of second matrix.
    cout << endl << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < r2; ++i)
        for(j = 0; j < c2; ++j)
        {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }

    // Initializing elements of matrix mult to 0.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
        {
            mult[i][j]=0;
        }

    // Multiplying matrix a and b and storing in array mult.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
            for(k = 0; k < c1; ++k)
            {
                mult[i][j] += a[i][k] * b[k][j];
            }

    // Displaying the multiplication of two matrix.
    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < r1; ++i)
    for(j = 0; j < c2; ++j)
    {
        cout << " " << mult[i][j];
        if(j == c2-1)
            cout << endl;
    }

    return 0;
} 
```

**输出**

```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

Output Matrix:
24 29
6  25 
```

在该程序中，首先要求用户输入两个矩阵的大小。

为了乘法，第一矩阵的列应等于第二矩阵的行。 如果不满足此条件，则使用`while`循环再次询问矩阵的大小。

然后，要求用户输入两个矩阵，最后计算并显示两个矩阵的输出。

由于该程序冗长且难以调试，因此最好通过将其传递给函数来解决该程序。

访问此页面以了解通过将数组传递给函数来乘以[矩阵的信息](/cpp-programming/examples/matrix-multiplication-function)。