Maze Solver (Right Hand rule)

Pages: 12
it didn't run, and what am i doing wrong? thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

int main(){

	const int SIZE = 12;
	int startX = 2;
	int startY = 0;
	const int endX = 4;
	const int endY = 11;
	int i,j;


	int maze[SIZE][SIZE]={
		{1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,1,0,0,0,0,0,0,1},
		{0,0,1,0,1,0,1,1,1,1,0,1},
		{1,1,1,0,1,0,0,0,0,1,0,1},
		{1,0,0,0,0,1,1,1,0,1,0,0},
		{1,1,1,1,0,1,0,1,0,1,0,1},
		{1,0,0,1,0,1,0,1,0,1,0,1},
		{1,1,0,1,0,1,0,1,0,1,0,1},
		{1,0,0,0,0,0,0,0,0,1,0,1},
		{1,1,1,1,1,1,0,1,1,1,0,1},
		{1,0,0,0,0,0,0,1,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1},
	};
	
	for(i=0 ; i< SIZE ; i++){
		for(j=0 ; j < SIZE ; j++){
			if(maze[i][j] == 1){cout << "#" ;}
			if(maze[i][j] == 0){cout << "." ;}
		}
		cout << endl;
	}


	while(startX != endX && startY != endY){
		//turn west
		if(maze[startX][startY-1]==0){
			maze[startX][startY]='X';}

		//turn south
		if(maze[startX-1][startY]==0){
			maze[startX][startY]='X';}

		//turn north
		if(maze[startX+1][startY]==0){
			maze[startX][startY]='X';}

		//turn east
		if(maze[startX][startY+1]==0){
			maze[startX][startY]='X';}

	}
	
	system("Pause");
}
I'm not sure what you mean but it doesn't run, but you will have an issue with running out of bounds on your array. What happens when you turn west and you're at the western most part of the maze? Your array will be -1 and that's not allowed. You need to test for the edge of the maze before you see what's to your side.

Also, system isn't a good habit to use.
The program supossed to find the way through the exit. And using the right hand rule, assume where the person inside the maze using his right hand to touch the wall and search the way out to the exit through the maze, and every way it has been walked should be marked as X. The alogrithm isn't finished yet. I have tried many ways to write the alogrithm ,but it wasn't work, once the program looped the way i didn't want to.
Your program is in an endless loop: You never change the values of startX or startY; therefore, your "while" statement is always true.

Put an output statement inside your loop and you will see that it just loops forever.
Already did that before. That's why i'm asking what am i doing wrong. The answer isn't helping
Cienjz, are you answering my post or "Volatile Pulse's" post?

Did you put an output statement inside your loop? Did you add code to change startX and startY?
here is my new code, my friend suggested me to use function. But i think there's something wrong with my declared function, it said that the function couldn't use the argument or whatever i couldn't understand, and i can't change the element in the array.

like maze[12][12], there is an element of 0 and 1, i want to change the element of 0 to 'X', if the way has been walked. Please give me a hint about the errors :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
using namespace std;

void checkpath(){

	int X,Y;
	int maze[12][12]={
		{1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,1,0,0,0,0,0,0,1},
		{0,0,1,0,1,0,1,1,1,1,0,1},
		{1,1,1,0,1,0,0,0,0,1,0,1},
		{1,0,0,0,0,1,1,1,0,1,0,0},
		{1,1,1,1,0,1,0,1,0,1,0,1},
		{1,0,0,1,0,1,0,1,0,1,0,1},
		{1,1,0,1,0,1,0,1,0,1,0,1},
		{1,0,0,0,0,0,0,0,0,1,0,1},
		{1,1,1,1,1,1,0,1,1,1,0,1},
		{1,0,0,0,0,0,0,1,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1},
	};


		//turn west
		if(maze[X][Y-1]==0){
			return true;}

		//turn north
		if(maze[X][Y-1]!=0 && maze[X+1][Y]!=0 && maze[X][Y+1]!=0 && maze[X-1][Y]==0){
			return true;}

		//turn south
		if(maze[X][Y-1]!=0 && maze[X+1][Y]==0){
			return true;}

		//turn east
		if(maze[X][Y-1]!=0 && maze[X+1][Y]!=0 && maze[X][Y+1]==0){
			return true;}
	

}


int findpath(){

	int X,Y;
	int maze[12][12]={
		{1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,1,0,0,0,0,0,0,1},
		{0,0,1,0,1,0,1,1,1,1,0,1},
		{1,1,1,0,1,0,0,0,0,1,0,1},
		{1,0,0,0,0,1,1,1,0,1,0,0},
		{1,1,1,1,0,1,0,1,0,1,0,1},
		{1,0,0,1,0,1,0,1,0,1,0,1},
		{1,1,0,1,0,1,0,1,0,1,0,1},
		{1,0,0,0,0,0,0,0,0,1,0,1},
		{1,1,1,1,1,1,0,1,1,1,0,1},
		{1,0,0,0,0,0,0,1,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1},
	};


		//turn west
		if(maze[X][Y-1]==0){
			return 1;}

		//turn north
		if(maze[X][Y-1]!=0 && maze[X+1][Y]!=0 && maze[X][Y+1]!=0 && maze[X-1][Y]==0){
			return 2;}

		//turn south
		if(maze[X][Y-1]!=0 && maze[X+1][Y]==0){
			return 3;}

		//turn east
		if(maze[X][Y-1]!=0 && maze[X+1][Y]!=0 && maze[X][Y+1]==0){
			return 4;}
}


int main(){

	const int SIZE = 12;
	int startX = 2;
	int startY = 0;
	int endX = 4;
	int endY = 11;
	int i,j;
	bool checkpath;
	int findpath;


	int maze[SIZE][SIZE]={
		{1,1,1,1,1,1,1,1,1,1,1,1},
		{1,0,0,0,1,0,0,0,0,0,0,1},
		{0,0,1,0,1,0,1,1,1,1,0,1},
		{1,1,1,0,1,0,0,0,0,1,0,1},
		{1,0,0,0,0,1,1,1,0,1,0,0},
		{1,1,1,1,0,1,0,1,0,1,0,1},
		{1,0,0,1,0,1,0,1,0,1,0,1},
		{1,1,0,1,0,1,0,1,0,1,0,1},
		{1,0,0,0,0,0,0,0,0,1,0,1},
		{1,1,1,1,1,1,0,1,1,1,0,1},
		{1,0,0,0,0,0,0,1,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1,1,1},
	};
	
	for(i=0 ; i< SIZE ; i++){
		for(j=0 ; j < SIZE ; j++){
			if(maze[i][j] == 1){cout << "#" ;}
			if(maze[i][j] == 0){cout << "." ;}
		}
		cout << endl;
	}



	while(startX<=endX && startY<=endY){

	if(startX>0 && checkpath()==true){
		if(findpath()==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath()==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath()==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath()==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startX<13 && checkpath()==true){
		if(findpath()==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath()==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath()==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath()==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startY>0 && checkpath()==true){
		if(findpath()==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath()==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath()==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath()==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startY<13 && checkpath()==true){
		if(findpath()==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath()==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath()==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath()==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}

	}

	
	system("Pause");
}
Here are some of the things that are wrong:

<code>
Your new functions do not take arguments.
In checkpath() you never initialize X or Y.
In checkpath() you do not handle the case if none of your turns are true!
In that case, who knows what you return!

You have defined checkpath as a void function which means return nothing, so this function won't even compile!

In findpath() you never initialize X or Y.
In findpath() you do not handle the case if none of your turns are true!
In that case, who knows what you return!

You have redefined your maze three times!
</code>

At your level, you need to define your maze once before all of your functions. Next, change your functions to pass in X and Y, like this:

1
2
3

int findpath( int X, int Y )


And call it like this:

1
2
3
4
5
6
7
8
9

if( findpath( startX, startY ) == 1 )
    {
    maze[ startX ][ startY ] = 'X';

    startY--;

    }


[/code]
How could i define 1 maze and can be used by all function? I tried using 1 defined maze in several function, but the program said the array is needed, so i added a maze to each local function. Do i have to use pointer or reference? And thanks for the answers.
How could i define 1 maze and can be used by all function?

You have two options here. The first is generally considered bad practice (more so later on in your programming career) and should be avoided. That's called a global variable, something declared/defined outside of the scope of any functions. The second option, and much more desired, is to pass the entire array as a variable. An example of each can be found below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// An example of a global variable
// The global variable can be used in any function or anywhere else in the program that you want.

#include <iostream>

using namespace std;

const int SIZE = 12; // a global variable
int maze[SIZE][SIZE]={ // also a global variable
	{1,1,1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,1,0,0,0,0,0,0,1},
	{0,0,1,0,1,0,1,1,1,1,0,1},
	{1,1,1,0,1,0,0,0,0,1,0,1},
	{1,0,0,0,0,1,1,1,0,1,0,0},
	{1,1,1,1,0,1,0,1,0,1,0,1},
	{1,0,0,1,0,1,0,1,0,1,0,1},
	{1,1,0,1,0,1,0,1,0,1,0,1},
	{1,0,0,0,0,0,0,0,0,1,0,1},
	{1,1,1,1,1,1,0,1,1,1,0,1},
	{1,0,0,0,0,0,0,1,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1,1,1},
};

int main() {
    // ...
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// An example of function variables

#include <iostream>

using namespace std;

const int SIZE = 12; // a global variable

void DisplayMaze(int maze[SIZE][]);

int main() {
    int maze[SIZE][SIZE] = // Declare/define maze here

    DisplayMaze(maze); // pass maze as a variable

    return 0;
}

void DisplayMaze(int maze[SIZE][]) {
    // write function to display the maze
}


Do i have to use pointer or reference?

Technically, an array is a pointer, and when passed, whatever is done to that array is done to the array elsewhere in the program. That should be more than enough to go you through. If you have any more questions, let me know.
Last edited on
Why void DisplayMaze(int maze[SIZE][]) , and not:
void DisplayMaze(int maze[SIZE][SIZE]) , or
void DisplayMaze(int maze[][SIZE]) , or
void DisplayMaze(int maze[][]) ,

And which one is better , using global variable declaration or function? I mean, what's the difference in program speed or efficiency. And i've seen some programs which declare functions as global variable, why should people do that?Thanks alot.
Last edited on
I'll leave the questions about which way to define DisplayMaze to Volatile Pulse.

As far as global verses functions, there are very many things to consider about which to use. The main problem with global variables is that because the variable is global, it can be changed more easily outside of functions you define. You want to always think of hiding the implementation (try Google for that definition.) Here I speak of when you start developing on a team and you as the designer of the program want to make other users use your functions.

For now, just make it global.
When it comes to passing an array to a function, you need to let the program you're passing an array, which is indicated by a pair of brackets, []. When passing a multidimensional array, you are required to specify all dimensions except for the final one, I believe. That's why I only specified the size of one dimension. You could, however, specify all dimensions if you prefer.

Edit: Also, in regards to performance, global variables hinder performance more so than local variables. Global variables remain on the stack for the length of the program, while local variables only remain for the length of the scope, that function only. Local variables are preferred over global not only for performance reasons, but also due to the appearance of code looking messy and the coder being lazy. As kooth said, for now, it doesn't matter, but in larger applications, it really does. Once I get home, I'll take a look at you program so far and see what's going wrong.
Last edited on
i have fixed the program, but it still shown 72 errors, and all of it is "function does not take arguments" and "'=' : no operator found which takes a right-hand operand of type 'char'". Please, it would be very kind if some one want to help me fix my error or tell me my wrong doing. :(:(:(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
using namespace std;

const int SIZE = 12;
	int maze[SIZE][SIZE]={
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
		{'#','.','.','.','#','.','.','.','.','.','.','#'},
		{'.','.','#','.','#','.','#','#','#','#','.','#'},
		{'#','#','#','.','#','.','.','.','.','#','.','#'},
		{'#','.','.','.','.','#','#','#','.','#','.','.'},
		{'#','#','#','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','#','.','#','.','#','.','#','.','#'},
		{'#','#','.','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','.','.','.','.','.','.','#','.','#'},
		{'#','#','#','#','#','#','.','#','#','#','.','#'},
		{'#','.','.','.','.','.','.','#','.','.','.','#'},
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
	};

int checkpath(){

	int X,Y;
		
		//turn west
		if(maze[X][Y-1]=='.'){
			return true;}

		//turn north
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]!='.' && maze[X-1][Y]=='.'){
			return true;}

		//turn south
		if(maze[X][Y-1]!='.' && maze[X+1][Y]=='.'){
			return true;}

		//turn east
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]=='.'){
			return true;}
	

}


int findpath(int x, int y){

	int x,y;

		//turn west
		if(maze[x][y-1]=='.'){
			return 1;}

		//turn north
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]!='.' && maze[x-1][y]=='.'){
			return 2;}

		//turn south
		if(maze[x][y-1]!='.' && maze[x+1][y]=='.'){
			return 3;}

		//turn east
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]=='.'){
			return 4;}
}


int main(){

	
	int startX = 2;
	int startY = 0;
	int endX = 4;
	int endY = 11;
	int i,j;
	bool checkpath;
	int findpath;

	
	
	for(i=0 ; i< SIZE ; i++){
		for(j=0 ; j < SIZE ; j++){
			if(maze[i][j] == 1){cout << "#" ;}
			if(maze[i][j] == 0){cout << "." ;}
		}
		cout << endl;
	}



	while(startX<=endX && startY<=endY){

	if(startX>0 && checkpath()==true){
		if(findpath(startX , startY)==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath(startX , startY)==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath(startX , startY)==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath(startX , startY)==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startX<13 && checkpath()==true){
		if(findpath(startX , startY)==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath(startX , startY)==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath(startX , startY)==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath(startX , startY)==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startY>0 && checkpath()==true){
		if(findpath(startX , startY)==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath(startX , startY)==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath(startX , startY)==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath(startX , startY)==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}
	if(startY<13 && checkpath()==true){
		if(findpath(startX , startY)==1){
			cout << maze[startX][startY]='X';
			startY-=1;
		}
		else if(findpath(startX , startY)==2){
			cout << maze[startX][startY]='X';
			startX-=1;
		}
		else if(findpath(startX , startY)==3){
			cout << maze[startX][startY]='X';
			startX+=1;
		}
		else if(findpath(startX , startY)==4){
			cout << maze[startX][startY]='X';
			startY+=1;
		}
	}

	}

	
	system("Pause");
}
Last edited on
Could you please post the errors so that we can see which lines are causing the problems?

One thing I see right off is this:

1
2
3

cout << maze[startX][startY]='X';


Are you trying to print out the value of the maze or assign 'X' to it?
1
2
cout << maze[startX][startY]='X';
			startX+=1;


it's exactly like you say, i'm trying to assign this 'X' element into maze[startX][startY], and then print out the element of assigned 'X'. And the next row, i'm trying to change startX/startY 's value for the next loop. Sorry for the inconvenience, because the error line is too many. It can't be post here, so i included download link.
http://www.mediafire.com/view/qpdcemrbmal89kg/New_Text_Document.txt
Well, don't do that. Make two different statements.

Sorry, my work will not let me view your post on mediafire. Can you post ONE error that relates to one line of your code?
Error 1 error C2082: redefinition of formal parameter 'x' c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 46 1 Project1
Error 2 error C2082: redefinition of formal parameter 'y' c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 46 1 Project1
3 error C2064: term does not evaluate to a function taking 0 arguments c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 91 1 Project1
Error 4 error C2064: term does not evaluate to a function taking 2 arguments c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 92 1 Project1

5~38
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'char' (or there is no acceptable conversion) c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 93 1 Project1
Error 6 error C2064: term does not evaluate to a function taking 2 arguments c:\users\cienjz\documents\visual studio 2012\projects\project1\project1\source.cpp 96 1 Project1
39 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 91 17 Project1
40 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 92 6 Project1
41 IntelliSense: no operator "=" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> = char c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 93 32 Project1

43~74
43 IntelliSense: no operator "=" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> = char c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 97 32 Project1
44 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 100 11 Project1
45 IntelliSense: no operator "=" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> = char c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 101 32 Project1
46 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 104 11 Project1
47 IntelliSense: no operator "=" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> = char c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 105 32 Project1
48 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 109 18 Project1
49 IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 110 6 Project1
50 IntelliSense: no operator "=" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> = char c:\Users\Cienjz\Documents\Visual Studio 2012\Projects\Project1\Project1\Source.cpp 111 32 Project1
how am i supposed to make two statements? sorry, i couldn'y really understand what you mean :(, thanks...

i guess what you mean is :

1
2
3
maze[startX][startY]='X';
cout << maze[startX][startY];
			startX+=1;


CMIIW
Last edited on
anyway i have a question, i tried to use a function, but there's some question that make me confuse.

this one, won't run. the program said, the function is error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;

const int SIZE=2;
void myArray(int array[SIZE][]);


int main(){
	
	
	int array[SIZE][SIZE]={{1,2},{2,3}};
	
	myArray(array);
	
}

void myArray(int array[SIZE][]){
	
	for(int i=0; i<2; i++){
		for(int j=0; j<2; j++){
			cout << array[i][j];
			
		}
		cout << endl;
	}
	
}



but this one is running well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;

const int SIZE=2;
void myArray(int array[SIZE][SIZE]);


int main(){
	
	
	int array[SIZE][SIZE]={{1,2},{2,3}};
	
	myArray(array);
	
}

void myArray(int array[SIZE][SIZE]){
	
	for(int i=0; i<2; i++){
		for(int j=0; j<2; j++){
			cout << array[i][j];
			
		}
		cout << endl;
	}
	
}


My question is, what make the difference between

myArray(int array[SIZE][]); and
myArray(int array[SIZE][SIZE]);

i thought, the first one will work.

Pages: 12