输入两个m行n列的矩阵A和B,输出它们的和A+B
时间: 2023-11-06 13:06:58 浏览: 171
C语言数组二
好的,下面是 C++ 代码实现:
```cpp
#include <iostream>
using namespace std;
const int MAXN = 1005; // 矩阵最大行列数
int A[MAXN][MAXN], B[MAXN][MAXN], C[MAXN][MAXN]; // 定义三个矩阵
int main()
{
int m, n;
cout << "请输入矩阵的行数和列数:";
cin >> m >> n;
// 输入矩阵 A
cout << "请输入矩阵 A:" << endl;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> A[i][j];
// 输入矩阵 B
cout << "请输入矩阵 B:" << endl;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> B[i][j];
// 计算矩阵 C = A + B
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
// 输出矩阵 C
cout << "矩阵 A + B = " << endl;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
cout << C[i][j] << " ";
cout << endl;
}
return 0;
}
```
希望能帮助到你!
阅读全文