Question

Write a program that searches for specified words hidden within a matrix of letters. C++ HELP...

Write a program that searches for specified words hidden within a matrix of letters.

C++ HELP -pls keep basic as possible, thanksssss


1. Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix.
2. Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row by row, column by column.
3. Following the rows of letters, there will be some number of words that might be hidden within the matrix. You are to search for these words and report 1) whether or not a given word is found within the matrix, and if so, the starting and ending matrix coordinates of the word.
4. Search for each word one at a time (i.e., read a word, search for it, report the results, and then read the next word from the file). Continue until all words have been searched for.
5. Start by creating your own file of data. You can start with a small matrix size and your own hidden words. When you’re ready, start testing one or more of the files I’m going to upload for your use.
6. To test your program, I suggest searching for the first 4 letters in the top row, then the first 4 letters in the leftmost (first) column, and then the first 4 letters starting from position [0][0] down the diagonal to the right and down. That will let you know if you are traversing the matrix properly.
7. Provide for fault-tolerance when trying to open the input file. If it doesn’t exist or can’t be found, tell the user. Also, tell the user the name of the file that can’t be found/opened. Then give the user a chance to enter another file name and try to open that file. Continue until you either find and open the file or until the user types something like “quit” or “exit.” If you feel comfortable doing so, use the perror function to report the system’s error message; then report your own error messages. Make sure your program can handle bad file names and/or non-existent files.
8. Don’t report success or failure for each word as you go. Instead, report only words that are found; but for words that are not found, shove them into a vector of strings and wait until you’ve searched for all words before reporting the words that weren’t found. Thus, you will have all successfully found words appear first -- along with their coordinates and (optionally) their direction -- and then the words that weren’t found will be listed at the end.
9. Assume that words can appear more than once in the matrix.
10. Don’t worry about wrapping around the edges of the matrix. Instead, if you hit an edge while searching, assume the word isn’t in the matrix.

FILE LOOKS LIKE THIS(HiddenWords-ComedyMovies.txt):

15 18
SCFRBOBROBERTSLOKL
TNLESREKCILSYTICRL
AOEPOBLRUAECHBUDEA
RITOTHEYOMTOLBATPH
LCHAIBAONILLAURGEI
AEKNLULDDGCISLHKLN
CRRURDBHSAGTEOPLUS
LOTCLULHLLBPCRAPLA
UMSKIROPOULALPEAAK
EHNSAHNNSTLASACCEH
LSOORADTEHSHBKCROA
EUOUFMEMECOHETHEER
SRMPARTRBTAROSAASV
SLEISTOOTHERUTLESE
NATTAHNAMSEYSLSSMY

ALLOFME
ANNIEHALL
BABE
BEINGTHERE
BIG
BOBROBERTS
BULLDURHAM
CARS
CATBALLOU
CITYSLICKERS
MEATBALLS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;

int main() {
   string file_name;
   //input the file name
   cin>>file_name;
   //open file
   ifstream myfile(file_name);
   if(myfile.is_open())
   {
       //take input of row and coloumn
       int row,col;
       myfile>>row>>col;
       //declare char matrix
       char mat[row][col];
       //input the character matrix
       for(int i=0;i<row;i++)
       {
           for(int j=0;j<col;j++)
           myfile>>mat[i][j];
       }
       //vectors of string not found in matrix
       vector<string> notFound;
       while(!myfile.eof())
       {
           //get the word
           string word;
           getline(myfile,word);
           //push the word in noFound if word will be found it will be removed
           notFound.push_back(word);
           //start traversing the matrix
           for(int i=0;i<row;i++)
           {
               for(int j=0;j<col;j++)
               {
                   //if first character matches
                   if(word[0]==mat[i][j])
                   {
                       //bool variable to store if word is found or not
                       bool found=false;
                       //search in right direction
                       if(j+word.size()-1<col)
                       {
                           //make test string of characters of matrix from i,j to i,j+word.size()-1
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i][j+k];
                           //if test is same as word
                           if(test==word)
                           {
                               //make found is true
                               found=true;
                               //pop the string from notFound vector
                               notFound.pop_back();
                               //print the position and direction
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in right direction"<<endl;
                           }
                       }
                       //search in left direction
                       //rest is similar to above case
                       if(j-word.size()+1>=0&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i][j-k];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in left direction"<<endl;
                           }
                       }
                       //search in vertical down direction
                       if(i+word.size()-1<row&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i+k][j];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in vertical downward direction"<<endl;
                           }
                       }
                       //search in vertical up direction
                       if(i-word.size()+1>=0&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i-k][j];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in vertical upward direction"<<endl;
                           }
                       }
                       //search in diagonally down-right
                       if(i+word.size()-1<row&&j+word.size()-1<col&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i+k][j+k];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in diagonally down-right direction"<<endl;
                           }
                       }
                       //search in diagonally down-left
                       if(i+word.size()-1<row&&j-word.size()+1>=0&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i+k][j-k];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in diagonally down-left direction"<<endl;
                           }
                       }
                       //search in diagonally up-left
                       if(i-word.size()+1>=0&&j-word.size()+1>=0&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i-k][j-k];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in diagonally up-left direction"<<endl;
                           }
                       }
                       //search in diagonally up-right
                       if(i-word.size()+1>=0&&j+word.size()-1<col&&found==false)
                       {
                           string test="";
                           for(int k=0;k<word.size();k++)
                           test+=mat[i-k][j+k];
                           if(test==word)
                           {
                               found=true;
                               notFound.pop_back();
                               cout<<word<<" is found at pos "<<i<<" "<<j<<" in diagonally up-right direction"<<endl;
                           }
                       }
                   }
               }
           }
       }
       //print not found words
       cout<<"The words not found are"<<endl;
       for(int i=0;i<notFound.size();i++)
       cout<<notFound[i]<<endl;
       myfile.close();
   }
   //if file can not be opened inform user
   else
   {
       cout<<"The file is not found"<<endl;
   }
   return 0;
}

Output of code run in input given in question

Add a comment
Know the answer?
Add Answer to:
Write a program that searches for specified words hidden within a matrix of letters. C++ HELP...
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
  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE pr...

    Write a program in C to make dictionary to add, delete and search words Using linked lists. MAKE THE SEARCHING IN THE program like the searching in the online dictionary(if we enter the 1 letter then it will show all the letters starting with the same number and if we enter 2 letters then it will show all the numbers starting with same two letters and so on up to the complete word.) make the following functions 1. insert 2....

  • Create a program that stores words in an STL vector and then searches the vector for...

    Create a program that stores words in an STL vector and then searches the vector for words. Requirements You will keep track of strings as C++ string objects. In main, declare a vector variable to contain your strings. Create a loop that gets one word at a time from the user until they enter "." as the only thing on the input line. For this assignment, it is adequate to limit strings to one word at a time. Put the...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • The picture is given in a PPM file and your program should put the converted one...

    The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the converted file.In addition, you can use a temporary file called tmp.ppm. •The number of rows and columns are not fixed numbers. •The converted file should also follow the PPM format with the above simplification, and can be converted subsequently. •Read the pixel matrix into a buffer. •For each row i(...

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