Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9886298

Have any Question?


Related Questions in Computer Engineering

Can someone help me with the following java question

Can someone help me with the following java question please (a) Implement a recursive search function in Java int terSearch(int arr[], int l, int r, int x) that returns location of x in a given sorted array arr[l...r] is ...

Question rivests distinguished point dp method is a

Question : Rivest's "distinguished point" (DP) method is a variable length hash chain where all chain end points have the same d-bit suffix. In the precomputation phase, a chain is computed until a value is output with t ...

Sub classassume super class and methods defined in question

Sub Class Assume Super Class and methods defined in question VIII Assume a Sub class named RunSomeSuper Write the Begin method Construct an instance of SomeSuper with parms of your choosing Just the Begin method, not the ...

To review stacksdirections write java code that prompts the

To review stacks Directions : Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the bas ...

Assume the following information for an imaginary closed

Assume the following information for an imaginary, closed economy. GDP = $100,000; taxes = $22,000; government purchases = $25,000; national saving = $15,000. Refer to Scenario 26-1.  For this economy, investment amounts ...

Explain that when an unauthorized individual gains access

Explain that when an unauthorized individual gains access to the information an organization trying to protect, that act is categorized as a deliberate act of espionage or trespass.

Please discuss the followingas demand increased for these

Please discuss the following: As demand increased for these mortgage backed securities, lenders reacted by relaxing their approval standards to increase production. No longer were "all" borrowers required to document the ...

An equally weighted portfolio consists of 64 assets which

An equally weighted portfolio consists of 64 assets which all have a standard deviation of 0.276. The average covariance between the assets is 0.106. Compute the standard deviation of this portfolio. Please enter your an ...

The following table records the number of days the stock

The following table records the number of days the stock market recorded the following outcomes: # of Days NASDAQ Up NASDAQ Down NASDAQ Unchanged DJIA Up 30 15 4 DJIA Down 10 40 3 DJIA Unchanged 3 5 2 What is the probabi ...

Question suppose you are constructing a 3-bit counter using

Question : Suppose you are constructing a 3-bit counter using three D flip-flops and some selection of gates. The inputs to the system are Reset (sets every output to zero) and Inc (a signal to increment the counter). Th ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As