Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

for this code pls make a pseudocode or a flow chart 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>

const int GRID_SIZE = 5;

// Function to generate a random letter
char generateRandomLetter() {
    return 'A' + rand() % 26;
}

// Function to generate a random grid
void generateGrid(char grid[GRID_SIZE][GRID_SIZE]) {
    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            grid[i][j] = generateRandomLetter();
        }
    }
}

// Function to display the grid
void displayGrid(const char grid[GRID_SIZE][GRID_SIZE]) {
    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            std::cout << grid[i][j] << ' ';
        }
        std::cout << std::endl;
    }
}

// Function to check if a word is found in the grid
bool isWordFound(const std::string& word, const char grid[GRID_SIZE][GRID_SIZE], int row, int col, std::vector<std::vector<bool>>& visited, int index) {
    // Base cases for recursion
    if (index == word.length()) {
        return true; // All characters of the word have been found
    }

    if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE) {
        return false; // Out of bounds
    }

    if (visited[row][col]) {
        return false; // Already visited this cell for the current word
    }

    if (grid[row][col] != word[index]) {
        return false; // Current character doesn't match
    }

    // Mark current cell as visited
    visited[row][col] = true;

    // Recursively search for the remaining characters in all eight directions
    bool found = false;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            if (i == 0 && j == 0) {
                continue; // Skip the current cell
            }
            found = isWordFound(word, grid, row + i, col + j, visited, index + 1);
            if (found) {
                break;
            }
        }
        if (found) {
            break;
        }
    }

    // Mark current cell as unvisited for the next word
    visited[row][col] = false;

    return found;
}

int main() {
    srand(time(0));

    char grid[GRID_SIZE][GRID_SIZE];
    generateGrid(grid);

    std::cout << "Welcome to Bad Spelling!" << std::endl;
    std::cout << "Generated grid:" << std::endl;
    displayGrid(grid);

    while (true) {
        std::string word;
        std::cout << "\nEnter a word (or 'q' to quit): ";
        std::cin >> word;

        if (word == "q") {
            break;
        }

        std::vector<std::vector<bool>> visited(GRID_SIZE, std::vector<bool>(GRID_SIZE, false));

        bool found = false;
        for (int i = 0; i < GRID_SIZE; i++) {
            for (int j = 0; j < GRID_SIZE; j++) {
                if (isWordFound(word, grid, i, j, visited, 0)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }

        if (found) {
            std::cout << "The word '" << word << "' is found in the grid!" << std::endl;
        } else {
            std::cout << "The word '" << word << "' is not found in the grid." << std::endl;
        }
    }

    return 0;
}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education