Assignment 5: Frequency Analysis

This time we want to create a program to perform a simple "frequency analysis" of an English and a German text. Specifically, I will give you two text files, one in English, the other in German. You have to create a program that opens the files and performs a frequency analysis on that file as follows:

Your program must return the following information for each of the two text files:

The total number of lines for the text
The total number of characters in the text
The average number of characters per line (which most likely is a decimal number !)
The letter that appears most frequently in the text
For extra credit, your program should also return all characters, sorted in order of frequency of appearance in the text, most frequent letter first.

You have to submit to my by Moday one email message, containing the following information:

The statistics for the English text (as described above)
The statistics for the German text (as described above)
The source code of your program as an attachment to your message.


Downloading data files

You can obtain the two text files as discussed in class. Alternatively, you can also download them here by "right-clicking" on the appropriate link and selecting the option to "Save" that link.

Right-click here for the text in English  and "Save link as" a file on your hard disk, floppy, or network drive.
Right-click here for the text in German and "Save link as" a file on your hard disk, floppy, or network drive.

To simplify your program a little, you can also use the previous programming assignment, where we created several sort functions. You can download the file vectmisc.cpp which contains the source code for various vector (or array) handling function, in particular for sorting, find max/min values, and for max/min values. Again, right-click on the link and save to disk.

Right-click here for sample sorting routines for arrays. Note that you may have to modify some or all of the functions contained n that source code.

Opening a text file for Reading

We discussed in class how to open a text file for reading. Here is the basic code, as a quick reminder:

#include <iostream.h>
#include <fstream.h>

int main(void)
   const char fileName[]="english.txt";

   fstream data;, ios::in);
   if (
      cout << "Error opening file: " << fileName << endl;
      // here goes the main code

Counting lines and characters

As discussed in class, we can not use redirection to read the characters from the text file if our goal is to count lines (why not ?). Instead, we have to use the get operation. Here is some code that counts all lines in the file. It asumes that the file is already opened correctly, and the corresponding fstream variable is called data.

int lineCounter = 0;
char cc = data.get();

while (!data.eof())
   if (cc == '\n')
   cc = data.get();

cout << "Number of lines in file: " << lineCounter << endl;