Arrays in user defined function for matrix addition

I'm trying to write a user defined function that will take two arrays as arguments and return the sum of the two matrices, the problem seems to be something to do with the expression requiring pointer to object type which is no help for me in determining the problem, but may be for some of you. specifically, it gives me the red line under the j in Mat[i][j] and Mat2[i][j] in line 8, telling me "expression must have pointer-to-object type". It also complains about the "return value not matching the function type" at "return(D)". how can I get this code to work??
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int MatrixSum(int Mat[], int Mat2[])
{
	int D[3][3]={{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};
	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
8->			D[i][j]=Mat[i][j]+Mat2[i][j];
		}
	}
	cout<<"The Matrix sum of A and B is ";
	return(D);
	getch();
}
It sounds like you want MatrixSum to take two 2-dimensional arrays. Right now you're passing in two one-dimensional arrays.

Also you're returning D which is an array, but the function is declared as returning an integer.

You might be better off passing in both the arguments and destination of the result. Something like:
void MatrixSum(int M1[3][3], int M2[3][3], int D[3][3])

@dhayden
Wouldn't defining the function like that mean i would have to provide a value of the result of the sum of M1 and M2?
You have a int Mat[]; (1D array) and you try to access Mat[i][j] (like it was a 2D array). Type mismatch. Therefore, the function has to take 2D array parameters.

You have int D[3][3] (a 2D array) and the function promises to return one int.

void MatrixSum( const int Mat[3][3], const int Mat2[3][3], int D[3][3] );

[Edit] array parameters are by reference rather than by value.
You do need space for the result anyway:
1
2
3
4
5
int X[3][3] = ...
int Y[3][3] = ...

int Result[3][3] = {0};
MatrixSum( X, Y, Result );
Last edited on
ok, so it seems happy with the j's, it's still got a problem with the "return D" though
Don't return anything from the function.
ok, thanks
Topic archived. No new replies allowed.