### Use a loop to traverse each character in the input string

Assignment Help Basic Computer Science
##### Reference no: EM13306977

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<char> (holds the list of characters from the

input string once per character) and vector<int> (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 <iostream>

#include <iomanip>

#include <string>

#include <vector>

using namespace std;

// FUNCTION PROTOTYPES GO HERE:

string getPhrase(const string & prompt);

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

void updateFreq(vector<int> & list, int index);

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

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

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

int main()

{

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

#### Questions Cloud

 How are investors able to interpret ratings : 1. What are your thoughts about reinvestment rate risk, and how this can be related to interest rate risk. In addition, is there a connection between rating risk and credit/default risk? Typically, how are investors able to interpret ratings.. Write a program that creates a binary tree from an array : Write a program that creates a Binary Tree from an array of integers, goes through it using inorder traversal (saving that to another array), then makes another balanced binary tree. What would be magnitude of coresponding forces on full scale : Offshore oil exploration platform is expected to encounter current of 3 m/s wave height 4 m and periods 15 seconds. A 1 in 20 scale modes is to be build and tested in hydraulic laboratory to determine the forces on the full scale structure. Determine an equation of motion for this oscillator : A particle executes linear harmonic motion in one dimension with a frequency of 0.25 Hz about the point x=0. Determine an equation of motion for this oscillator Use a loop to traverse each character in the input string : 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. Find the speed of the soccer ball : The momentum of a soccer player is 100kg-m/sec and he hits a soccer ball with a mass 0.1 kg, find the speed(velocity) of the soccer ball Calculate minimum-sized sand particle that would be removed : Calculate the minimum-sized sand particle that would be removed in the chamber. To be removed, the particles must settle the depth of 2 m during the period the wastewater spends in the chamber (dT=V/Q). What are some indications that investors are risk averse : What are some indications that investors are risk averse? How would you as a portfolio manager support these investors? What kind of recommendations would you make? What would you recommend as a portfolio manager to reduce the risk for a risk a.. How much energy is liberated : Consider the nuclear reaction: (2/1)H + (9/4)Be --> (7/3)Li + (4/2) He. How much energy is liberated

### Write a Review

#### Sort the array and write out the sorted array

Consider a program that will read employee information into an array of objects, sort the array by employee identification number, write out the sorted array, and compute various statistics on the data, such as the average age of an employee.

#### Employees may work from home

Employees may work from home up to 15 hours per week. Each project leader needs a list of current means of contacting each member of the project team: home phone and home FAX number. To contact employees "on the road" the project leaders also need..

#### Write program to enter two separate numbers

Write a program which would permit a user to enter two separate numbers and choose one of four mathematical operations (add, subtract, multiply, divide).

#### Define a specialized representation language

Define a specialized representation language to describe the activities of a public library. This language will be a set of concepts and relations using conceptual graphs. Do the same thing for a retail business.

#### You enable completeonly specific input element true or false

You can enable auto complete only for specific input element. True or False

#### Problems of understanding natural languages

Briefly discussed the problems of understanding natural languages as opposed to formal programming languages and describe how the problem of traveling from one city to another could be framed as a production system. What are the states? What are the..

#### Using java create a basic coin-flip guessing game

Using JAVA create a basic coin-flip guessing game. The game should prompt the player to choose heads or tails, flip a virtual coin and then display the results to the player.

#### Swimlane-hypothesis space

Assignment need to be done. It is about swimlane. I am attaching document and example of how it suppose to be done.

#### What operations can you perform on pointers

How would you initialize a pointer that you do not want to point to anything? What is a null pointer and how do you create it?

#### Manufacturer of fine cosmetic soap products

Handcrafted Soap is a manufacturer of fine cosmetic soap products. Sweet Treats' two main products are soap and cleanser. Each batch of soap consumes 50 pounds of glycerin and 30 pounds of oil. Each batch of cleanse consumes 20 pounds of glycerin and..

#### Why does segment sequence number remain unchanged

Why does the Segment Sequence Number remain unchanged (indicated by a horizontal line in the graphs) with every drop in the congestion window?

#### The commonly used password management protocol

Analyze one of the commonly used password management protocol  what is the weakness of this protocol?