Search Different Directions in 2d Array

I need help searching a matrix in multiple directions. I have a 4 x 4 matrix and I want to search for the word 'cat'. In this example 'cat' is in the puzzle twice one going South and one going Southwest.

BBBB
BBCB
BAAB
TBTB

I have another function that will check for any 'C' in the matrix and if it finds it then it will check the direction. If it finds the word, I want it to print out the location of the word and then I want it to return true or false due to the other functions that I have.

Anyway, when I go to run it, it's not working and I believe it's because of this function. Can someone give me advice on this? Is this correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool direction(grid[maxRows][maxCols]. string word, int rows, int columns, string dir, int dr, int dc)
{
   for (int i = 1; i < word.length(); i++)
   {
    if(word[i] == puzzle[rows][columns])
    {
       rows += dr;
       cols += dc;

       cout<<word<<" found at "<<row<<cols<<dir<<endl;
       return true;
        
    else
        return false;
   }

}
1. Start i at 0, not 1. This lets you test the first letter.
2. use a , not a . after grid[maxRows][maxCols]
3. Don't cout every time you found a matching letter. Save that for the end when you've confirmed that the whole word matches. Stick this outside of the for loop.
4. You have one more { than }.
5. You exit as soon as you found a first matching letter.

I like how you've implemented dr and dc, very nice. I assume that these are set outside of the function based on the direction.

This should help you out (your revised code with minimal changes):
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
bool direction(grid[maxRows][maxCols], string word, int rows, int columns, string dir, int dr, int dc)
{
	bool found = true;
	for (int i = 0; i < word.length(); i++)
	{
		if(word[i] == puzzle[rows][columns])
		{
			rows += dr;
			cols += dc;
		}
		else
		{
			found = false;
			break;
		}
	}

	if (found)
	{
		cout<<word<<" found at "<<row<<cols<<dir<<endl;
		return true;
	}

	return false;
}


Edit: Just simplified the function for you a bitL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool direction(grid[maxRows][maxCols], string word, int rows, int columns, string dir, int dr, int dc)
{
	for (int i = 0; i < word.length(); i++)
	{
		if(word[i] != puzzle[rows][columns])
			return false;

		rows += dr;
		cols += dc;
	}

	cout<<word<<" found at "<<row<<cols<<dir<<endl;
	return true;
}
Last edited on
I just thought, if your program is actually crashing, then it is likely because we aren't checking if we exceed the bounds of the array. If you don't do that in the calling function (which would be more difficult since we aren't going cell by cell there) then you have to do that here. It's a matter of adding one extra if condition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool direction(grid[maxRows][maxCols], string word, int rows, int columns, string dir, int dr, int dc)
{
	for (int i = 0; i < word.length(); i++)
	{
		if(rows < 0 || columns < 0 || rows > maxRows || columns > maxCols)
			return false;
		if(word[i] != puzzle[rows][columns])
			return false;

		rows += dr;
		columns += dc;
	}

	cout<<word<<" found at "<<row<<cols<<dir<<endl;
	return true;
}
Last edited on
Thank you this has helped tremendously!
Topic archived. No new replies allowed.