**URGENT** Hello, I have to Write a program for a word search that only recogniz
ID: 3813235 • Letter: #
Question
**URGENT**
Hello, I have to Write a program for a word search that only recognizes words that are in rows. Im Having trouble with keeping it in proper format for the project as all the aids online suggest different methods to make the program simpler. Unfortunately I don't know how to use those methods. Here is the project:
Write a Java program name FindWords.java to find words in a matrix. The letters in the matrix look random, but there are several hidden words in the matrix. You program will use a dictionary (provided) to find all the hidden words, and print them out. For example, you will find a file 0606matrix on Canvas, it reads: Here, the first number 6 is the number of rows, the second number 6 is the number of columns. The program name is FindWords.java. We type the following command %java FindWords < 0606matrix The result will be: the he hew ox box ox so The first word “the” is indicated in the following graph. The word “he” is indicated in the following graph. 2 The word “hew” is labeled in the following graph. Similarly, you can locate the other words in the matrix. Please note that when you are trying to find the words in the matrix, you only consider the situation where letters are on the SAME row. The letters of the same word must be adjacent to each other, from left to right. In this project, we do not consider the situation that letters lying out vertically or diagonally. For example, the following combination is not considered in this project.
Array 0606:
66
t h e w d q
c g u x o x
s t b o x n
x z a v t w
k v i k z c
n s o c q h
The Project also needs to be in a simplified format since it is only and intro course. Here is the template I received from my professor.
import java.util.Scanner;
import java.io.*;
public class FindWords{
// search word in the array list
// return true if found, otherwise false
// please finish this method
public static boolean checkword(String[] list, String word){
return false;
}
public static void main(String[] args){
// read words from file "words"
// the words are saved to array wordlist
// Please do not change this part
String[] wordlist= new String[99171];
int index=0;
try{
Scanner in = new Scanner(new FileReader("words"));
while(in.hasNextLine()){
wordlist[index]=in.nextLine();
index++;
}
}catch(IOException e){
e.printStackTrace();
}
// fill the following part
// read the matrix
// save the letters of the matrix to a 2D array
// fill the following part
// checks the sequences of letters
// to see whether they are words or not
}
}
Explanation / Answer
// C++ programs to search a word in a 2D grid
#include<bits/stdc++.h>
using namespace std;
// Rows and columns in given grid
#define R 3
#define C 14
// For searching in all 8 direction
int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
// This function searches in all 8-direction from point
// (row, col) in grid[][]
bool search2D(char grid[R][C], int row, int col, string word)
{
// If first character of word doesn't match with
// given starting point in grid.
if (grid[row][col] != word[0])
return false;
int len = word.length();
// Search word in all 8 directions starting from (row,col)
for (int dir = 0; dir < 8; dir++)
{
// Initialize starting point for current direction
int k, rd = row + x[dir], cd = col + y[dir];
// First character is already checked, match remaining
// characters
for (k = 1; k < len; k++)
{
// If out of bound break
if (rd >= R || rd < 0 || cd >= C || cd < 0)
break;
// If not matched, break
if (grid[rd][cd] != word[k])
break;
// Moving in particular direction
rd += x[dir], cd += y[dir];
}
// If all character matched, then value of must
// be equal to length of word
if (k == len)
return true;
}
return false;
}
// Searches given word in a given matrix in all 8 directions
void patternSearch(char grid[R][C], string word)
{
// Consider every point as starting point and search
// given word
for (int row = 0; row < R; row++)
for (int col = 0; col < C; col++)
if (search2D(grid, row, col, word))
cout << "pattern found at " << row << ", "
<< col << endl;
}
// Driver program
int main()
{
char grid[R][C] = {"GEEKSFORGEEKS",
"GEEKSQUIZGEEK",
"IDEQAPRACTICE"
};
patternSearch(grid, "GEEKS");
cout << endl;
patternSearch(grid, "EEE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.