finding matrix transpose - why doesn't it work when passing pointer argument?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbd
    New Member
    • Feb 2008
    • 1

    #1

    finding matrix transpose - why doesn't it work when passing pointer argument?

    Hi

    I'm adapting some code I've written using 2d arrays (to represent matrices) to handle large arrays such that double matrix[][] goes to double **matrix and then I'm using malloc.

    It seems to work fine for part of my program up to where I have to find the matrix transpose at which point it does something I don't understand. I've taken that bit of code out and run it by itself (included below), get the same problem... which is that the input matrix is getting modified when the function is called, here's an example for a test 3 by 3:
    The input is:in[0][0] = 10.000000
    in[0][1] = 10.000000
    in[0][2] = 10.000000
    in[1][0] = 5.000000
    in[1][1] = 5.000000
    in[1][2] = 5.000000
    in[2][0] = 3.333333
    in[2][1] = 3.333333
    in[2][2] = 3.333333

    but after being passed to find_transpose is comes out as:in[0][0] = 10.000000
    in[0][1] = 10.000000
    in[0][2] = 10.000000
    in[1][0] = 10.000000
    in[1][1] = 5.000000
    in[1][2] = 10.000000
    in[2][0] = 3.333333
    in[2][1] = 3.333333
    in[2][2] = 10.000000

    and the actual transpose output is:
    out[0][0] = 10.000000
    out[0][1] = 10.000000
    out[0][2] = 10.000000
    out[1][0] = 10.000000
    out[1][1] = 5.000000
    out[1][2] = 3.333333
    out[2][0] = 10.000000
    out[2][1] = 10.000000
    out[2][2] = 10.000000

    I really don't understand why!?
    Can anyone help??

    Thanks
    jbd


    nt find_transpose( int n, double **a, double **b)
    {
    int i,j;
    for (i=0; i<n; i++)
    {
    for (j=0; j<n; j++)
    b[i][j] = a[i][j];
    }
    for (i=0; i<n; i++)
    {
    for (j=0; j<n; j++)
    b[i][j] = a[j][i];
    }

    }


    int main (void)
    {

    int i, j, p=3;
    double **in, **out;

    in = malloc(p * sizeof(int *));
    out = malloc(p * sizeof(int *));

    for (i=0; i<p; i++){
    in[i]= malloc(p * sizeof(int *));
    out[i]= malloc(p * sizeof(int *));}

    for (i=0; i<p; i++)
    {
    for (j=0; j<p; j++)
    {in[i][j]= 10./(i+1);
    printf("in[%i][%i] = %f\n", i, j, in[i][j]);
    }
    }

    find_transpose( p, in, out);

    for (i=0; i<p; i++)
    {
    for (j=0; j<p; j++)
    printf("in[%i][%i] = %f\n", i, j, in[i][j]);
    }

    for (i=0; i<p; i++)
    {
    for (j=0; j<p; j++)
    printf("out[%i][%i] = %f\n", i, j, out[i][j]);
    }

    return 0;
    }
  • hdanw
    New Member
    • Feb 2008
    • 61

    #2
    Originally posted by jbd
    Hi



    [qoute]
    int find_transpose( int n, double **a, double **b)
    {
    int i,j;
    for (i=0; i<n; i++)
    {
    for (j=0; j<n; j++)
    b[i][j] = a[i][j];
    }
    for (i=0; i<n; i++)
    {
    for (j=0; j<n; j++)
    b[i][j] = a[j][i];
    }
    }
    [/qoute]
    Did you compile this?
    The first loop is useless, the intire contents are over written by the second loop.
    Also need to return a val since you said it would return one, ore declare the function void.

    [qoute]
    Code:
    double **in, **out;
    
    in = malloc(p * sizeof(int *));
    out = malloc(p * sizeof(int *));
    [/qoute]

    This is not currently a problem, but you are mixing your pointers up.

    Dont declare it as a pointer to a double unless it is. What you have is pointers to ints.

    [qoute]
    Code:
    int **in, **out;
    [/qoute]

    And you ought to cast them so :
    [qoute]
    Code:
    in =  ( int ** ) malloc(p * sizeof(int *));
    out =( int ** ) malloc(p * sizeof(int *));
    [/qoute]

    Again for the rows
    [qoute]
    Code:
    for (i=0; i<p; i++){
    in[i]= malloc(p * sizeof(int *));
    out[i]= malloc(p * sizeof(int *));}
    [/qoute]

    Should be

    [qoute]
    Code:
    for (i=0; i<p; i++){
    in[i]= ( int * ) malloc(p * sizeof(int *));
    out[i]= ( int * ) malloc(p * sizeof(int *));}
    [/qoute]


    I would suggest getting rid og malloc alltogether:

    Code:
     //in = malloc(p * sizeof(int *));
      in = new int*[p];
    
    //out = malloc(p * sizeof(int *));
     out = new int*[p];
    
    for (i=0; i<p; i++){
    
    // in[i]= malloc(p * sizeof(int *));
       in[i] = new int[p];
    
    //out[i]= malloc(p * sizeof(int *));
      out[i] = new int[p];
    
    }
    After compileing this, it works fine.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There are no multi-dimensional arrays in C or C++.

      You might read this:
      Originally posted by weaknessforcats
      First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
      [code=c]
      int array[5];
      [/code]

      That is an array of 5 elements each of which is an int.

      [code=c]
      int array[];
      [/code]

      won't compile. You need to declare the number of elements.

      Second, this array:
      [code=c]
      int array[5][10];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 int.

      [code=c]
      int array[5][10][15];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


      [code=c]
      int array[][10];
      [/code]

      won't compile. You need to declare the number of elements.

      Third, the name of an array is the address of element 0
      [code=c]
      int array[5];
      [/code]

      Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

      [code=c]
      int array[5][10];
      [/code]

      Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
      [code=c]
      int array[5][10];

      int (*ptr)[10] = array;
      [/code]

      Fourth, when the number of elements is not known at compile time, you create the array dynamically:

      [code=c]
      int* array = new int[value];
      int (*ptr)[10] = new int[value][10];
      int (*ptr)[10][15] = new int[value][10][15];
      [/code]

      In each case value is the number of elements. Any other brackets only describe the elements.

      Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

      [code=c]
      int** ptr = new int[value][10]; //ERROR
      [/code]

      new returns the address of an array of 10 int and that isn't the same as an int**.

      Likewise:
      [code=c]
      int*** ptr = new int[value][10][15]; //ERROR
      [/code]

      new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

      With the above in mind this array:
      [code=cpp]
      int array[10] = {0,1,2,3,4,5,6, 7,8,9};
      [/code]
      has a memory layout of

      0 1 2 3 4 5 6 7 8 9

      Wheras this array:
      [code=cpp]
      int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
      [/code]
      has a memory layout of

      0 1 2 3 4 5 6 7 8 9

      Kinda the same, right?

      So if your disc file contains

      0 1 2 3 4 5 6 7 8 9

      Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

      Therefore, when you do your read use the address of array[0][0] and read as though you have a
      one-dimensional array and the values will be in the correct locations.

      Comment

      Working...