I have a question about my code :
#include
void draw_a_square(int row,int column, int grid[10][10]){
for(int r = 0; r < row; r++){
for(int c = 0; c < column; c++){
if (grid[r][c] == 5) {
printf("\\ ");
} else if (grid[r][c] == 0){
printf(". ");
} else if (grid[r][c] == 1){
printf("* ");
}
}
printf("\n");
}
}
int dfs(int row,int column,int grid[10][10],int rows_border,int columns_border){
if(row==rows_border){
return 0;
}
if (row==0)
{
return 0;
}
if(column==columns_border){
return 0;
}
if (column==0)
{
return 0;
}
int r= row-1;
int c= column-1;
grid[r][c]==1;
draw_a_square(rows_border,columns_border,grid);
//up
dfs(row-1,column,grid,rows_border,columns_border);
//down
dfs(row+1,column,grid,rows_border,columns_border);
//left
dfs(row,column-1,grid,rows_border,columns_border);
//right
dfs(row,column+1,grid,rows_border,columns_border);
}
int main(){
int rows = 10;
int columns = 10;
// Hardcode the sizes here so the initializer { ... } is legal
int grid[10][10] = {
{5,5,5,5,5,5,5,5,5,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,0,0,0,0,0,0,0,0,5},
{5,5,5,5,5,5,5,5,5,5}
};
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if (grid[r][c] == 5) {
printf("\\ ");
} else if (grid[r][c] == 0){
printf(". ");
}
}
printf("\n");
}
int start_row_floodFill = 5;
int start_column_floodFill = 5;
draw_a_square(rows,columns,grid);
dfs(start_row_floodFill,start_column_floodFill,grid,rows,columns);
return 0;
}
I am trying to implement FloodFill algorithm inside a console, the problem I have faced is very strange. It has happened due to error in memory address, but I cannot understand why.
Code consists of 3 main functions:
mainfunction with 2D array -> nested for loops to draw it -> functiondraw_a_square()to again draw agrid[10][10]draw_a_square()is a function which again draws a squaredgriddfs()this function is which implements a FloodFill Algo, it should recursive fill all the points on agrid
My main problem is that when I watch this grid in VS Code I see it as a pointer *grid not as a complete structure as I can watch it inside a main()
Why does it happen ?
This is main function
This is draw_a_square()
Please refer your attention to upper left corner
This is how grid is shown inside a main function(as a 2D array)
This is how grid is shown inside draw_a_square() (as you see it is a list of parameters and a sub menu is shown as *grid )