I have this code, which is a function to make the steps for a percolation simulation in a bidimensional array.
int step (double ** mat, unsigned n, unsigned m, double a, double b)
{
int i, h, r, c, steps, x, y, o, v; // search for minimum
int min;
FILE *fp;
for(steps=0; steps<2; steps++) // percolation steps
{
for (o=0; o<n; o++)
{
for(v=0; v<m; v++)
{
if (mat[o][v]==-2) {mat[o][v]=-1;}
}
} //trasformo i -2 in -1
min=b; //set the minimum to max of range
for(x=0; x<n; x++) // i search in the matrix the invaded boxes
{
for(y=0; y<m; y++)
{
if (mat[x][y]=-1) //control for the boxes
{
if (mat[x][y-1]<=min && mat[x][y-1]>=0) {min=mat[x][y-1]; r=x; c=y-1;} //look for the minimum adjacent left and right
if (mat[x][y+1]<=min && mat[x][y+1]>=0) {min=mat[x][y+1]; r=x; c=y+1;}
for (i=-1; i<=1; i++) //look for the minimum adjacent up and down
{
for(h=-1; h<=1; h++)
{
if (mat[(x)+i][(y)+h]<=min && mat[(x)+i][(y)+h]>=0)
{
min=mat[(x)+i][(y)+h];
r=(x)+i; c=(y)+h;
}
}
}
}
}
}
mat[r][c]=-2;
x=r; y=c;
}
return 0;
}
When I use it in my main function I obtain Segmentation-fault (core dump created). Do you have any idea where the error is?
mat[x][y-1]withy = 0leads to undefined behaviour. Same fory = mandmat[x][y+1]. Also, use of=instead of==. Enable warnings in your compiler. - Zetaif (mat[x][y]=-1)? - BLUEPIXY