Like Tree1Likes
  • 1 Post By rags_to_riches

arrays in C

This is a discussion on arrays in C within the C Programming forums, part of the General Programming Boards category; Hi for everyone, I have a matrix that contains zero and nonzero elements i want to do a function that ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    8

    arrays in C

    Hi for everyone,
    I have a matrix that contains zero and nonzero elements
    i want to do a function that return 3 arrays
    the first one is for nonzero elements
    the second array contains the corresponding row numbers of each nonzero element
    the third array
    contains the corresponding column numbers of each nonzero element

    Any help please ??

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    In, C you can NOT return arrays.
    You can return pointers and structures; in addition to normal data type variables.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    thanks for replying
    Do you have some idea how to start ?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    I would start by writing code that display the matrix.
    Write code the returns the columns of the matrix.
    Write code the returns the rows of the matrix.

    The above will help decide how to do the real task.

    Edit: One way is to pass an array of bools (true/false) to a function; and set it to true/false based on if the column is all zeros.
    Edt2: An other way is to use dynamic memory and return a pointer to that memory.

    Tim S.
    Last edited by stahta01; 03-17-2013 at 07:59 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    I have started with this but i'm stack ,i don't konw how to call
    these array in int main
    Code:
    void fonction(int **mat, int n, int m, int tab1[], int tab2[], int tab3[])
    {
        int i,j;
        int k=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                if(mat[i][j]!=0)
            {
                tab1[k]=mat[i][j];
                tab2[k]=i;
                tab3[k]=j;
    
    
            }
            k++;
        }
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    How to call the function; note you need to declare the variables correctly.

    Edit: I have no idea if you function does anything that is correct.

    Code:
        fonction(mat, n, m, tab1, tab2, tab3);
    Last edited by stahta01; 03-17-2013 at 08:34 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    You had more code over here, what happened to it?
    Salem likes this.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    You had more code over here, what happened to it?
    Well, i realize that this idea will not working , so i've thinked about creating a function that return three table
    And as i 'm a beginners in prgramming C,i've asked for help

  9. #9
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    Maybe a little research about passing an address by reference.

    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up a tiny little example of what you are trying to do, and we can help you much better. You're making this too difficult.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You're on the right track. Except your k++ should be inside the condition.
    How do you ensure that the calling function has allocated sufficient space for the three arrays?

  12. #12
    nmn
    nmn is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    allocate sufficient space for tab1[], tab2[], and tab3[] in your calling function according to the values of n and m...you can pass the double-dim array to the fuction as *mat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21