CSE 332 Lab 2: Tiles, Words, and Scores

Lab dates: Wednesday September 16 and Wednesday September 23, 2009
Due by Tuesday September 29 at 11:59 pm (deadline for e-mailing a zip file with your solution)
Final grade percentage: 5 percent

Objective:

This lab is intended to extend your use of basic C++ language features from the previous lab, and to introduce more advanced techniques like the use of member operators and algorithms from the C++ STL, including:

To do this, you will implement a simple C++ program that can (1) count the total number of times each letter occurs in the strings in a dictionary file, (2) give a score to each letter so that more frequently occurring letters have lower scores than rarer ones, and (3) generate and emit a tile definition file (like the one from the previous lab) based on the letters, their frequencies, and their calculated scores.

In this lab and in the subsequent labs, you are encouraged to re-use and modify code from your previous lab solutions - if you choose do this, please put a completely new copy of any header and source files from your previous lab(s) in the new lab project directory you create. This can avoid some potential for error or confusion within Visual Studio, and also ensures you have a backup copy that you could go back to if a modification you try doesn't pan out.

In the next lab assignment you will use these features to generate permutations of letter tiles, check each permutation against a dictionary, and generate a score for each matching permutation.

Assignment:

    Part I - Readings:

  1. The following readings in the optional text books may be useful as reference material while working on this lab assignment, and though we'll touch on most of the issues in the next few studio sessions, selectively looking up topics and/or reading ahead (on demand as you encounter issues while working on the lab) is encouraged:

  2. The C++ string class, file input and output stream, and input and output stringstream examples in the C++ Data and I/O lecture slides also again may be helpful when implementing parts this lab.

  3. Please read the following CSE 332 Programming Guidelines which are again relevant to this lab as well as the previous one, and please follow them as you implement your solution: A.1-A.6, A.9-A.11, A.17, B.7, B.9, B.13, B.15, B.16, B.20-B.23, B.25, B.30, B.33, B.34, C.1-C.4 and C.6-C.9.

    Part II - Program Design and Implementation:

    Note: the details of this assignment are again intentionally somewhat under-specified, leaving you some room to choose what you think is the best way to implement them, as long as what you do is reasonable and you explain your design decisions in comments in the code and in your Readme.txt file.

  4. Open up Visual Studio, and create a new Win32 Console Application Visual C++ project for lab 2.
  5. If you have not done so already in lab 1, modify your usage message function so that it takes in two strings: one giving the name of the program (which will always be given to the main function in argv[0]), and another giving the rest of the information to convey to the user. As in the lab 1 assignment, the function should print out the program name and how to run the program using those arguments, and should return a non-zero integer.
  6. Declare and define a struct to represent a "letter tile definition" (e.g., of type LetterTileDef) that contains a letter tile (e.g., of type LetterTile if you so named that struct in lab 1), and an unsigned integer (of type unsigned int) that counts how many occurances of that tile there are.

    In that struct, declare a less than operator that takes a reference to a const letter tile definition object and returns a bool. Define the operator (in a separate source file for that struct) so that it returns true if (and only if) either (1) the current object's count is less than the count of the object passed in, or (2) the counts are the same but the character in the current object's letter tile is less than that of the object passed in. In addition to taking a reference to a const object (which promises that the code will not change the object that was passed), the operator also should promise not to change the current object by putting the const keyword after the closing parenthesis of its signature, in both its declaration and its definition.

  7. Declare and define a function for generating a vector of tile definitions from a vector of stings, that takes a reference to a const vector of C++-style strings (of type vector<string> &) and a reference to a (non-const) vector of letter tile definitions (e.g., of type vector<LetterTileDef> &) and returns an integer (0 for success, or non-zero to indicate it encountered a problem).

    The function should first form a single string by concatenating all the strings in the first vector, and then sort the letters in the resulting string into ascending order using the STL sort algorithm. For example, if the first vector contained "all" and "good" then concatenating them would give "allgood" and sorting the concatenated string would give "adglloo".

    The function should then count the number of times each letter appears in the sorted concatenated string, and for each letter push back a letter tile definition (using the letter, a score of 0, and the count that was just obtained) into the second vector that was passed to the function.

    The function should then sort the vector of tile definitions (which should order them according to the less-than operator given above) and assign a score to each tile definition so that tile definitions that occur less frequently have higher scores than ones that occur more frequently (you are free to assign the same scores to tile definitions with the same occurrence counts, though that is not strictly required).

  8. Declare and define a function for printing out letter tile definitions to a file, that takes a reference to a const vector of letter tiles definitions (for example of type const vector<LetterTileDef> & if you named your struct type LetterTileDef) and a C-style string (of type char *) that has a default argument value of "tile_defs.txt" (that is, if the argument is not supplied where the function is called, that value will be used by default).

    This function should open a file stream using the C-style string given in the second function parameter as the name of the file to open and write out one line at a time to that file with the letter, score, and count for each letter tile definition in the vector appearing together on a line with spaces in between them. If the file cannot be opened or the function encounters any other problems during its execution it should print out a helpful error message indicating the problem and return a non-zero value; otherwise it should return 0 to indicate success.

  9. For your project's main function:

  10. Build your project, and fix any errors or warnings that occur. Please be sure to note all of the different kinds of errors or warnings you ran into (though you don't have to list every instance of each kind) in your ReadMe.txt file. If you were fortunate enough not to run into any errors or warnings, please note that instead, where you would have listed errors or warnings in your ReadMe.txt file.

  11. Open up a Windows console (terminal) window and change to the directory where your program's executable file was created by Visual Studio (see the first studio exercises document for more details on this, if you don't remember them).

  12. Run the executable program through a series of trials that test it with good coverage of cases involving both well formed and badly formed inputs. For this lab one especially useful test is to check whether the output printed by your program consistently represents the contents of valid dictionary and tile definition files it was given. In your ReadMe.txt file please document which cases you ran (i.e., what the command lines were) and summarize what your program did and whether that is correct behavior, in each case.

  13. In your ReadMe.txt file, make sure that your name and the lab number (lab 2) are at the top of the file, and that you've documented whether you ran into any errors or warnings while developing your solution (and if so what they were) and what the executable program did for each of the trials you ran. Be sure to save your changes to your ReadMe.txt file, as well as those to your source (and header) file(s) before preparing your solution zip file.

  14. Prepare a zip file that contains your project's source, header, and ReadMe.txt files (except for stdafx.h, stdafx.cpp or other Windows-generated files you did not modify), by:

    Send the zip file containing your lab 2 solution as an e-mail attachment to the course e-mail account (cse332@cec.wustl.edu) by the submission deadline for this assignment. If you need to make changes to your lab solution you are welcome to send a new zip file, and we will grade the latest one received prior to the deadline (according to the time stamp the server puts on the e-mail).

    Part III - Scoring Words in the Dictionary: (optional, worth up to 5% extra credit)

    The goal of this optional part of the assignment is to build on the code you wrote in the main portion of the assignment and give you additional experience working with the techniques involved.

    Please ask for help from your professor or teaching assistant if you are uncertain about how to do this extra credit part or if you run into any difficulty with it:

  15. Declare and define a function that takes a reference to a const C++-style string and a reference to a const vector of tile definitions, and uses the tile definitions to compute and return a score for the string.

    If the string has any character that does not have a corresponding tile definition, or if the number of times any character appears in the string exceeds the count in the corresponding tile definition, the function should return 0. The function should return a negative value if it ran into any other problem during execution. Otherwise, the function should return the sum of the scores for each of the characters appearing in the string.

    For example if the letters 'd', 'e', 'g', and 'l' all have scores of 2 and counts of 1, letter 'o' has a score of 1 and a count of 2, and no other characters had definitions, then "good" would score 6, but both "google" ('g' appears more times than its count) and "dove" ('v' does not have a corresponding definition) would score 0.

    Hint: even though the vector of strings is const (which means you cannot modify the strings in it, you can create a separate string that contains the same characters, and then do things like sorting the characters in the separate string etc. which can really help in implementing the requirements above).

  16. Declare and define a function that takes a reference to a const vector of strings and a reference to a const vector of tile definitions, uses the above function to compute a score for each of the strings in the first vector, and prints out each string followed by its score and then a line break to the standard output stream (cout).

  17. In your main function, after the dictionary file has been read into a vector of strings and the vector of tile definitions has been generated, call the above function with those vectors.

  18. Run your program, copy the output produced by this extra credit portion into your ReadMe.txt file, in a separate extra credit section. In that separate section describe briefly how you implemented the extra credit portion of the lab, and note any design decisions or problems you ran into and how you resolved them. Submit the expanded program and Readme.txt file per the instructions for the main assignment above.


Updated 2:25pm Wednesday September 16 2009 by
Chris Gill

Changes since original posting: