Question
C++ Write a program to search words in a matrix (imagine it is a 2-dimenional array). This program will find the words listed in command
C++ Write a program to search words in a matrix (imagine it is a 2-dimenional array). This program will find the words listed in command line and highlight the words with color. On Canvas, there are several matrices for testing. For example, inside 0505matrix file, you will read:
Here, the first 5 is the number of rows, the second 5 is the number of columns. If the C++ program name is words and we want to search tcnj and go in this matrix. We can type the following command
%words tcnj goAs you can see, tcnj and go are red colored. If you want to search this project is hard inside file 1520matrix. You can type: %words this project is hard
Similarly, to search you enjoy this project inside 2020matrix, you can type:
%words you enjoy this projectHow 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 array, from argv[1] to argv[argc-1]. The following code shows how to use argc and argv[]. Please note argv is an array, the elements of the array are char pointers. The argv[0] is the program name itself.
#include using namespace std;
int main(int arg, char *argv[]){ for(int i = 0; i
cout
Should I use two-dimensional array or something else? In general, we strongly suggest you use vector in this project. Vector will make coding way much easier than 2D array. The following is an example how to define two-dimensional vector, which is a replacement of traditional two-dimensional array.
I prefer you define one function that will check whether the word match the string starting from [i][j] at one direction (up, down, left,right, diagonals). If there is match, another method will record this information into a separating matrix.
This is the colormod.h code you will need to color the words:
Code:
#include "colormod.h" // namespace Color #include using namespace std; int main() {
Color::Setting red(Color::FG_RED); Color::Setting def(Color::FG_DEFAULT); cout "
}
0,J r th qnpoa achvg rtrah ufkez
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started