# Thread: Question comes from drawing a maze.

1. ## Question comes from drawing a maze.

hi all,
i write this snippet of code to randomly draw a maze. when ROW is defined less then 12, it works well. but when ROW >12, the result truns out to be weired.(the "wall" around the maze is broken :( )
eg. (PS: 'O' DENOTES SPACE)
right form:
XXXXXXX
XOXOXOX
XXOXOXX
XXXXXXX
undesired form: (when row >12)
XXXXXXXoXXXX
XXOOOOOXXO
XOOOOXXXXX
XX OXOOXX
OXXXOOXXXXX
the "wall" is broken...what happened?
it does sth. with my complier or .......plz help me
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 10
#define COLUMN 10

int main()
{
int draw_maze();

draw_maze();

getchar();
return 0;
}

int draw_maze()
{
int maze[ROW][COLUMN]={0};
int i,j;

for(i=1; i<(COLUMN-1); i++) //clear inner space
for(j=1; j<(ROW-1); j++)
maze[i][j]=1;

srand((unsigned)time(NULL));
for(i=1; i<COLUMN-1; i++)     //randomly set blocks
for(j=1; j<ROW-1; j++)
maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1

for(i=0; i<COLUMN; i++)
{
for(j=0; j<ROW; j++)
if(maze[i][j]==0)
printf("x");
else
printf(" ");
printf("\n");
}
}```

2. I got a bunch of compile errors, so I fixed it, look here for what I edited:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 10
#define COLUMN 10

int main(void)
{
int draw_maze();

draw_maze();

getchar();
return 0;
}

int draw_maze()
{
int maze[ROW][COLUMN]={{0}};
int i,j;

for(i=1; i<(COLUMN-1); i++) //clear inner space
for(j=1; j<(ROW-1); j++)
maze[i][j]=1;

srand((unsigned)time(NULL));
for(i=1; i<COLUMN-1; i++)     //randomly set blocks
for(j=1; j<ROW-1; j++)
maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1

for(i=0; i<COLUMN; i++)
{
for(j=0; j<ROW; j++)
if(maze[i][j]==0)
printf("x");
else
printf(" ");
printf("\n");
}
return 0;
}```

3. I don't know what you mean by specifying row greater than 12, so I can't check that.

4. Ah nvm, I get what you meant now, and it works fine for me when I specify Row greater than 12, so check what I fixed, doesn't your compiler give you errors when you compile?

5. ## oh,no...

thanks for you re-edit.
i've tried both version in dev-c++, vc2005, and code::blocks,and i got no complier warning,
and your's still can't perform correctly when row>15 in my machine...oh,my god

6. Here is what i get when i can compile your program. The warning messages
Code:
```gcc -Wall -W strtol.c
strtol.c: In function &#226;draw_maze&#226;:
strtol.c:20: warning: missing braces around initializer
strtol.c:20: warning: (near initialization for &#226;maze[0]&#226;)
strtol.c:41: warning: control reaches end of non-void function```
ssharish

7. Originally Posted by albert3721
thanks for you re-edit.
i've tried both version in dev-c++, vc2005, and code::blocks,and i got no complier warning,
and your's still can't perform correctly when row>15 in my machine...oh,my god

I do not know what's happening because I set rows to over 20 and it seems to be fine, make sure you have edited it all exactly and not missed anything out.

What command do you use for compiling, maybe you haven't invoked the warning flags.

8. ## found it!

Code:
`maze[ROW][COLUMN]`
it should be
Code:
`maze[COLUMN][ROW]`
so , it shouldn't have worked well with your complier, i think.
thanks.

9. Originally Posted by albert3721
Code:
`maze[ROW][COLUMN]`
it should be
Code:
`maze[COLUMN][ROW]`
so , it shouldn't have worked well with your complier, i think.
thanks.

You have got your columns and rows mixed up. WHen I increase rows, it's actually the columns that are increasing.

10. This is what the code should be, you do not need to swap the ROWS and COLUMNS in the line you indicated, but you do need to for all the other lines.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 20
#define COLUMN 10

int main(void)
{
int draw_maze();

draw_maze();

getchar();
return 0;
}

int draw_maze()
{
int maze[ROW][COLUMN]={{0}};
int i,j;

for(i=1; i<(ROW-1); i++) //clear inner space
for(j=1; j<(COLUMN-1); j++)
maze[i][j]=1;

srand((unsigned)time(NULL));
for(i=1; i<ROW-1; i++)     //randomly set blocks
for(j=1; j<COLUMN-1; j++)
maze[i][j] = rand()/(RAND_MAX/2);  // maze[i][j] = 0 or 1

for(i=0; i<ROW; i++)
{
for(j=0; j<COLUMN; j++)
if(maze[i][j]==0)
printf("x");
else
printf(" ");
printf("\n");
}
return 0;
}```
Incorrectly doing this, will probably lead to a segmentation fault.

Now this should be all correct