Question

Need help in c++ programming to output the lines in my code: if word if found:...

Need help in c++ programming to output the lines in my code:

if word if found:

           cout << "'" << word << "' was found in the grid" << endl;

           cout << "'" << word << "' was not found in the grid" << endl;

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.

My code makes a grid and asks user to enter a word but does not output if the word exists in the grid or not. I need help to add the above lines in my code so when user enters a word the program checks if the word is found in the grid or not.

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

using namespace std;

int n;
char grid [10] [10];
int z;
int j;

bool dfs(char grid[5][5], bool visited[5][5], string word, int ind, int x, int y){
    //This means we have reached the end of word
    if(word[ind] == '\0') 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[26] = { '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[5][5];
    char grid[5][5];
    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[0]){
                visited[z][j] = true;
                found = dfs(grid,visited,word,0, z,j);
                visited[z][j] = false;
            }
            if(found) break;
        }
        if(found) break;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int n;
char grid [10][10];
int z;
int j;

bool dfs(char grid[5][5], bool visited[5][5], string word, int ind, int x, int y){
    //This means we have reached the end of word
    if(word[ind] == '\0') 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; k<=y+1;k++){
                        
                if(!(i == x && k == y)) {
                        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[26] = { '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[5][5];
    char grid[5][5];
    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
    for(z=0;z<n;z++){
        for(j=0;j<n;j++){
            visited[z][j] = false;
        }
    }
    
    while(true) {
        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[0]){
                    found = dfs(grid,visited,word,0, z,j);
                    if(found) {
                            cout << "Word found at " << z << ", " << j << endl;
                    }
                }
                if(found) break;
            }
            if(found) break;
        }
        if(!found) { cout << "Word not found" << endl; }
        
        char choice;
        cout << "Enter y for another word: ";
        cin >> choice;
        if(choice != 'y') { break; }
    }
}
Add a comment
Know the answer?
Add Answer to:
Need help in c++ programming to output the lines in my code: if word if found:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • C++ Programming I have finished the code but there's one error which shows that "strcpy" might...

    C++ Programming I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you. Text.cpp: #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include "Text.h" Text::Text ( const char *charSeq ) {    bufferSize...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT