I'm a begginer to C and I've got some problems with programming a function which would take two matrices, add them up and return the result as a third matrix. The fundamental problem is making a function return an array.
I found some solutions online on how to return an array by returning a pointer to the first element of the array, but couldn't apply it to my situation with two-dimensional array. I know how to add matrices in main function, but I need to break the program down into several functions.
Here is my code
float matrix_add(float matrixA[MAX_SIZE][MAX_SIZE], float matrixB[MAX_SIZE][MAX_SIZE], int column, int line)
{
float matrixRes[MAX_SIZE][MAX_SIZE];
for (int i=0; i<column; i++)
{
for (int j=0; j<line; j++)
{
matrixRes[i][j]=matrixA[i][j]+matrixB[i][j];
}
}
return matrixRes;
}
I've tried one of the solutions I've found online:
float *matrix_add(float matrixA[MAX_SIZE][MAX_SIZE], float matrixB[MAX_SIZE][MAX_SIZE], int column, int line)
{
static float *matrixRes[MAX_SIZE][MAX_SIZE];
for (int i=0; i<column; i++)
{
for (int j=0; j<line; j++)
{
*matrixRes[i][j]=matrixA[i][j]+matrixB[i][j];
}
}
return matrixRes;
But there are a few problems with it - I don't understand it, and the function still doesn't work - it returns false results and there is a warning in the compiler "return from incompatible pointer type". Also, I'm not sure how to call it (maybe that's the problem with the solution I found?). I wanted to get some specific value from the array and called the function like this
matrix_add(matrixA, matrixB, column, line)[value][value]);
Where matrixA and B are some 2d arrays, column and line are integer variables. This returns an error (subscripted value is neither array nor pointer nor a vector)
Can you point me in the right direction and tell me how to make this function work (and explain the solution)? MAX_SIZE is predefined value (10) as in this assignment I'm supposed to use static memory allocation (but if you can help me by using dynamic allocation, that's fine) The main function looks like this
int main()
{
int column_num[2], line_num[2];
float matrixA[MAX_SIZE][MAX_SIZE], matrixB[MAX_SIZE][MAX_SIZE];
scanf("%d", &column_num[0]);
scanf("%d", &line_num[0]);
matrix_load_val(matrixA, column_num[0], line_num[0]);
scanf("%d", &column_num[1]);
scanf("%d", &line_num[1]);
matrix_load_val(matrixB, column_num[1], line_num[1]);
}
for (int i=0; i<column_num[0]; i++)
{
for(int j=0; j<line_num[0]; j++)
{
printf("%0.5g\n", matrix_add(matrixA, matrixB, i, j));
}
}
matrix_load_val is a procedure which asks the user for values and puts them in a resultant matrix (it works for sure, tested)