Hi everyone!
I'm trying to create this function:
Given a matrix, create a list which has in his nodes the values of the matrix that are greater than zero. The function has to return the double pointer to the list.
Here there is my function:
Code:
struct node** positive_mtr (int *A, int r, int c){
struct node **doub;
struct node *point;
doub = &point;
*doub= NULL;
int i,j, value;
for (i=0;i<r;i++){
for (j=0;j<c;j++){
if(A[i*c+j]>0){
value= A[i*c+j];
suf_insert(doub, value);
/* suf_insert is the function which insert the value at the end of the list*/
}
}
}
return doub;
}
Here I write you the main where I use this function:
Code:
int main(){
struct node **dptr;
int *B;
int n=2;
B=(int*)calloc(n*n, sizeof(int));
int i,j,value;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
printf ("\n\which values do you want to put ");
printf(" in the position a=%d c=%d ?\n\n",i+1,j+1);
scanf("%d",&value);
B[i*n+j]=value;
}
}
dptr=positive_mtr(B,n,n);
printf ("\n\n\n\n The values of the matrix greater than zero \n\n");
while ((*dptr)!=NULL){
printf ("\n\n one of the values in the list is %d\n\n", (*dptr)->value);
(*dptr)= (*dptr)->next;
}
return 0;
}
In the main, the program terminates at the first cycle of while.
Instead of printing "one the values in the list is (the first number greater than zero of the matrix)",
it prints "one the values in the list is 268433539".
Someone told me that there are some mistakes in the main, while the function is correct.
May someone help me to find the mistakes in the main please?