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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
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 <<matrix3[0][0]<<endl;
cout <<matrix3[0][1]<<endl;
cout <<matrix3[1][0]<<endl;
cout <<matrix3[1][1]<<endl;
}
~
~
~
~
~
~
~
~
"matrix.cpp" 49L, 1012C written
ws014@cs:~$ c++ matrix.cpp
ws014@cs:~$ a.out
275
-9.63777e+304
518
-1.92755e+305
Segmentation fault (core dumped)
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>

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.