Dynamic matrix

Discussion in 'C Programming' started by paytam@gmail.com, Nov 13, 2005.

  1. Guest

    Hi all
    How can I define a dynamic matrix and pass it to a function?
    , Nov 13, 2005
    #1
    1. Advertising

  2. Malcolm Guest

    <> wrote in message
    news:...
    > Hi all
    > How can I define a dynamic matrix and pass it to a function?
    >


    /*
    caller
    */

    /* for the sake of argument, make the matrix 20 x 20 */
    int N = 20;
    double *ptr = malloc(N * N * sizeof(double));

    setidentity(ptr, N);

    /* do all sorts of wonderful thngs with your matrix here */
    /* free after you have finished with it */
    free(ptr);
    /*
    function - sets a square matrix to identity (all zero except the main
    diagonal)
    */
    void setidentity(double *mtx, int size)
    {
    int i;

    for(i=0;i<size * size;i++)
    mtx = 0.0;
    for(i=0;i<size;i++)
    mtx[i*size+i] = 1.0;
    }
    Malcolm, Nov 13, 2005
    #2
    1. Advertising

  3. a écrit :
    > Hi all
    > How can I define a dynamic matrix and pass it to a function?


    Use a structure to gather the relevent information.

    struct mat2d
    {
    /* array of y pointers to arrays of x T */
    T **p;
    size_t x;
    size_t y;
    };

    or the linear way :

    struct mat2d
    {
    /* array of (y * x) T */
    T *p;
    size_t x;
    size_t y;
    };

    and provide a function to access the data.

    --
    A+

    Emmanuel Delahaye
    Emmanuel Delahaye, Nov 13, 2005
    #3
  4. On 13 Nov 2005 11:17:55 -0800, wrote:

    >Hi all
    >How can I define a dynamic matrix and pass it to a function?


    There are two common approaches. For N rows with M columns each:

    One is to simulate the matrix with a one-dimensional array. You
    allocate space for N*M objects, as with
    T *ptr = malloc(N * M * sizeof *ptr);
    and you reference the (i,j)th element by calculating the appropriate
    subscript yourself, as in
    ptr[i*M+j] = 0;

    The other is to build an array of pointers, each pointing to one
    row of the matrix, as in
    T **ptr = malloc(N * sizeof *ptr);
    for (k = 0; k < N; k++)
    ptr[k] = malloc(M * sizeof *ptr[k]);
    and you reference the (i,j)th element using the natural syntax, as in
    ptr[j] = 0;

    In either case, you pass the array to a function by using the ptr
    variable. Note, since N and M are not known until run time:

    Using the first approach always requires M to be available to the
    function.

    In either approach, the function will need both N and M if it is
    concerned in any way with the top or right boundary elements of the
    matrix.

    The second approach is more flexible in that it can handle jagged
    arrays where M is not constant for each row.


    <<Remove the del for email>>
    Barry Schwarz, Nov 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonietta Tobito via JavaKB.com

    Dynamic radio buttons matrix using Struts

    Antonietta Tobito via JavaKB.com, Feb 2, 2005, in forum: Java
    Replies:
    2
    Views:
    3,355
    Wendy S
    Feb 2, 2005
  2. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,638
    Jonathan Bromley
    Jul 5, 2006
  3. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    391
    Holgerson
    Oct 26, 2007
  4. Terry Reedy
    Replies:
    0
    Views:
    538
    Terry Reedy
    Apr 2, 2009
  5. Robert Kern
    Replies:
    0
    Views:
    573
    Robert Kern
    Apr 2, 2009
Loading...

Share This Page