I'm Having Trouble Multiplying Matrices, Need Help
Apr 1, 2018 at 10:24pm UTC
Hello. I am a beginner user with C++ on Cygwin and I am having trouble multiplying two already-established matrices together with nests and I'm having a little trouble. Can anyone tell me what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
int main(){
int A[2][2] = {{1,2},{3,4}};
int B[2][3] = {{5,6,7},{8,9,10}};
int C[2][3];
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
C[i][j] = 0;
for (int k = 0; k < 3; k++){
C[i][j] += A[i][k] * B[k][j];
}
cout << C[i][j] << " " ;
}
cout << endl;
}
return 0;
}
The output is this:
It should be this:
Last edited on Apr 1, 2018 at 11:08pm UTC
Apr 1, 2018 at 11:14pm UTC
Your loop on lines 14--16 assumes that the A has 3 columns and that the B has 3 rows. That is not quite true, is it?
Apr 1, 2018 at 11:20pm UTC
Your loop on lines 14--16 assumes that the A has 3 columns and that the B has 3 rows. That is not quite true, is it?
No, you're right! It should be k < 2, not k < 3! Thank you! I got the result I wanted now! Thank you again!
Topic archived. No new replies allowed.