### Iterating through 2 2D vectors, encountering syntax errors

Greetings! Conceptually, I believe I have this down well enough. I'm comparing letters in a string vector to letters in a word search puzzle and seeing if they match for the length of the word. Try as I might I can't get things to work properly for the words in the right direction only. Here is my function that searches for potential words. Ideally, it would find all the words that are from left to right, horizontally.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````void potentialWords(const vector > & puzzle, const vector & wordList, vector & wordsFound) { vector wordFound(wordList.size(), true); for (int h = 0; h < wordList.size(); h++) for (int i = 0; i < wordList.at(h).size(); i++) { for (int j = 0; j < puzzle.size(); j++) for (int k = 0; k < puzzle.at(0).size(); k++) { if (wordList.at(h).at(i) == puzzle.at(j).at(k)) while (i + wordList.at(h).size() < puzzle.at(0).size()) { for ( int m = 0; m < wordList.at(h).size(); m++) if (wordList.at(h).at(i+m) !=puzzle.at(j).at(k)) wordFound.at(h) = false; break; } break; } break; } for (int i = 0; i < wordFound.size(); i++) { if (wordFound.at(i) == true) cout << wordList.at(i) << endl; } };``````

As it sits right now, I know it doesn't work. I go into an infinite loop. I threw those breaks in there haphazardly just to try to get something out. I think the problem I'm having is that it falls out of bounds if keeps trying to increment m past the range of the puzzle row, and that screws it up. I'm not sure what to put as the parameter of a while loop to fix it though, if that's even my problem. Any ideas? Just for full exposure, this was for a previous assignment that I want to fix after the turn in date for no additional credit. I don't gain anything from this but knowledge.
Last edited on
Here is my suggestion. You change this around so that you scan the puzzle, one letter at a time. Each letter, you check the first letter of each word. If it's the same, check To see if it's even possible if the word can go left or right (do this by checking the number of characters left in the row or the number of characters before the the current one and compare the length of the word minus 1). If it's plausible the word exists, compare the string against the letters of n length in the correct direction. If it is equal, you found the word.

If you need some code, let me know.
Thank you for the suggestion, and I will try to implement your idea right now.

So just to turn it into words briefly:

When the position in the puzzle matches the first letter in one of the words:

If the length of the puzzle row - the position in the row where the letters match is greater than the length of the word, its possible that that word exists in that row. Did I word that correctly?

This is what I changed it to:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````void potentialWords(const vector > & puzzle, const vector & wordList, vector & wordsFound) { vector wordFound(wordList.size(), true); for (int j = 0; j < puzzle.size(); j++) for (int k = 0; k < puzzle.at(0).size(); k++) { for (int h = 0; h < wordList.size(); h++) for (int i = 0; i < wordList.at(h).size(); i++) { while (wordList.at(h).at(i) == puzzle.at(j).at(k)) { if(puzzle.at(0).size() - k > wordList.at(h).size()) for (int m = 0; m < wordList.at(h).size(); m++) if (puzzle.at(j).at(k+m) != wordList.at(h).at(m)) wordFound.at(h) = false; break; } } } for (int i = 0; i < wordFound.size(); i++) { if (wordFound.at(i) == true) cout << wordList.at(i) << endl; } };``````

It compiles and runs, but I'm getting 'great' 'duper' and 'tubular' from this puzzle:

CCCCCCCKEVIN
DDDDDDDDDDDD
SWEETTTTTTTT
AWESOMEEEEEE
sickwwwwwwww

KEVIN
SWEET
AWESOME
cool
great
wowwie
super
duper
tubular

In my innermost for loop, am I implementing 'm' properly? I think that's where my problem is.
Last edited on
Really brief psuedo code:
 ``12345678910111213`` ``````for (loop puzzle rows) // row for (loop puzzle cols) // col for (loop words) // word if (puzzle letter == the first letter of each word) { if (row width - location of letter in row >= length of word) // word can be to the right for ( loop the letters of the word) // letter if ( puzzle letter != word letter) break; else if (puzzle letter == word letter && last letter) word found! if (location of letter in row - length of word >= 0) // word can be to the right // do same as above make sure you decrement the puzzle letters though }``````

It doesn't look much different, but I suggest at least renaming your variables so that they are easier to identify. Also, do you know you can access vectors the same way as arrays with the [n] operator? The .at(n) really threw me off for a while.

The great thing with doing it this way, you can easily add vertical detection and diagonal detection
Last edited on
Topic archived. No new replies allowed.