2D array word search solver needs to work in 8 directions

I need to make a word search program that takes in a txt file like this

15
N O L X Y H H J T R H H N F D
R X Q M F R W F M R G A D P J
X H B F V P A I A Q U L K H E
G S H V X H I L V U A M A W V
W O M A N W V T L N O N P S J
E L Y K F J Q E O I I O N A M
U F F L D G C D Z G H T Z V F
J P H Y A F V L O T F N O N E
A A S E W G Z T G C U I J K B
B U B Q R K D Q B G F L W O H
A C K N Y U F P I I W C Y M P
K S W Y B W P P V R O R B M Q
L Y O O H O H V S L C Q I O M
W N O F B E A S B Y Z O N S J
T E N I B A C N L R O C S F B
9
BOY
CABINET
CLINTON
DONALD
GIRL
HILLARY
MAN
TRUMP
WOMAN

I have the code for finding words from left to right. But i do not know how to write the code to search in other 7 directions (left to right, up, down, left-up diagonal, left-down diagonal, right-up diagonal & right-down diagonal directions) as well.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int MAX = 25;
char matrix[MAX][MAX], output[MAX][MAX];
string *words;
int numWords, n; //actual matrix is n * n size

void readData() {
ifstream finput("puzzle.txt");
finput >> n;
for(int i=0 ; i<n; i++)
for(int j=0 ; j<n; j++) {
finput >> matrix[i][j];
output[i][j] = ' ';
}

finput >> numWords;
words = new string [numWords];
for(int i=0 ; i<numWords; i++)
finput >> words[i];
}

bool searchRight(int si, int sj, string word) {
/*matrix[si][sj] matches word[0]?
matrix[si][sj+1] matches word[1]?
matrix[si][sj+2] matches word[2]?
... until end of string
then we found it!*/
if (sj+word.length() > n)
return false;

for(int i=0 ; i<word.length(); i++)
if (word[i] != matrix[si][sj+i])
return false;
//we have a match!
for(int i=0 ; i<word.length(); i++)
output[si][sj+i] = matrix[si][sj+i];
}

bool searchleft(int si, int sj, string word) {
/*matrix[si][sj] matches word[0]?
matrix[si][sj+1] matches word[1]?
matrix[si][sj+2] matches word[2]?
... until end of string
then we found it!*/
if (sj+word.length() > n)
return false;

for(int i=n ; i<word.length(); i++)
if (word[i] != matrix[si][sj+i])
return false;
//we have a match!
for(int i=n ; i<word.length(); i++)
output[si][sj+i] = matrix[si][sj+i];
}

void findWord(string word) {
//find the word in crossword puzzle matrix
// copy the word to output matrix
for(int i=0 ; i<n; i++)
for(int j=0 ; j<n; j++)
searchRight(i, j, word);

}

int main() {
readData();

for(int i=0 ; i<numWords; i++)
findWord(words[i]);

//output the array
for(int i=0 ; i<n; i++) {
for(int j=0 ; j<n; j++)
cout << output[i][j] << ' ';
cout << endl;
}

}
well, a diagonal for example is

array[row+n][row+n] //down and right

array[row+n][row-n] //down and left

down is
array[row+n][column]


does that help get you started?
I am actually really lost... first can someone explain to me how the code i posted works cause my professor gave it to us to modify to search the other 7 directions.
Topic archived. No new replies allowed.