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

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 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