c: ignores return and function is repeated -
i have determine if graph connected or not.
i think used bad concept of return here, problem is: when top =<0 , k == vertecsfuncion, enters penultimate if, , wanted return 0; ending function , carrying on execution. when arrives return 0; go time while(top>0). happens 2 times, while second time, top increases 1 (i don't know how) , arrives "error". thank time.
int connex(int **matrix, int *visited, pila *p, int vertucsfuncion, int k,int x, int top){ int u,j,c,f,contdos=0, cont=0; x = pila_top(*p); printf("\n%d\n",top); while (top > 0){ cont = 0; k++; for(u=0;u<k;u++){ if(visited[u]!=x){ contdos++; } } if (contdos==k){ visited[contdos-1]=x; contdos = 0; }else{ k--; contdos = 0; } for(j=0;j<vertucsfuncion;j++){ if(matrix[(x-1)][j]!=0){ if(j!=(x-1)){ matrix[(x-1)][j]=matrix[(x-1)][j]-1; matrix[j][(x-1)]=matrix[j][(x-1)]-1; for(c=0;c<vertucsfuncion;c++){ for(f=0;f<vertucsfuncion;f++){ printf("|%d|",matrix[c][f]); } printf("\n"); } printf("\n"); pila_push(p,(j+1)); top++; x = pila_top(*p); connex(matrix,visited,p,vertucsfuncion,k,x,top); }else{ matrix[(x-1)][j]=matrix[(x-1)][j]-1; for(c=0;c<vertucsfuncion;c++){ for(f=0;f<vertucsfuncion;f++){ printf("|%d|",matrix[c][f]); } printf("\n"); } pila_push(p,(j+1)); top++; connex(matrix,visited,p,vertucsfuncion,k,x,top); } } cont++; } if(cont == vertucsfuncion ){ pila_pop(p); top--; connex(matrix,visited,p,vertucsfuncion,k,x,top); } } if(k==vertucsfuncion){ printf("is connex"); return 0; } else{ return 1; } return 0; }
inside connex, inside while(top>0)
loop recursively calling connex again:
top--; connex(matrix,visited,p,vertucsfuncion,k,x,top);
when second call connex calls return 0
return inner call connex , outer call still proceed while(top >0)
loop.
Comments
Post a Comment