Write a program in the file freqcpp which reads


Write a program in the file freq.cpp which reads a string from the user and computes the frequency of each letter that appears in the string. The frequency of a letter is the number of times it appears in the string. For example, the letter s in the string "Mississippi" has a frequency of four. The frequency of the letter M has a frequency of one. Your program will use two vectors to account for each letter in the input string and its frequency. One vector will store each letter that appears in the input string at most once and the other vector will store the frequency of the letter at the same index location. Note that the two vectors will have the same size. Also note that we will not compute the frequency of the space character (these will be ignored) if it is entered and will treat the input as case sensitive. You can assume that the user will not type any other white space character, such as a tab, or invisible characters such as control characters.

  1. All functions should be written AFTER the main procedure.

  2. The function prototypes are provided for you and must not be changed in any way. They

    must be placed BEFORE the main procedure.

  3. Do NOT add more functions to the solution.

  4. Each function should have a comment explaining what it does.

  5. Each function parameter should have a comment explaining the parameter.

  6. Write the procedure main() by following the algorithm contained within its code body.

    Determine how to call the functions indicated in the comments.

  7. Declare two vector variables as type vector (holds the list of characters from the

    input string once per character) and vector (holds corresponding frequencies of that character that are stored in the first list). Note that these two lists will have the same size and must correspond. For example, if letter m is stored at index 5 of the vector of characters then its corresponding frequency is stored at index 5 of the vector of integers.

  8. Use the COLUMNWIDTH constant when setting the width of your fields for displaying your formatted results.

  9. Declare and initialize any other variables you may need.

  10. Prompt the user using the getPhrase() function. This function has one input string that is the

    prompt message displayed to the user. The function returns the string entered by the user.

  11. Next, the procedure main() will compute the frequencies of the letters in the input string:

    a. Use a loop to traverse each character in the input string.
    b. If the character is a space then skip it and continue to the next character in the input.

    If the character is not a space then continue as follows:
    c. Use the function charIndex() to determine which index location the character is

    stored in the vector of characters. The function takes the vector and character as input and returns the index location of the character in the vector or -1 if the character is not in the vector. Note that a character from the input will only be stored ONCE in the vector and in the order that it appears in the input.

    d. If the character is not in the vector of characters, then use the function addLetter() to add the character to the vector of characters at the end of the vector and also add a frequency (count) of 1 to the vector of integers at the end of the vector. The function takes both vectors and the character as input and returns nothing.

    e. If the character is in the vector of characters already, then use the function updateFreq() instead to add 1 to the frequency of this letter. This function takes asinput the vector of integers and the index for the location of the frequency (count) of this letter. Increment this letter's frequency by 1 at this index location to account for its occurrence again in the input string.

    f. After the loop has completed accounting for all of the letters in the input string, display the contents of the vector of characters and the vectors of integers to the screen using the functions displayLetters() and displayFreqs().

    •  The function displayLetters() takes a vector of characters and a column width for formatting. The function will display the contents of the vector of characters, where each character is displayed in the same order as it is stored in the vector. Format the display of each character with the column width using setw and separate each character with a comma (see solution executable) except for the last character.

    •  The function displayFreqs() takes a vector of integers containing frequencies and a column width for formatting. The function will display the contents of the vector of frequencies, where each integer is displayed in the same order as it is stored in the vector. Format the display of each integer with the column width using setw and separate each integer with a comma (see solution executable) except for the last integer. Note each character displayed using displayLetters() must be aligned with each corresponding frequency displayed usingdisplayFreqs().

    1. TEST YOUR CODE THOROUGHLY. Determine boundary cases such as when the user just hits the enter key or types only spaces or leading spaces. Your program must not have a run- time error.

       

      Here is a template :

      #include

      #include

      #include

      #include

       

      using namespace std;

       

      // FUNCTION PROTOTYPES GO HERE:

       

      string getPhrase(const string & prompt);

      int charIndex(const vector & list, char letter);

      void updateFreq(vector & list, int index);

      void addLetter(vector & letters, vector & freqs, char letter);

      void displayLetters(const vector & letters, const int colwidth);

      void displayFreqs(const vector & freqs, const int colwidth);

       

      int main()

      {

      // Define your local variables

      const int COLUMNWIDTH = 2;

      // Prompt the user for the input string by calling function getPhrase

      // Loop through each character in the input phrase and update vectors

      // Call the functions charIndex, updateFreq, and addLetter here

       

      // Display letters and frequencies by caling functions displayLetters and displayFreqs

       

      return 0;

      }

      // FUNCTION DEFINITIONS GO HERE

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Write a program in the file freqcpp which reads
Reference No:- TGS0645666

Expected delivery within 24 Hours