I want to pass in dynamic 2d arrays so I can do matrix multiplication but I'm getting incorrect values.I get correct values when I use a fixed sized array but doing the same thing with a malloced array gives me incorrect values in the last row of my result matrix.
Here is the output for the dynamic array:
-1 15 18 15 -11 0 -13 1 -7 -16
0 -0 -1 0 -0 -0 -0 0 0 0
-0 0 0 0 -0 -0 0 0 -1 0
0 0 -0 -0 -0 0 -0 -0 0 -0
-3988 16658 -9831 7502 3081 58360 3515 -5844 4840 6023
Here is the output for the static array:
-1 15 18 15 -11 0 -13 1 -7 -16
0 -0 -1 0 -0 -0 -0 0 0 0
-0 0 0 0 -0 -0 0 0 -1 0
0 0 -0 -0 -0 0 -0 -0 0 -0
0 -0 -0 -0 0 -0 0 -0 0 0
Here are the important parts of the code for the dynamic array:
int main(int argc, char * argv[])
{
double **final=NULL;
double **transarray=NULL;
final=(double **)malloc(diffvalue*sizeof(double*));
transarray=(double **)malloc(col*sizeof(double*));
for(i = 0; i < col; i++)
{
transarray[i] = malloc( value * sizeof(double));
}
for(i = 0; i < col; i++)
{
final[i] = malloc( col * sizeof(double));
}
run(final,transarray,finalmidd);
}
void run(double **hold,double **transarray,double **finalmidd)
{
double one;
int i;
int j;
int k;
for(i = 0; i < col ; i++)
{
for( j = 0; j < value ; j++)
{
one = 0;
for( k = 0; k < col; k++)
one+= hold[i][k] *transarray[k][j];
finalmidd[i][j]=one;
}
}
}
Here are the important parts of the code for the fixed size array:
int main(int argc, char * argv[])
{
double final[col][col];
double transarray[col][value];
run(final,transarray,finalmidd);
}
void run(double hold[col][col],double transarray[col][value],double finalmidd[col][value])
{
double one;
int i;
int j;
int k;
for(i = 0; i < col ; i++)
{
for( j = 0; j < value ; j++)
{
one = 0;
for( k = 0; k < col; k++)
one+= hold[i][k] *transarray[k][j];
finalmidd[i][j]=one;
}
}
}
I don't see why they both codes don't output the same thing. I just translated the same code for the static array into a dynamic one. I'm reading in large inputs, so I need it to be dynamic so it doesn't segfault. Can someone please help me with this. Please ask if you need more info.