-
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 ?? :frown:
-
In, C you can NOT return arrays.
You can return pointers and structures; in addition to normal data type variables.
Tim S.
-
thanks for replying
Do you have some idea how to start ?
-
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.
-
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++;
}
}
-
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);
-
-
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
-
Maybe a little research about passing an address by reference.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
-
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.
-
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?
-
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