CS 16, Winter 2018

Programming Assignment 5

Due: Sunday, March 4, 11:59pm
Worth: 100 points

PA5 can be done either in two-people teams or individually. If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.

We suggest you follow the instructions in sequence. First you are asked to write a program that performs a basic task (namely counting the number of times each ASCII character code occurs in the standard input, and printing results to standard output). You should do Part 1 and test it carefully to make sure it is working before proceeding to the next step. Half credit will be given just for accomplishing this basic part successfully. In the next part you will add features allowing a user to list input files and/or an output fle on the command line.

There is a program skeleton and another necessary file this time. Either download counts.cpp and common.h, or get copies as follows from ~cs16/pa5/ and we suggest you store them in a new directory in your own account at CSIL, specifically ~/cs16/pa5.

cp ~cs16/pa5/counts.cpp ~/cs16/pa5/
cp ~cs16/pa5/common.h ~/cs16/pa5/

The parameters defined for the main function in counts.cpp should be ignored while you work on Part 1, but leave them in the function header. These parameters are used in Part 2 for processing "command line arguments" (data typed on the command line after a program's name). Our textbook does not cover this topic, but it will be covered in lecture, and there is a demonstration program to look at below. If you want further guidance on this topic, see this nice command line argument tutorial from Prof. Stewart Weiss.

Our executable solution is ~cs16/pa5/counts (permissions are set to run but not copy) - type its full path and name at the command line to run it from your own account. Your goal should be to make yours run exactly like that one.

  1. Complete counts.cpp using the tools provided in common.h as follows:
    • Read characters from standard input until EOF (the end-of-file mark) is read (see the textbook page 353, and know that cin has most of the same features as input file streams. Do not prompt the user to enter text - just read data as soon as the program starts.
    • Keep a running count of each different character encountered in the input, and keep count of the total number of characters input.
      Note there are a total of 128 ASCII codes, equal to the value of the symbolic constant NUM defined in common.h. Get familiar with the features of common.h - many of them are handy, and you will be required to use some of them in later steps.
    • Print a neat table that shows each different input code, the corresponding character and its count, and print the total count at the end. Do not print any rows for ASCII codes that are not included in the input text.
    • You must exactly match the format of our solution's basic results, and so you must use the symbols and functions defined in common.h. Do NOT print anything any other way.
      • Use prHeader one time at the start.
      • Use prCountStr for each row that corresponds to a special character (codes 0-32, and 127), and use the symbol strings provided in common.h.
      • Use prCountChr for each row that corresponds to a printable character (codes 33-126).
      • Use prTotal one time at the end.
    • You can test it by typing text manually, in which case you must enter ctrl-D on Linux (or ctrl-Z on Windows) to end the input. Or better, test it by redirecting a file using the Linux redirection operator ('<'). For example, to input this sample text file named sample.txt, in our second sample run, notice that we typed the following:
      ./counts < sample.txt
      Another good input file for testing is allchars.txt which contains one of each ASCII character code. Of course, many of these codes don't show up well when you view it in your web browser, but your program should be able to identify them. Here's the result (of course): allchars-out.txt. A copy of allchars.txt is in ~cs16/pa5/ too.
  2. Make a backup copy of counts.cpp before attempting Part 2. A fully working basic version (i.e., just Part 1) will earn half-credit for this project, more points than a broken enhanced version that we cannot test at all.
  3. Enhance counts.cpp so that it responds to command line arguments as specified below. Here is an example program showing how command line arguments can be processed: args.cpp.
    • Let the user enter input filenames and/or an output filename on the command line.
      • If any input files are requested, then read from those files instead of cin - count all of the characters in all of the input files as one set of input data.
      • if a command line argument begins with '-o', then the next command line argument is an output filename (-o name). In that case print results to the file instead of cout. Ignore any command line arguments that may follow the output filename.
    • Signal errors by using the functions defined at the bottom of common.h:
      • Use badFile if a file (whether input or output) cannot be opened.
      • Use badOption if an argument begins with '-' but the second character is not 'o'.
      • Use missing if the '-o' option is not followed by a filename (even if the user types -oname with no space before the name).
      • Match these enhanced results. Notice the last several runs show error messages and no results.
  4. Submit PA5 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
    ~submit/submit -p 962 counts.cpp
    Be sure to wait for the test results. If you score 100/100 and you've followed all of the other rules, then you'll earn full credit.

Optional Challenges

Do not alter counts.cpp in response to these challenges - work with a copy instead.

Updated 2/21/2018, by C. Michael Costanzo