Question: I can't get this C + + code to provide output. The requirements are as follows: 1 . ) Break up the problem into smaller

I can't get this C++ code to provide output. The requirements are as follows:
1.) Break up the problem into smaller functions and get each one working before going on to the next. Concentrate first on how to build the two-dimensional array.
2.) Make sure your output shows the 8 by 8 array just like the sample above
My code is listed below and a screenshot of the input file conent is attached.
#include
#include
#include
#include
const int SIZE =8;
void readTable(std::ifstream& file, char table[SIZE][SIZE]){
for (int i =0; i SIZE; ++i){
for (int j =0; j SIZE; ++j){
file >> table[i][j];
}
}
}
bool searchWord(const char table[SIZE][SIZE], const std::string& word, int& row, int& col, std::string& direction){
int wordLength = word.length();
// Horizontal search
for (int i =0; i SIZE; ++i){
for (int j =0; j = SIZE - wordLength; ++j){
bool found = true;
for (int k =0; k wordLength; ++k){
if (table[i][j + k]!= word[k]){
found = false;
break;i
}
}
if (found){
row = i;
col = j;
direction = "Horizontal";
return true;
}
}
}
// Vertical search
for (int i =0; i = SIZE - wordLength; ++i){
for (int j =0; j SIZE; ++j){
bool found = true;
for (int k =0; k wordLength; ++k){
if (table[i + k][j]!= word[k]){
found = false;
break;
}
}
if (found){
row = i;
col = j;
direction = "Vertical";
return true;
}
}
}
// Diagonal (top-left) search
for (int i =0; i = SIZE - wordLength; ++i){
for (int j =0; j = SIZE - wordLength; ++j){
bool found = true;
for (int k =0; k wordLength; ++k){
if (table[i + k][j + k]!= word[k]){
found = false;
break;
}
}
if (found){
row = i;
col = j;
direction = "Diagonal";
return true;
}
}
}
return false;
}
void printResults(const std::vector& words, const std::vector>& positions, const std::vector& directions){
std::cout "Word\tRow\tColumn\tDirection
";
for (size_t i =0; i words.size(); ++i){
std::cout words[i]"\t" positions[i].first "\t" positions[i].second "\t" directions[i]"
";
}
}
int main(){
std::ifstream inputFile("input.txt");
if (!inputFile){
std::cerr "Error: Unable to open input file.
";
return 1;
}
char table[SIZE][SIZE];
readTable(inputFile, table);
std::vector words;
std::string word;
while (inputFile >> word){
words.push_back(word);
}
std::vector directions;
std::vector> positions;
for (const auto& word : words){
int row, col;
std::string direction;
if (searchWord(table, word, row, col, direction)){
positions.push_back(std::make_pair(row, col));
directions.push_back(direction);
}
}
if (positions.empty()){
std::cout "Not found
";
} else {
printResults(words, positions, directions);
}
return 0;
}
I can't get this C + + code to provide output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!