2D Array Passing to function - only reads first line
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: 2D Array Passing to function - only reads first line

  1. #1
    Join Date
    Dec 2010
    Posts
    18

    2D Array Passing to function - only reads first line

    Hi guys,
    Hoping someone has some insight to what my issue is. Any help would be greatly appreciated.

    I have an array of char 4x4 that is being passed into a function.
    When I create the array with my own letters (debugging purposes), everything works like a champ. The function sees the entire 2d array.
    When I create the array with random letters, the array is created fine with random letters but the function only takes in the first 4 characters a[0][0] to a[0][3].

    The only difference in my code, is the array is set as static when debugging.

    Here are the relevant bits of code. If you need to see more to clarify, please let me know.

    h
    Code:
    class boggleGame  {
    public:
    	boggleGame(bool debug);
    	void printBoggle();
    	//char letters[SIZE][SIZE];
    	static char letters[SIZE][SIZE]; //for debug
    
    	friend class boggleCheater; //so solver can access letters[][]
    };
    
    class boggleCheater {
    public:
    	boggleCheater();
    	void solve (int size, boggleGame board, ifstream &words);
    	
    private:
    	bool matchWord(const string &word, int row, int col, int index, int activeLetters, char board[][SIZE] );
    };

    cpp
    Code:
    boggleGame::boggleGame(bool debug) {
    	if (debug == false)
    	{
    		int row, col;
    		unsigned seed = (unsigned) time (0);
    		srand (seed);
    		for (row = 0; row < SIZE; row++) {     
    			for (col = 0; col < SIZE; col++) {
    				letters[row][col] = rand () % 26 + 'a';
    			}
    		}
    
    	}
    }
    
    //FOR DEBUG ONLY
    char boggleGame::letters[4][4] =
    {
    	{'y','o','x','f'},
    	{'r','b','a','k'},
    	{'v','e','d','i'},
    	{'u','y','t','r'}
    };
    
    void boggleCheater::solve(int size, boggleGame board, ifstream &words) {
    	string word;
    
    	//go through word in wordlist
    	while (!words.eof())
    	{
    		getline (words, word);
    		//kick out if not 3 or more letters or more than 17 (16 unless you have Q/Qu so 17)
    		if ((word.length() < 3 ) || (word.length() > 17)) continue;
    
    		for (int row = 0; row < 4; row++)
    			for (int col = 0; col < 4; col++)
    				if (board.letters[ row ][ col ] == word[ 0 ])
    					if (matchWord( word, row, col, (word[ 0 ] == 'q') ? 2 : 1, (1 << ((row *4) +col)), board.letters ))
    						cout << word << '\n'; //output the word
    	}
    
    }
    its this matchWord function that doesnt see the entire 2d array (board.letters).
    Code:
    bool boggleCheater::matchWord(const string &word, int row, int col, int index, int activeLetters, char board[][SIZE]) {
    	if (index == word.length()) return true; 
    	for (int r = -1; r < 2; r++)
    		for (int c = -1; c < 2; c++)
    		{
    			if (((row +r) < 0) || ((row +r) > 3) || ((col +c) < 0) || ((col +c) > 3)) continue;
    		
    			int mask = 1 << (((row +r) *4) +col +c);
    			if (((activeLetters & mask) == 0) &&  (board[ row +r ][ col +c ] == word[ index ]))
    				if (matchWord( word, row +r, col +c, (word[ index ] == 'q') ? index +2 : index +1, activeLetters | mask, board ))
    					return true;
    		}
    		return false;
    }

    so when im not using my static array with pre-determined letters, the array is created when you make a boggleGame object.

    any ideas why?
    Thanks!!
    =G

  2. #2
    Join Date
    Mar 2006
    Posts
    139

    Re: 2D Array Passing to function - only reads first line

    The problem is likely the way you have declared the solve member:

    Code:
    void boggleCheater::solve(int size, boggleGame board, ifstream &words) {
    You've got "boggleGame board" for the board argument. That means when this function is called the board object is copied. If you haven't defined a copy constructor/assignment operator which copies the contents of the "letters" member, the copy which "solve" is operating on will contain undefined data. Instead you probably want to pass the board in through a pointer (in addition to defining an assignment operator and copy constructor):

    Code:
    void boggleCheater::solve(int size, boggleGame * pBoard, ifstream &words) {
    It works when "letters" is static because both class instances use the same (static) member.

    Hope this helps!

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: 2D Array Passing to function - only reads first line

    Quote Originally Posted by GeoRanger View Post
    Code:
    void boggleCheater::solve(int size, boggleGame * pBoard, ifstream &words) {
    As it is C++ you better use a reference but a pointer:

    Code:
    void boggleCheater::solve(int size, boggleGame & board, ifstream &words)
    Passing by reference means that the changes you do on board will happen in the original board variable defined by the caller thus the values were *returned*.

    Same happens with pointer but pointer access is more error-prone and has no advantage in that case.

    The static means that for all boggleGame instances you have the same letters array. That only may make sense if you never has two or more instances created but actually is a flaw in your current design.

  4. #4
    Join Date
    Dec 2010
    Posts
    18

    Re: 2D Array Passing to function - only reads first line

    Thanks for the help guys!!
    The whole copy constructor issue really shed some light as to why I was only getting the first row.
    I ended up passing by reference, and it works properly now.

    The whole static variable thing was just for testing. The solver will never have more than one board, so it made sense at the time, but I am actually going to remove it from the code.

    Thanks again for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center