for this code pls make a pseudocode or a flow chart #include #include #include #include #include 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>& 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> visited(GRID_SIZE, std::vector(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; }
for this code pls make a pseudocode or a flow chart
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <
#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;
}
Step by step
Solved in 4 steps with 2 images