Question

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 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.

11) Finally, add an interactive capability to your program that asks the user to enter additional words to search for in the matrix once you’ve searched for all words in the data file.

The following is hiddenWords.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

//Your code

#include<bits/stdc++.h>
#include<fstream>
using namespace std;
int main(){
   ifstream fout;
   string line;
   while(1){
       printf("Enter file Name or Enter exit to stop\n");
       string st;
       getline(cin,st);//reading file
       if(st == "exit")
           break;
       fout.open(st);
       //cout<<fout<<endl;
       if(!fout){//If file not opened
           perror("File Not found\n");continue;
       }else{
           int i=0,n,j,m,k;
           fout>>n;
           fout>>m;
           cout<<n<<" "<<m<<endl;
           //char c;
           //fout>>c;
           i=0;
           string line;
           char mat[n+1][m+1];
           while(fout && i < n){
               getline(fout,line);
               if(line.size() > 0){
               cout<<line<<endl;
               for( j = 0 ; j < m ; j++ )
                   mat[i][j] = line[j];
               i++;
               }
           }
           vector<string>v_s;//to store Not found string
           int ex=1;
           while(ex){
               if(fout){
                   getline(fout,line);
               }else{
                   printf("If you want to enter more words to search ,Enter word else Enter exit\n");
                   cin>>line;
                   if(line == "exit")break;
               }
               //cout<<line<<" ";
               int fl = 0 ,l = line.size();
               for( i = 0 ; i < n ; i++ ){//Searching string
                   for( j = 0 ; j < m ; j++ ){
                       fl=0;
                       for( k = 0 ; k < l && j+k < m; k++ ){
                           if(mat[i][j+k] == line[k] )fl++;
                           else break;
                       }
                       if(fl == l){
                           i=n;j=m;break;
                       }
                       fl=0;
                       for( k = 0 ; k < l && i+k < m; k++ ){
                           if(mat[i+k][j] == line[k] )fl++;
                           else break;
                       }
                       if(fl == l){
                           i=n;j=m;break;
                       }
                       fl=0;
                       for( k = 0 ; k < l && j+k < m && i+k < n; k++ ){
                           if(mat[i+k][j+k] == line[k] )fl++;
                           else break;
                       }
                       if(fl == l){
                           i=n;j=m;break;
                       }  
                   }
               }
               if(fl == l && fl){//If string is found
                   cout<<line<<" is found"<<endl;
               }else{
                   v_s.push_back(line);
               }
           }
           printf("Not found words are\n");
           for( i = 0 ; i < v_s.size() ; i++ )
               if(v_s[i].size() > 0)
               cout<<v_s[i]<<endl;
           getchar();
       }
      
   }
return 0;
}

//Screenshot of editor

//Screenshot of my output terminal

//If you have any doubt.Please feel free to ask.

Thanks

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

  • 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...

  • 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...

  • 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(...

  • Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words...

    Write a C++ program that prompts the user with the following menu options: [E]rase–ArrayContent [C]ount–Words [R]ev–Words [Q]uit 1. For Erase–ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase is as follows: void Erase( int a[ ], int * N, int * Search-Element ) The function Erase should remove all occurrences of Search-Element from the array    a[ ]. Note that array a[ ] is loaded with integer numbers entered by the user through the...

  • 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...

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

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
ADVERTISEMENT