Need Help Finding an Error
Hey, I'm working on a program that creates a maze in a matrix array using classes. It compiles fine, but I get an error when I test run it. I used a template to setup the classes, but some of the stuff in there surpasses my understanding. I was wondering if any of you could help me find what is causing my program to crash when I test run it. Thanks
I'm assuming that the problem lies within the class that I'm using to actually setup the maze so I will just include the class.
I'm assuming that the problem lies within the class that I'm using to actually setup the maze so I will just include the class.
class mazeClass { private: protected: // Insert members which are private to this and child class objects here public: // the default constructor mazeClass(); // the initialization constructor mazeClass(const mazeClass &); ~mazeClass(); mazeClass & operator = ( const mazeClass & ); mazeClass( ) { } int mazeGenerate ( int n, int startx, int starty, int endx, int endy ) { int m[n][n]; // default each element of the maze to 0 for ( int i = 0; i < n; i++ ) { for ( int j = 0; j < n; j++ ) { m[i][j] = 0; } } // set the bounderies of the maze // top of maze for ( int i = 0; i < n; i++ ) { m[0][i] = 1; } // left side for ( int i = 0; i < n; i++ ) { m[i][0] = 1; } // right side for ( int i = 0; i < n; i++ ) { m[i][n] = 1; } // bottom for ( int i = 0; i < n; i++ ) { m[n][i] = 1; } // set start and endpoints to 0 to open them m[startx][starty] = 0; m[endx][endy] = 0; // print maze ofstream fout("graph.txt", ios::out); for ( int i = 0; i < n; i++ ) { for ( int j = 0; j < n; j++ ) { if ( m[i][j] == 1 ) { fout << " . " << flush; if ( n == j - 1 ) { fout << endl; } } else { fout << "#" << flush; } if ( n == j - 1 ) { fout << endl; } } } } };
0
Comments
There were a couple of errors, I've reformatted the code slightly to make it easier to read, and pointed out the problems where they were. There's still slight issues with the actual generation but I leave that to you so solve, after all, it's fun right
The main crash that you were probably experiencing (given the laxity of some compilers, you may have been able to get away with the other errors) is marked **** Problem ****
If your array has 20 elements they are 0 - 19, not 1 - 20, you were trying to access element 20...
M
[PHP]
class mazeClass {
private:
protected:
public:
// Problem - multiple constructors
// the default constructor
mazeClass()
{
// Nothing here
}
//mazeClass( ) {
//}
// "Problem" - Seemingly unnescessary given the scope of the class
// the initialization constructor
// mazeClass(const mazeClass &);
// Problem - no body for the destructor, you may have this elsewhere but you didnt specify that...
~mazeClass()
{
// Nothing here
}
mazeClass & operator = ( const mazeClass & );
int mazeGenerate ( int n, int startx, int starty, int endx, int endy )
{
// Counters
int i, j;
// Problem 2 - cannont initialize array with non constant values
// int m[n][n];
// Instead - M = "pointer to pointer"
int** m;
// Allocate the "array of arrays"
m = new int*[n];
// And now each member of the above
for (i = 0; i < n; i++)
m = new int[n];
// default each element of the maze to 0
for (i = 0; i < n; i++ )
{
for ( int j = 0; j < n; j++ )
{
m[j] = 0;
}
}
// set the bounderies of the maze
// top of maze
for (i = 0; i < n; i++ )
{
m[0] = 1;
}
// left side
for (i = 0; i < n; i++ )
{
m[0] = 1;
}
// right side
for (i = 0; i < n; i++ )
{
m[n] = 1;
}
// bottom
for (i = 0; i < n; i++ )
{
// **** Problem **** - You cant access n
// m[n] = 1;
m[n-1] = 1;
}
// set start and endpoints to 0 to open them
m[startx][starty] = 0;
m[endx][endy] = 0;
// print maze
ofstream fout("graph.txt", ios::out);
for (i = 0; i < n; i++ )
{
for (j = 0; j < n; j++ )
{
if ( m[j] == 1 )
{
fout << " . " << flush;
if ( n == j - 1 )
{
fout << endl;
}
}
else
{
fout << "#" << flush;
}
if ( n == j - 1 )
{
fout << endl;
}
}
}
// Problem (not really) - you should return if your method type is int
return 1;
}
};
[/PHP]
So after I use:
I can use m[][] ?
(I'm trying to setup a matrix by using m[][] - I was assuming that it would set it up as a matrix setup in rows and columns)
something like that or
http://mathbits.com/mathbits/compsci/Arrays/Matrix.htm
at the very bottom
Since you have non-constants, what you have to do is allocate the memory yourself, just remember that a 2D array, say m[3][4], can be described as an array with 3 members, each member being an array with 4 members - ie, an array of arrays:
heres the same code but with slightly better naming etc:
[PHP]
int i;
int** array2D;
int ROWS = 3;
int COLUMNS = 4;
// Allocate the rows array (3 main arrays)
array2D = new int*[ROWS];
// Allocate each of the arrays in the main array
for (i = 0; i < ROWS; i++)
array2D = new int
// Do something, we can now access array2D as array2D[][]
array2D[0][0] = 0;
// Dont forget deallocate it afterwards, this is done in reverse order
for (i = 0; i < ROWS; i++)
delete[] array2D;
delete[] array2D;
[/PHP]
C++
the pointers (*) are a dead giveaway.
in java it would be
to get 0-3
[PHP]Random rand = new Random();
int x = rand.nextInt(4);[/PHP]
and 1-4 would be:
[PHP]Random rand = new Random();
int x = rand.nextInt(4)+1;[/PHP]
Sure is, to get a number in a specified range use:
rand()%upper_limit ... remember that it starts at 0 though ...
so for 0->3 (4 numbers in range): rand()%4;
if you wanted 10->20: rand()%10 + 10
etc
M