You will explore 2D arrays and char strings.
Your job is to convert a matrix that transforms the columns into rows for use with the strstr function
#include <iostream>
using namespace std;
const int WIDTH = 8;
const int HEIGHT = 10;
const int ROWS = HEIGHT;
const int COLS = WIDTH;
const int NEW_ROWS = WIDTH;
const int NEW_COLS = HEIGHT;
int main() {
int i,j;
// The +1 allows the null zero
char matrix[ROWS][COLS + 1] = { "gfcvymcc",
"yeoruoph",
"prmlpyza",
"orpytaci",
"ceupedor",
"rttlripb",
"imeurryi",
"aurmefsd",
"hlfofovo",
"cpoofyjq"};
// declare new_matrix to hold the newly created
// transposed matrix
cout << "The first row in the matrix" << endl;
cout << "gfcvymcc" << endl;
for (i=0; i<COLS; i++)
cout << matrix[0][i] ;
cout << endl;
cout << "The first column in the matrix" << endl;
cout << "(matches this, vertically) gypocriahc" << endl;
for (i=0; i<ROWS; i++)
cout << matrix[i][0] << endl;
cout << endl;
// create the nested loop that copies the first
// row to the first column and so forth
// into the new matrix
// create a for loop that prints the first row
// of characters
cout << "First row in the new matrix (should match this)" << endl;
cout << "gypocriahc" << endl;
// create a for loop that prints the first column
// of characters in the new_matrix
cout << "First column in the new matrix ";
cout << "(should match this, vertically)" << endl;
cout << "gfcvymcc" << endl;
// new line
cout << endl;
// create a nested loop that prints
// the new_matrix that should match
// the following output.
cout << "The new matrix should match this" << endl;
cout << "gypocriahc" << endl;
cout << "ferretmulp" << endl;
cout << "computerfo" << endl;
cout << "vrlyplumoo" << endl;
cout << "yupterreff" << endl;
cout << "moyadirfoy" << endl;
cout << "cpzcopysvj" << endl;
cout << "chairbidoq" << endl;
cout << "-------------" << endl;
cout << endl;
return 0;
}
Your job is to search for words in the puzzle and correlate them to the original matrix if found that was converted by Team A.
#include <iostream>
#include <cstring>
using namespace std;
const int WIDTH = 8;
const int HEIGHT = 10;
const int ROWS = HEIGHT;
const int COLS = WIDTH;
const int NEW_ROWS = WIDTH;
const int NEW_COLS = HEIGHT;
const int NUM_WORDS = 5;
int main() {
//char buffer[NEW_COLS + 1];
int colFound, row, wordn;
char *spot = nullptr;
char words[NUM_WORDS][NEW_COLS + 1] = {"ferret",
"computer",
"chair",
"copys",
"plum"
};
// The +1 allows the null zero
char matrix[ROWS][COLS + 1] = { "gfcvymcc",
"yeoruoph",
"prmlpyza",
"orpytaci",
"ceupedor",
"rttlripb",
"imeurryi",
"aurmefsd",
"hlfofovo",
"cpoofyjq"};
char new_matrix[NEW_ROWS][NEW_COLS + 1] =
{ "gypocriahc",
"ferretmulp",
"computerfo",
"vrlyplumoo",
"yupterreff",
"moyadirfoy",
"cpzcopysvj",
"chairbidoq"
};
// Write a nested loop to find the words in the puzzle
row = 3;
wordn = 4; // plum
cout << "The haystack is " << new_matrix[row] << endl;
cout << "The needle is " << words[wordn] << endl;
if ( (spot = strstr(new_matrix[row],words[wordn] )) ) {
colFound = spot - new_matrix[row] + 1;
cout << "I found it in the new matrix at column " << colFound << endl;
cout << "and in new matrix it is at row " << row + 1 << endl;
cout << "Where is it in the original matrix?" << endl;
} else {
cout << "I did not find the word in this haystack" << endl;
}
return 0;
}