You will explore 2D arrays and char strings.

Team A

Your job is to convert a matrix that transforms the columns into rows for use with the strstr function

  1. Declare new_matrix to hold the newly created transposed matrix
  2. Create the nested loop that copies the first row to the first column and so forth into the new matrix
  3. Create a for loop that prints the first row of characters of the new matrix.
  4. Create a for loop that prints the first column of characters in the new_matrix.
  5. Create a nested loop that prints the new_matrix.
#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;
}

Team B

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.

  1. Write the nested loop to search for the words in the puzzle. A single case of a wordsearch for a word on a single line is presented.
  2. For words that are found, display the original position they were located in matrix.
#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;
}