Matrix/ fill with zeros.

Discussion in 'C Programming' started by David, Jul 6, 2003.

  1. David

    David Guest

    Anyone,
    Does anyonee now how to use the following outline to fill a matrix
    with zeros and call on the first function in the following program?


    void fill_with_zeros(int *mat){
    mat=mat1=mat2=mat3;
    return 0;
    }

    int main(void){
    int mat;
    mat= {0};
    fill_with_zeros(*mat);
    ......
    }

    The first function is called on in main while mat is called into
    fill_with_zeros. The syntax is killing me in my program. Also, the
    matrix is a 3x3. If this isn't enough, I will just copy the entire
    program on here next time I need to ask.

    All I need the output to do is to print:
    "About to fill Matrix1, Matrix2, and Matrix3 with zeros...
    Matrix1:
    0 0 0
    0 0 0
    0 0 0
    Matrix2:
    0 0 0
    0 0 0
    0 0 0
    Matrix3:
    0 0 0
    0 0 0
    0 0 0
    "


    I hope this is good enough.
     
    David, Jul 6, 2003
    #1
    1. Advertising

  2. On 5 Jul 2003 21:40:48 -0700, (David) wrote:

    >void fill_with_zeros(int *mat){
    >mat=mat1=mat2=mat3;


    Hard to know what to do with the above three undeclared identifiers.
    mat is a pointer to int in this scope.

    >return 0;


    The function returns void, i.e. nothing.

    >}
    >
    >int main(void){
    >int mat;


    mat is an int not a matrix. Do you want to declare a two-dimensional
    array?

    >mat= {0};
    >fill_with_zeros(*mat);


    These two expressions are approaching hopelessness. You are treating
    mat as a pointer when it is an int. It is likely that you are
    invoking undefined behavior.

    Why not just write:

    int mat[3][3] = {0};

    mat will be an array of three arrays of three ints. It has
    "matrix-like" form, although it certainly lacks the algebra of
    matrices. All the elements of mat will be zeros.

    If you need to refill the array with zeros, you will need to either
    always use the same dimension (perhaps with a two defines for the
    column and row number) or pass a size to the function for the number
    of rows and columns.

    HTH.

    Best wishes,

    Bob
     
    Robert W Hand, Jul 6, 2003
    #2
    1. Advertising

  3. David

    Kevin Easton Guest

    David <> wrote:
    > Anyone,
    > Does anyonee now how to use the following outline to fill a matrix
    > with zeros and call on the first function in the following program?


    [...]

    > The first function is called on in main while mat is called into
    > fill_with_zeros. The syntax is killing me in my program. Also, the
    > matrix is a 3x3. If this isn't enough, I will just copy the entire
    > program on here next time I need to ask.


    If you want to create a 3x3 int matrix, you'd declare it as:

    int m[3][3];

    If you want to initialise it with all zeroes, this is enough:

    int m[3][3] = { 0 };

    If you want to write a function to fill a three by three matrix with
    zeroes, you could do:

    #include <string.h>

    void fill_with_zeros(int (*m)[3][3])
    {
    static const int z[3][3] = { 0 };

    memcpy(m, &z, sizeof z);
    }

    And call it on a matrix declared as above in the following way:

    fill_with_zeros(&m);

    - Kevin.
     
    Kevin Easton, Jul 6, 2003
    #3
  4. On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
    <> wrote:

    >David <> wrote:
    >> Anyone,
    >> Does anyonee now how to use the following outline to fill a matrix
    >> with zeros and call on the first function in the following program?

    >
    >[...]
    >
    >> The first function is called on in main while mat is called into
    >> fill_with_zeros. The syntax is killing me in my program. Also, the
    >> matrix is a 3x3. If this isn't enough, I will just copy the entire
    >> program on here next time I need to ask.

    >
    >If you want to create a 3x3 int matrix, you'd declare it as:
    >
    >int m[3][3];
    >
    >If you want to initialise it with all zeroes, this is enough:
    >
    >int m[3][3] = { 0 };
    >
    >If you want to write a function to fill a three by three matrix with
    >zeroes, you could do:
    >
    >#include <string.h>
    >
    >void fill_with_zeros(int (*m)[3][3])


    There is no need to clutter the function and its calling statement
    with an unnecessary level of indirection.

    void fill_with_zeros(int m[3][3])

    >{
    > static const int z[3][3] = { 0 };
    >
    > memcpy(m, &z, sizeof z);


    memcpy(m, z, sizeof z);

    >}
    >
    >And call it on a matrix declared as above in the following way:
    >
    >fill_with_zeros(&m);


    fill_with_zeros(m);

    >
    > - Kevin.




    <<Remove the del for email>>
     
    Barry Schwarz, Jul 6, 2003
    #4
  5. David

    Kevin Easton Guest

    Barry Schwarz <> wrote:
    > On Sun, 06 Jul 2003 06:04:06 GMT, Kevin Easton
    > <> wrote:

    [...]
    >>If you want to write a function to fill a three by three matrix with
    >>zeroes, you could do:
    >>
    >>#include <string.h>
    >>
    >>void fill_with_zeros(int (*m)[3][3])

    >
    > There is no need to clutter the function and its calling statement
    > with an unnecessary level of indirection.
    >
    > void fill_with_zeros(int m[3][3])


    Sure there is - my version allows the the compiler to check both the
    number of rows and columns is correct, in yours only the columns.

    And as you've shown, the only thing we pay for this is 4 characters in
    the function definition and one in the function caller.

    - Kevin.
     
    Kevin Easton, Jul 7, 2003
    #5
    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. Nevets Steprock
    Replies:
    10
    Views:
    705
    Nevets Steprock
    Jun 14, 2005
  2. lvcargnini

    Matrix composed by two matrix

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

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    420
    Holgerson
    Oct 26, 2007
  5. Johnny
    Replies:
    2
    Views:
    393
    Christian Heimes
    Aug 14, 2008
Loading...

Share This Page