### Multiplying Matrix

Hello all I am trying to multiply two matrix together. For some reason the first number in each column comes out correct, but the second is some tiny decimal number so I get a segmentation fault core dumped error. I need n in the function to be 10 so I am not sure if I am using the correct matrix size when multiplying. Any help would be greatly appreciated. Here is my code with the error shown at the bottom.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include using namespace std; //const int SIZE = 100; //typedef double Matrix[SIZE][SIZE]; void multiply(double a[2][5], double b[5][2], double c[5][5], int n) { int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { c[i][j] = 0; for (k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j]; } } int main() { double count1; double matrix1[2][5]={{1,2,3,4,5}, {4,5,6,7,8}}; double matrix2[5][2]={ {10,11}, {13,14}, {16,17}, {19,20}, {23,25}}; double matrix3[5][5] ={{0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0}}; multiply(matrix1,matrix2,matrix3,10); cout <
Last edited on
Try this:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include using namespace std; void multiply(double a[2][5], double b[5][2], double c[2][2], int m, int n) { int i, j, k; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { c[i][j] = 0.; for (k = 0; k < n; k++) { c[i][j] += a[i][k] * b[k][j]; } } cout << '\n'; } } int main() { double matrix1[2][5]= {{1,2,3,4,5}, {4,5,6,7,8}}; double matrix2[5][2]= {{10,11}, {13,14}, {16,17}, {19,20}, {23,25}}; double matrix3[2][2] = {{0,0}, {0,0}}; multiply(matrix1,matrix2,matrix3, 2, 5); for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) cout << matrix3[i][j] << ' '; cout << '\n'; } }`````` ``` 275 295 518 556```

Generally speaking, the product of a matrix (M x P) with a matrix (P x N) gives a matrix (M x N).
Last edited on
Hi condor thanks for the help it does work! Just wondering why n is 5? I need to multiply 2 matrix where n is 10.
One more question just to make sure I understand Matrix c is 2x2, but if a would have been 5x2 and b would have been 2x5 would c then be 5x5?
Yes, 5x2 and 2x5 give 5x5. I wrote in my previous post: (MxP) with (PxN) -> (MxN).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include using namespace std; #define m 2 #define p 5 #define n 3 int main(void) { // Example a(2x5) x b(5x3) -> c(2x3) int a[m][p] = {{1, 2, 3, 4, 5}, {3, 4, 5, 6, 7}}; int b[p][n] = {{7, 8, 1}, {9, 10, 2}, {10, 11, 1}, {1, 2, 3}, {3, 14, 3}}; int c[m][n]; for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) { c[i][j] = 0; for(int k = 0; k < p; ++k) c[i][j] += a[i][k] * b[k][j]; } for (int i = 0; i < m; ++i) { cout << '\n'; for(int j =0; j < n; ++j) cout << setw(5) << c[i][j]; } cout << endl; return 0; }`````` ``` 74 139 35 134 229 55```

EDIT:
If all is OK please mark this thread as Solved.
Last edited on
Topic archived. No new replies allowed.