View the step-by-step solution to:

# nwlrb bmqbh cdar z owkky hiddq Enter the word to search for : dark . Program finished with exit code Press ENTER to exit console.

This code makes a 5 by 5 gird and asks user to enter a word, but it does not output if the word exists in the grid or not. I need help coding that function to output if the word is found in the grid or not. Here is my sample output: The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board:

Q W E R T

A S D F G

Z X C V B

Y U A O P

G H J K L

contains the magic words WAS, WAXY, JOB, and others, but not the word BOX.

```#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int n;
char grid  ;
int z;
int j;

bool dfs(char grid, bool visited, string word, int ind, int x, int y){
//This means we have reached the end of word
if(word[ind] == '') return true;

if(x<0 || x>=n) return false;

if(y<0 || y>=n) return false;

if(grid[x][y]!= word[ind]) return false;

if(visited[x][y]) return false;

visited[x][y] = true;

bool success = false;

for(int i=x-1; i<=x+1; i++){
for(int k = y-1; y<=y+1;k++){

success =dfs(grid, visited, word,ind+1,i,k);
if (success) break;
}
if (success) break;

}

visited[x][y] = false;

return success;
}

int main()
{

bool found = false;
string word;
// declare an array with alphabet
char alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };

bool visited;
char grid;
n=5;
for (z = 0; z < n; z++)
{
for (j=0; j < n; j++)
{
// print letters
grid [z] [j] = alphabet[rand() % 26];
cout<<grid [z] [j]<<" ";
}
cout<<endl;
}

//The visited array is the make sure there are no duplicate letters from the grid
//visited = new bool*[n];
for(z=0;z<n;z++){
// visited[z] = new bool[n];
for(j=0;j<n;j++){
visited[z][j] = false;
}
}

cout<<"Enter the word to search for: ";
cin>>word;

for (z = 0; z < n; z++)
{
for (j=0; j < n; j++)
{
if(grid[z][j] == word){
visited[z][j] = true;
found = dfs(grid,visited,word,0, z,j);
visited[z][j] = false;
}
if(found) break;
}
if(found) break;
}
}
``` nwlrb
bmqbh
cdar z
owkky
hiddq
Enter the word to search for : dark
. Program finished with exit code
Press ENTER to exit console.

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents
• ### -

Question & Answers

Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

Ask a Question