Need Help Finding an Error

edited December 2006 in Internet & Media
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.
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;  
                      
	  }       
          
      }    
    
    }

  }

};

Comments

  • mondimondi Icrontian
    edited November 2006
    Hi there,

    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]
  • edited November 2006
    My program actually ran without crashing when I changed what you were suggesting.

    So after I use:
       // Allocate the "array of arrays"
            m = new int*[n];
    
            // And now each member of the above
            for (i = 0; i < n; i++)
                m[i] = new int[n]; 
    
    

    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
  • mondimondi Icrontian
    edited November 2006
    Yes, what that little section of code does is exactly what m[x][y] does. But you cannot use the static definition since ou cannot allocate an array using non-constant variables (or you shouldn't).

    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]
  • airbornflghtairbornflght Houston, TX Icrontian
    edited November 2006
    is this C++ or Java, cause it looks close to java, but there are some things that look a little foreign.
  • mondimondi Icrontian
    edited November 2006
    is this C++ or Java, cause it looks close to java, but there are some things that look a little foreign.

    C++
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited November 2006
    is this C++ or Java, cause it looks close to java, but there are some things that look a little foreign.

    the pointers (*) are a dead giveaway.
  • edited November 2006
    Hey, thanks alot mondi. If I have anymore questions, I'll be sure to ask.
  • edited November 2006
    Is it possible to use rand() and have it only select random numbers within the range of 0 to 3 or 1 to 4 ?
  • airbornflghtairbornflght Houston, TX Icrontian
    edited November 2006
    it is in java, but I cant really speak for C++ though I bet you it is.

    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]
  • mondimondi Icrontian
    edited November 2006
    bustacrab wrote:
    Is it possible to use rand() and have it only select random numbers within the range of 0 to 3 or 1 to 4 ?

    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
  • edited December 2006
    thanks for the help
Sign In or Register to comment.