我正在编写一个程序,将两个3X3矩阵相乘。我遇到了一些问题,我无法弄清楚问题所在。任何帮助,将不胜感激:d在C++中乘以3x3矩阵
#include <iostream>
using namespace std;
int main(){
int matrix1[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int matrix2[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int results[3][3];
int product = 0;
int i;
int j;
for (i = 1; i <= 3; i++){
for (j = 1; j <= 3; j++){
product += matrix1[i][j] * matrix2[j][i];
cout << product << endl;
}
results[i][j] = product;
product = 0;
}
cout << endl << "Output Matrix: " << endl;
for (int i = 1; i < 4; i++){
for (int j = 1; j < 4; j++){
cout << results[i][j];
}
cout << endl;
}
system("pause");
return 0;
}
这是结果我把它弄出来:
提前再次25
73
-1717986851
48
129
-858993331
-1867771963
1566576709
1595991863
Output Matrix:
-858993460-858993460-858993460
-1717986851-858993460-858993460
-85899333112
Press any key to continue . . .
谢谢! :D
为什么你使用'删除[]矩阵1;'ASO,如果你从来没有创建这些使用'new'? –
'results [i] [j] = product;'应该在内部循环体内。 –
首先,你似乎忘记了数组索引从零到大小减1。 –