在 C++ 中,我们可以创建多维数组,这意味着“数组的数组”。例如,`int x[3][4]`,`x` 是一个具有 3 行 4 列的多维数组,或者我们可以称它为矩阵,它可以容纳最多 12 个元素。
要解决这个 C++ 程序,我们需要知道如何相乘两个矩阵。只有当第一个矩阵的列数等于第二个矩阵的行数时,我们才能进行矩阵乘法。然后结果矩阵将具有与第一个矩阵相同的行数和与第二个矩阵相同的列数。
在这里,要求用户输入第一个矩阵的行数 **r1** 和列数 **c1**,以及第二个矩阵的行数 **r2** 和列数 **c2**。使用 `while` 语句检查第一个矩阵的列数是否等于第二个矩阵的行数,`While (c1 != r2 )`。
如果不相等,则打印错误消息,否则,要求用户输入第一个和第二个矩阵的行数和列数。如果满足条件 **c1 = r2**,则存储第一个矩阵的元素。
要求用户输入第一个矩阵的元素。使用嵌套 `for` 循环将元素读入第一个矩阵。将元素读入第一个数组 **a[i][j]**。类似地,将元素读入第二个矩阵 **b[i][j]**。
访问矩阵 'a' 的第一列的第一个元素和矩阵 '**b**' 的第一行的第一个元素。将这些元素相乘并将结果存储在数组 **mul** 中。显示结果矩阵。
步骤 1: 调用头文件 `iostream`。
步骤 2: 使用 `namespace std`。
步骤 3: 打开整型主函数;**int main()**。
步骤 4: 声明整型变量 **r1, c1, r2, c2, i, j, k**;以及数组 **a[10][10], b[10][10], mul[10][10]**;
步骤 5: 要求用户输入第一个矩阵和第二个矩阵的行数和列数。
步骤 6: 分别将数字读入变量 **r1, c1** 和 **r2, c2**;
步骤 7: 使用 `while` 循环检查 **c1=r2**。如果不满足,则打印错误消息并重复步骤 5。
步骤 8: 要求用户输入第一个矩阵的元素。
步骤 9: 将元素读入数组 **a[10][10]**。
步骤 10: 要求用户输入第二个矩阵的元素。
步骤 11: 将元素读入数组 **b[10][10]**;
步骤 12: 将矩阵 **mul** 的元素初始化为 0;
步骤 13: 获取矩阵 **a[10][10]** 的第一行的第一个元素和矩阵 **b[10][10]** 的第一列的第一个元素;
步骤 14: 将两个元素相乘并将结果存储在数组 **mul[10][10]** 中;
步骤 15: 重复步骤 14 直到 **a[r1][c1]** 和 **b[r2][c2]**。
步骤 16: 显示 **mul[10][10]**。
步骤 17: 退出。
#include <iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], mul[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 mul to 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mul[i][j]=0;
}
// Multiplying matrix a and b and storing in array mul.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mul[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 << " " << mul[i][j];
if(j == c2-1)
cout << endl;
}
return 0;
}
Enter rows and columns for first matrix: 2 3 Enter rows and columns for second matrix: 2 3 Error! column of first matrix not equal to row of second.Enter rows and columns for first matrix: 3 2 Enter rows and columns for second matrix: 2 3 Enter elements of matrix 1: Enter element a11 : 3 Enter element a12 : 2 Enter element a21 : 5 Enter element a22 : 8 Enter element a31 : 4 Enter element a32 : 6 Enter elements of matrix 2: Enter element b11 : 4 Enter element b12 : 2 Enter element b13 : 6 Enter element b21 : 3 Enter element b22 : 8 Enter element b23 : 4 Output Matrix: 18 22 26 44 74 62 34 56 48