| | |
Help With this program
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
My teacher wrote this code.
i am having trouble understanding what is going on in the code.
can someone please help me by commenting in the program
if you can tell me the importance of the functions and how everything works.
please help me.... i'm a noob programmer
#include <iostream>
using namespace std;
int col = 0;
int row = 1;
const int ROWMAX = 12;
const int COLMAX = 12;
char maze[ROWMAX][COLMAX] =
{
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
{' ',' ','#',' ','#',' ','#','#','#','#',' ','#'},
{'#','#','#',' ','#',' ',' ',' ',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#','#','#',' ','#',' ',' '},
{'#','#','#','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#','#',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
{'#','#','#','#','#','#',' ','#','#','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};
void printMaze();
void mazeTraverse(int, int);
void printMaze()
{
for(int row = 0; row < ROWMAX; row++)
{
for(int col=0; col < COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void mazeTraverse(int row, int col)
{
if( (row>0 && row<ROWMAX) && (col>=0 && col<COLMAX)) {
if( maze[row][col] == ' ') {
maze[row][col]='*';
mazeTraverse(row, col+1);
mazeTraverse(row, col-1);
mazeTraverse(row-1, col);
mazeTraverse(row+1, col);
}
}
}
int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
mazeTraverse(1, 2);
printMaze();
return 0;
}
i am having trouble understanding what is going on in the code.
can someone please help me by commenting in the program
if you can tell me the importance of the functions and how everything works.
please help me.... i'm a noob programmer
#include <iostream>
using namespace std;
int col = 0;
int row = 1;
const int ROWMAX = 12;
const int COLMAX = 12;
char maze[ROWMAX][COLMAX] =
{
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#'},
{' ',' ','#',' ','#',' ','#','#','#','#',' ','#'},
{'#','#','#',' ','#',' ',' ',' ',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#','#','#',' ','#',' ',' '},
{'#','#','#','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#','#',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#'},
{'#','#','#','#','#','#',' ','#','#','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};
void printMaze();
void mazeTraverse(int, int);
void printMaze()
{
for(int row = 0; row < ROWMAX; row++)
{
for(int col=0; col < COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void mazeTraverse(int row, int col)
{
if( (row>0 && row<ROWMAX) && (col>=0 && col<COLMAX)) {
if( maze[row][col] == ' ') {
maze[row][col]='*';
mazeTraverse(row, col+1);
mazeTraverse(row, col-1);
mazeTraverse(row-1, col);
mazeTraverse(row+1, col);
}
}
}
int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
mazeTraverse(1, 2);
printMaze();
return 0;
}
The function mazeTraverse() is pretty much this whole program. It checks to see if the position in the maze that is it at is a space and then if it is it runs its self 4 items (up one spot, down one, left one, right one) and doing the same checks as before and if those spots are spaces then it runs its self another 4 times. This happens until it comes to a dead end.
Not sure if that helps it took me a few secs to look at the function to figure out what it was doing.
Not sure if that helps it took me a few secs to look at the function to figure out what it was doing.
•
•
Join Date: Jul 2005
Posts: 1,705
Reputation:
Solved Threads: 274
Declare a maze that will consist a 12 by 12 board represented internally by a 2D char array. The characters in the array will be # to indicate walls, space char to indicate available choices (unvisited cells) and * to indicate that we've been here before and can't go there again from another cell (though we will go back to each cell we've visited that initially contained a space until we've looked in all four directions from that cell). There will be a solid wall around the maze except for two possible exits. You will start at a predetermined cell given by the program.
The program will look at the maze using a recursive function of type void taking current row and column indexes as parameters. Each call to the funciton will look at a single cell. If the cell is within the maze and if the cell currently contains a space char then the function will place an asterix in the current cell and will then use a protocol of evaluating neighbors of the current cell by looking up before down before left before right.
The output of the program will be a picture of the maze before the recursive function is called and a picture of the maze after the recursive function has looked at all the cells it can.
In the picture of the maze after the recursive function has ended if there is an asterix in one or both of the cells in the external wall of the maze then the maze is solvable; if not, then it's not.
Note that the program is not designed to give a specific solution, or the shortest solution, if there is one. It will just determine if one is possible.
Also note that the program will not stop once a solution is found.
A recursive function is one that "calls itself". It may create a never ending loop so a gaurantee that it will stop must be built in to prevent the program from locking up.
The program will look at the maze using a recursive function of type void taking current row and column indexes as parameters. Each call to the funciton will look at a single cell. If the cell is within the maze and if the cell currently contains a space char then the function will place an asterix in the current cell and will then use a protocol of evaluating neighbors of the current cell by looking up before down before left before right.
The output of the program will be a picture of the maze before the recursive function is called and a picture of the maze after the recursive function has looked at all the cells it can.
In the picture of the maze after the recursive function has ended if there is an asterix in one or both of the cells in the external wall of the maze then the maze is solvable; if not, then it's not.
Note that the program is not designed to give a specific solution, or the shortest solution, if there is one. It will just determine if one is possible.
Also note that the program will not stop once a solution is found.
A recursive function is one that "calls itself". It may create a never ending loop so a gaurantee that it will stop must be built in to prevent the program from locking up.
Klatu Barada Nikto
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Help with a program (Java)
- Add compression to my program (C++)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: initialise value in linklist
- Next Thread: list iterator not dereferencable
| Thread Tools | Search this Thread |
.dll 3d api array arrays asp based binary bitmap c# c++ calculator char char* class commentinghelp compile compiler compression console crashcourse delayload desktop development download email embedded encryption engine equation error evc exam examples faq file floatingpoint fstream function functions game graph guessing gui int introduction java jni keyboard linker lnk2019 math maze microsoft multidimensional music mysql newbie opengl output pause primenumbersinrange problem professor program programing programmer programming projects python qt read recursion recursive regqueryvalueex richedit rpg send set snakes stream string studio subclass temperature template test text timer traverse tree university url variable vector visual visualization visualstudio volume win32







