Question: Write a program to search words in a matrix (imagine it is a 2-dimensional array). This program will find the words listed in the command

Write a program to search words in a matrix (imagine it is a 2-dimensional array). This program will find the words listed in the command line. On Canvas, there are several matrices for testing. For example, inside 0505matrix file, you will read: 0505matrix file download(https://tcnj.instructure.com/courses/1722274/files/115173175/download?wrap=1)

5 5

u r a q o

f t c n j

k r h p r

e a v o t

z h g a h

Here, the first 5 is the number of rows, the second 5 is the number of columns. If we want to search for tcnj and go in this matrix. We can type the following command

a.exe tcnj go < 0505matrix

* * * * *

* t c n j

* * * * *

* * * o *

* * g * *

As you can see, tcnj and go are printed as is while the other characters are printed as *.

How to search ONE word in the matrix: Your program should scan the whole matrix. If the program is visiting [i][j] element now, it will check whether word match the strings on [i][j] elements left side, right side, upward, downward, and four diagonal directions. If the program finds a match, it will mark a separating matrix to keep the record. This separating matrix can be a Boolean array or a char array. Please keep in mind that the input matrix may have multiple matches for one word. Your result must show all the matches.

Should I use a two-dimensional array or something else? In the lecture, we will compare a 2D array with other choices. In general, we strongly suggest you use a vector in this project. Vector will make coding way much easier than a 2D array.

How to search multiple words in the matrix: Once your program can search one word, you can use a for loop to go over each word typed at the command line. Note all the words you type will be saved to argv[]. You can use argc to check how many words typed. The words you typed are stored in an array, from argv[1] to argv[argc-1]. The following code shows how to use argc and argv[].

#include  using namespace std; int main(int arg, char *argv[]){ for(int i = 0; i < arg; i++) cout << argv[i] << endl; }

Please note argv is an array, the elements of the array are char pointers. The argv[0] is the program name itself. For example, if you type

a.exe a b c

The output will be

a.exe a b c

How to read the matrix into memory: you should use cin and nested for loops to read the data.

make a file and put the matrix file, the a.exe file, and the cpp file to run it.

run it in command prompt

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!