I am writing matrix multiplication and trying to return a two dimensional array from a function but I keep getting errors. Can someone please help me?
here is my code (it is just the skeleton of my program):
void main ()
{
...
int *matmultiply (int, int, int, int [] [mat_cols], int [] [vec_cols], int [][vec_cols])
...
}
int *matmultiply (int mat_rows, int mat_cols, int vec_cols, int matrix [] [mat_cols], int vector [] [vec_cols], int result [][vec_cols])
{
for (int i = 0; i < mat_rows; i++)
{
res = 0; //Stores result of multiplication
for (int k = 0; k < vec_cols; k++)
{
int vrows = 0;
for (int j = 0; j < mat_cols; j++)
{
res = res + ((matrix [j]) * (vector [vrows] [k]));
vrows++;
}
result [k]= res;
}
}
return result; //This is the problem
}
//matmultiply performs multiplication and returns the result matrix.
Any help is appreciated.
'result' must be a global array or static array. And the return type of matmultiply should be int **, since you can't really return an array in C.
Why would you need to return it? You're passing by reference, which modifies the original. The data's already accessible outside the function, in the original.
Arrays don't work that way, anyhow; most compilers won't let you define an array with the size of a parameter. Those which would, don't always get it right...
---------- Post updated at 10:29 PM ---------- Previous update was at 08:06 PM ----------
To return new memory, you'll need to actually allocate new memory (and need to remember to free it later).
#include <stdio.h>
#include <stdlib.h>
// A 2d matrix structure that can be freed with an ordinary 'free'.
typedef struct arr2d
{
int r,c, **rows;
} arr2d;
arr2d *alloc_2d(int r, int c)
{
arr2d *array=NULL;
int *data, n;
size_t size=(r*c)*sizeof(int); // Room for r*c integers
size += sizeof(int *)*(r+1); // ...plus (r+1) int* pointers
size += sizeof(arr2d); // ...plus the structure itself
array=(arr2d *)malloc(size); // Now, actually allocate it
if(array==NULL) return(NULL); // Give up if out of mem
array->r=r; // Set up structure members
array->c=c;
array->rows=(int **)(array+1);
data=(int *)(array->rows+r+1);
for(n=0; n<r; n++) // Set up row pointers
{
array->rows[n]=data;
data+=c;
}
array->rows[r]=NULL; // Guard-pointer; insta-crash if we go beyond it
return(array);
}
void print_2d(const arr2d *array)
{
int r,c;
if(array == NULL) return; // don't print blank arrays
for(r=0; r<array->r; r++)
{
for(c=0; c<array->c; c++) printf("\t%d", array->rows[r][c]);
printf("\n");
}
printf("\n");
}
arr2d *multiply_2d(const arr2d *arr1, const arr2d *arr2)
{
int i, j, n;
arr2d *out;
// Don't operate on empty matrices
if((arr1==NULL)||(arr2==NULL)) return(NULL);
// Don't operate on arrays whose sizes don't match
if(arr1->c != arr2->r) return(NULL);
// Create new matrix to write to
out=alloc_2d(arr1->r, arr2->c);
for(i=0; i<out->r; i++)
for(j=0; j<out->c; j++)
for(n=0; n<arr2->r; n++)
out->rows[j] += arr1->rows[n] * arr2->rows[n][j];
return(out);
}
int main(void)
{
arr2d *arr1=alloc_2d(4, 3), *arr2=alloc_2d(3,2), *arr3=NULL;
arr1->rows[0][0]=14; arr1->rows[0][1]=9; arr1->rows[0][2]=3;
arr1->rows[1][0]=2; arr1->rows[1][1]=11; arr1->rows[1][2]=15;
arr1->rows[2][0]=0; arr1->rows[2][1]=12; arr1->rows[2][2]=17;
arr1->rows[3][0]=5; arr1->rows[3][1]=2; arr1->rows[3][2]=3;
arr2->rows[0][0]=12; arr2->rows[0][1]=25;
arr2->rows[1][0]=9; arr2->rows[1][1]=10;
arr2->rows[2][0]=8; arr2->rows[2][1]=5;
arr3=multiply_2d(arr1, arr2);
print_2d(arr1); print_2d(arr2); print_2d(arr3);
free(arr1); free(arr2); free(arr3);
}
Arrays are call by references in c language.No need to return it.