Segmentation Error 11

My code uses a function for the multiplication of two matrices, then a pointer function is used to call the matrix multiplication function. However, I get a segmentation error (11) when attempting output the solution of the pointer function. Any ideas?

Thanks!

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
#include <iostream>


void matrixMultiplication (const double A[3][3], const double B[3][3], double output[3][3])
{
	int i, j, k; 

	for (i=0;i<3;i++) 
		{
		for(j=0;j<3;j++)
			{
			for(k=0;k<3;k++)
				{
				output[i][j]+=A[i][k]*B[k][j];
				}
			}
		}
}

double (*MM (const double (*left)[3], const double (*right)[3]))[3]
{
    double output[3][3];
    matrixMultiplication(left, right, output);
    
}

int main ()	{

	using namespace std;

	double A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
	double B[3][3]={{1,1,1},{1,1,1},{1,1,1}};
    
    cout<<"The function MM returns..."<<endl;
    
    double print[3][3]=MM(A,B);

    int i, j;
    for (i=0;i<3;i++)   {
        for (j=0;j<3;j++)   {
            cout<<print[i][j]<<"\t";
        }
        cout<<"\n";
    }

    delete[] print;
    return 0;
}
Why are you delete'ing print, if it was never new'd. Since your matrixMultiplication function returns it's result by argument, use it that way, and skip the middle man (that's what MM stands for?):
In place of line 36:
 
matrixMultiplication(A, B, print);

For it to work, print should be declared as an empty array, or you will get random numbers. So, to sum up, in place of line 36 use 2 lines:
1
2
double print[3][3] = {0};
matrixMultiplication(A, B, print);
Last edited on
Sorry, I'm a bit of a rookie when it comes to C++

So I'm trying to do an exercise in code reuse by introducing a pointer function 'MM' that calls the function matrixMultiplication. But I'm having problems trying to output the matrix formed by this pointer function
Topic archived. No new replies allowed.