0

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.

WedaPashi
  • 3,561
  • 26
  • 42
  • There are several pieces of relevant info missing such as the definitions of `col`, `value`, and `finalmidd`. Please update your question with a [mcve]. – dbush Jan 01 '18 at 04:51
  • @H.S. for(i = 0; i < col; i++) { transarray[i] = malloc( value * sizeof(double)); } it's col x value – Kamarudeen Ayankunbi Jan 01 '18 at 05:00
  • @KamarudeenAyankunbi: The fixed size `final` array is having dimension `col x col` and the dynamic 2D `final` array size is `diffvalue x col`. For dynamic 2D `final` array you are allocating number of rows as `diffvalue` - `final=(double **)malloc(diffvalue*sizeof(double*));` but while allocating rows, the `for` loop is running only `col` times for `final` - `for(i = 0; i < col; i++)`. – H.S. Jan 01 '18 at 05:04
  • There is no need to cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714). `final = malloc (diffvalue * sizeof *final);` is all that is required and by using the dereferenced variable to determine the size, there is no chance of inadvertent mistake in typesize. – David C. Rankin Jan 01 '18 at 06:54
  • Your code is still missing crucial information, and isn't initializing any matrices, and isn't showing sample input data. Please fix the question (so we can all have a happier new year)! – Jonathan Leffler Jan 01 '18 at 06:55
  • What is the type for the array `finalmidd`? Is it static or dynamic? – Rishikesh Raje Jan 01 '18 at 07:25

0 Answers0