Date: Mon, 11 Nov 1996 17:00:37 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Tue, 29 Oct 1996 21:21:14 GMT Content-length: 6598 Program 5

Program 5

For this assignment you will write a program that can print a simple histogram, or bar chart, based on the contents of a string. The chart shows how many of each letter of the alphabet appeared in the string. For example, when run on the string "Program Four", the program should print the following output:

   a:    1 *
   f:    1 *
   g:    1 *
   m:    1 *
   o:    2 **
   p:    1 *
   r:    3 ***
   u:    1 *

   11 letters
   scale factor: 1

Note how only the letters are counted - punctuation, such as spaces, is ignored. Also, upper- and lower- case letters are not distinguished. The chart shows the number of occurrences of each letter in both numerical and "visual" form (a bunch of asterisks).

How to Do It

The basic technique is to create an array of integers to hold the counts for each letter. The array should have 26 elements - one for each letter of the alphabet. The first element in the array will hold the number of A's, the second element holds the number of B's, and so on. You can achieve this mapping of the letters 'A' - 'Z' to the numbers 0 - 25 with the following functions:
   int char_to_int (char ch)
   {
      return tolower (ch) - 'a';
   }

   char int_to_char (int n)
   {
       return n + 'a';
   }
These functions rely on the fact that characters are really just integers, and the compiler will translate from one to the other automatically.

Your program should go through the entire input string character-by-character, and if it is a letter, add 1 to the count stored in the element of the array that corresponds to that letter.

What to Do

There are some other requirements for this assignment. You must do these, but I highly recommend that you get the basics working first. Only work on these after you can actually print out a simple histogram!

Counting the Total Number of Characters

You must count the total number of letters and print out this information. See the example below.

Scaling the Bar Chart

If any character occurs more than 50 times, you must scale all of the bars in the chart so that the longest bar is printed with 50 stars. You should also print out the scaling factor. For example, if the input string was:
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbb
   ccccccccccccccccccc
   d
   eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
   eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
   fffffffffffff
   gggggg
Then your program should print:
   a:   37 *****************
   b:   22 **********
   c:   19 *********
   d:    1
   e:  104 **************************************************
   f:   13 ******
   g:    6 **

   26 letters
   scale factor: 0.480769
The scaling factor is just 1.0 if the most frequent character occurs at most 50 times. If it appears, say 104 times, then the scaling factor would be 50.0 / 104, or 0.480769. You would then multiply this scaling factor by the frequency of each character to compute the number of stars to print.

Using Functions

Your program must be properly structured using functions. This means that different tasks or subproblems are performed in different functions. Information is passed in and out of the the functions via the parameters and return value.

To help push you in the right direction in this regard, I am instituting a few rules for this assignment.

In addition, these rules always apply:

Reading the Input from a File

Eventually, after you get everything else working, you must change your program so that it get its input from a file. It should ask the user for the name of the file, read the file, and use this as the input. Fortunately, this should be really easy, since I am providing you with a function that reads an entire file as a single string. The function has the following prototype:
     string read_file (string filename)
This function reads the file filename and returns its contents as a single string. (This string may contain many lines, which are separated by special newline and carriage return characters. Your program should just ignore these, like all non-letters.) If there was an error (for example, the file didn't exist) then it returns the empty string.

The read_file function is defined in the object file r:\public\mbirk\readfile.obj. This means that you will have to add this to the Project Window. The Project Window should contain three files: prog4.cpp, r:\public\mbirk\string.obj, and r:\public\mbirk\readfile.obj.

What to Hand In

Hand in your source code, as well as the output of your program on the following test cases: (note: don't hand in the test cases themselves)

Also, here are a couple more test cases and their result, which you can use to see if your implementation is working. Do not hand these in.


mbirk@cs.wisc.edu