两个3行3列矩阵相加用c++怎么写
#include <iostream>
#include <cstdlib>
using namespace std;
void addMatrix(int **a, int **b, int **c, int cRow, int cCol);
void printMatrix(int **array, int arrayRow, int arrayCol);
int main() {
int aRow, aCol;
cout << "请输入矩阵a的行数和列数: ";
cin >> aRow >> aCol;
int **a = new(nothrow) int *[aRow];
for(int i = 0; i < aRow; ++i)
a[i] = new(nothrow) int[aCol];
int **b = new(nothrow) int *[aRow];
for(int i = 0; i < aRow; ++i)
b[i] = new(nothrow) int[aCol];
int **c = new(nothrow) int *[aRow];
for(int i = 0; i < aRow; ++i)
c[i] = new(nothrow) int[aCol];
cout << "请输入矩阵a[" << aRow << "][" << aCol << "]的值" << endl;
for(int i = 0; i < aRow; ++i)
for(int j = 0; j < aCol; ++j)
cin >> a[i][j];
cout << "请输入矩阵b[" << aRow << "][" << aCol << "]的值" << endl;
for(int i = 0; i < aRow; ++i)
for(int j = 0; j < aCol; ++j)
cin >> b[i][j];
addMatrix(a, b, c, aRow, aCol);
cout << "矩阵a和矩阵b相加" << endl;
printMatrix(c, aRow, aCol);
return 0;
}
void addMatrix(int **a, int **b, int **c, int cRow, int cCol) {
for(int i = 0; i < cRow; ++i) {
for(int j = 0; j < cCol; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
void printMatrix(int **array, int arrayRow, int arrayCol) {
for(int i = 0; i < arrayRow; ++i) {
for(int j = 0; j < arrayCol; ++j) {
cout << array[i][j] << " ";
}
cout << endl;
}
}
//任何两个矩阵相加都可以适用