CSCI 160 bonus lab exercise, F21N01/N02

This lab requires a different set of instructions to obtain the lab, but the process to submit it is the same (make submit from inside your csci160/bonuslab directory).
The lab must be submitted by 5pm on Thursday Nov. 25th, and (since this lab is purely available as a bonus) absolutely no late submissions will be accepted. The "make submit" will cut off submissions at precisely the lab deadline.

Obtaining the lab
To obtain the lab, run the commands below exactly as shown, noting that the csci160-01 is used for students who were originally registered or waitlisted in the Monday lab, while the -02 version is for students who were originally registered or waitlisted in the Friday lab.

Monday lab group
cd
ssh  -x  csci  fork  csci160-01/bonuslab  csci160-01/$USER/bonuslab
cd  csci160
git  clone  csci:csci160-01/$USER/bonuslab
Friday lab group
cd
ssh  -x  csci  fork  csci160-02/bonuslab  csci160-02/$USER/bonuslab
cd  csci160
git  clone  csci:csci160-02/$USER/bonuslab

bonuslab.cpp objectives
In this lab we'll be working on loops and arrays of strings, applying a customized form of sorting.

For the lab exercise, we'll be getting the user to enter two lists of words: the first representing a dictionary, the second representing the text to be sorted. We'll then apply a customized sorting algorithm (described below) to display the text contents in a particular order.

You must use the iostream library (not cstdio) and the string library (not cstring) for this lab. I would also recommend the use of the sstream library, for the istringstream functionality discussed in the "Design hints" section at the bottom of the page.

The applied objective for this exercise is to create and submit a complete and correct C++ program, adhering the the course code standards, with the following behaviour and restrictions:

  1. First, the program displays a short message describing the program behaviour to the user.

  2. Next, the program prompts the user to enter a sequence of words (all on one line and separated by spaces), and reads/stores the words in a dictionary (to be implemented as an array of strings).
    Tips on how to do this are provided in the design hints section at the bottom of this page.

  3. Next, the program prompts the user to enter another sequence of words (also one line, space delimited), and reads/stores these into a second array of strings.

  4. Finally, the program displays the words from the second array, but rearranged to match the order in which they were entered for the dictionary.
    (Again, tips on how to do this are provided in the design hints section at the bottom of the page.

  5. The maximum number of dictionary words the program needs to handle is 20, while the maximum number of "text" words it needs to handle is 100. (Of course, be sure to use constants.)

  6. Error checking:

Sample run

The table below shows a sample run of the program. Note that you do not have to use the exact same prompts or error messages as those shown below, but be sure your prompts and messages are clear and informative.

Just to make this display a little clearer, I've highlighted what the user entered in bold italics. (That's not something you could/would do in your program.)

Welcome to the dictionary sorter!

You will be asked to enter a list of dictionary words (the words that
can appear in the user text to be entered later), and the program will
read in those words.

Afterwards, you'll be asked to enter the sequence of words to be sorted.

The program will then display the text, but with the words re-arranged to
match the order given in the dictionary.

---

Please enter your dictionary words (all on one line, separated by spaces,
   for example:   ok gang lets split up and look for clues

I like my dictionary words list for now they are these wow

Thank you, now please enter the text to be sorted, again separated by spaces
and all on one line, and only using words from the dictionary.

my words for now are like these I like my list my dictionary now my list for now

The words from your text, rearranged in the given dictionary order, are:
I
like like
my my my my
dictionary
words
list list
for for
now now
are
these

Thank you for using the dictionary sorter, bye!

Deadlines and late penalties

This lab is due by 5pm Thursday November 25th, absolutely no late submissions will be accepted.

Reminders

Don't forget to do your "make submit" when finished, and don't forget about the tools/tips from week 2 of labex1 (ssh'ing to the pups, getting your lab feedback, and use of X2Go).

Don't forget to follow the code standards, be sure to check your feedback from previous labs to see if there were concerns with your previous work with respect to the code standards.

Design and implementation hints

Since we're obtaining the user input as a collection of words on a single line, we cannot really use a cin >> str style of input (that will skip past the newline when it encounters it). Instead, I suggest using something like the following to put the entire line into a string variable:
string str;
getline(cin, str);
Note that when using getline to cin to an array of chars the syntax is
char text[80];
cin.getline(text, 80);

Once that is done, we can use <sstream> library types and routines to take words from the string and put them into the dictionary, e.g.
   istringstream strm(str);  // creating the readable stream from str
   do {
      strm >> word;  // assuming word is a string var
      if (!strm.fail()) {
         // put the word in the dictionary array
         // (unless the array is already full)
      }
   } while (!strm.fail());

Having read the words into the dictionary, you can apply a similar technique to read the words of text the user wants sorted.

In generating the "sorted" output, we could use one loop going through each word in the dictionary, count how often that word appears in the main text, and display the word that many times:
  // for each position, i, in the dictionary
  //     lookup the word in position i
  //     search the main text, counting the number of times that word appears
  //     display the word that many times

As always, I strongly recommend an incremental development approach.