I am having some trouble multiplying matrices and printing the result out. My code is as follows:
#include <iostream>
using namespace std;
int main() {
int matArow, matAcol, matBrow, matBcol, itervar1, itervar2, itervar3, value;
cin >> matArow >> matAcol;
int matA[matArow][matAcol];
for (itervar1 = 0; itervar1 < matArow; itervar1++) {
for (itervar2 = 0; itervar2 < matAcol; itervar2++) {
cin >> matA[itervar1][itervar2];
}
}
cin >> matBrow >> matBcol;
if (matBrow != matAcol) {
cout << "Impossible\n";
return 0;
}
int matB[matBrow][matBcol];
for (itervar1 = 0; itervar1 < matBrow; itervar1++) {
for (itervar2 = 0; itervar2 < matBcol; itervar2++) {
cin >> matB[itervar1][itervar2];
}
}
int matC[matArow][matBcol];
for (itervar1 = 0; itervar1 < matArow; itervar1++) {
for (itervar2 = 0; itervar2 < matBcol; itervar2++) {
value = 0;
for (itervar3 = 0; itervar3 < matAcol; itervar3++) {
value += matA[itervar1][itervar3];
value += matB[itervar3][itervar2];
}
matC[itervar1][itervar2] = value;
}
}
for ( itervar1 = 0; itervar1 < matArow; itervar1++ ) {
for ( itervar2 = 0; itervar2 < matBcol; itervar2++ ) {
cout << matC[itervar1][itervar2] << ' ';
}
cout << endl;
}
return 0;
}
The code doesn't return any errors, but results in incorrect matrix multiplication. Any help would be greatly appreciated.
value += matA[itervar1][itervar3]; value += matB[itervar3][itervar2];
不应该是
value += matA[itervar1][itervar3] * matB[itervar3][itervar2];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.