Discussion

Frequency analysis is important for decrypting secret messages. In a simple substitution cipher, the frequency characteristics of the original letters are still part of the encrypted message, so one could apply frequency analysis to try and decrypt the message. The 6 most frequently occurring letters in the English language are shown in the following table along with their relative frequencies. See image.

Problem

Write a class named letter_frequencies that represents the frequency of letters for a given string of an unspecified length.

A text file containing words, delimited by whitespace (spaces and newlines).

Output

A formatted, vertical star (*) plot of the frequencies of the letters used in the file in the order from most frequent to less frequent. Here is a portion of a typical plot for the top 4 letters of the alphabet for some string. Your plot must show all 26 letters. See image.

An annotated display of the original string using operator <<.

Minimum requirements

  • A class named letter_frequencies with at least the following members.
    • A constructor that takes std::string representing the string that was read from the file.
    • Overriden value semantics.
    • Overriden destructor.
    • A constant member that gets the frequency of its character argument. It should have a declaration as follows. size_type get_ltr_freq(const char) const;
    • An overloaded operator << that displays the letter frequencies as follows. The example is for the first 3 characters on each line. The operator should display 13 characters per line with a column width of 4 and left justification. Use two newlines between each major line.
    • A darray of letter/frequency pairs.
  • A struct named ltr_freq_pair that has has two members only—a char and a size_type to represent a character and its frequency.
  • A global function that plots the letter frequencies. Here is the declaration. void plot_letter_frequencies(const letter_frequencies&); Notice that you will have to sort your dynamic array based on frequency, and that the output is formatted. See below for a sorting algorithm.

Driver requirements

  • Present a greeting to the user (global function) which pauses and asks for a keypress and ENTER to continue.
  • Read the text file from the command line in argv[1].
  • Instantiate the letter_frequencies class and compute the letter frequencies.
  • Output the instance using operator << with an annotation.
  • Produce a vertical star plot of the letter frequencies with the global function and an annotation.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.