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()

      {

      // 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

Reference no: EM13306977

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

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Use c++ write program that to factorial the prompted integer

use C++ write program that to factorial the prompted integer in the output

  Choose one schedule development tools

In this discussion, choose one of the schedule development tools introduced or another tool that you are already familiar with that was not identified in the studies. Identify the tool and complete the following.

  Explain decrease in memory cost and push to keep data

Explain the apparent contradiction between the decrease in memory cost and the push to keep a single copy of Explain decrease in memory cost and the push via the paradigm of deduplication.

  Describe and discuss

Describe and discuss how the redesigned network needs to operate, the possible network design solutions and why the new design is more suitable for the introduction of mobile devices.

  How to boost over company-s former speed champ

Explain the definition (or definitions) of performance which you think the company utilzed. Do you believe the news release is misleading?

  Explaining quasi-legal activities over internet

Online gambling and buying of pharmaceutical drugs over the Internet are quasi-legal activities which can only take place as Internet allows the parties offering gambling.

  Determine whether value parameter is within specified range

The intended purpose of the following module is to determine whether the value parameter is within a specified range. The module will not work, however. Find the problem

  It department staffing easier-less expensive as technologies

IT department staffing should become easier and less expensive as technologies simplify and become more mainstream. Agree or disagree and why?

  Explain worst possible reports from system

Imagine worst possible reports from a system. What is wrong with them? Write as many problems as you can. What are the consequences of such reports?

  Give minimal dfa that performs as a mod

Give minimal DFA that performs as a mod-3 1's counter, i.e, outputs a 1 each time the number of 1's in the input sequence is a sequence is a multiple of 3.

  Twenty-four voice signals

Twenty-four voice signals are to be multiplexed and transmitted over twisted pair. What is the bandwidth required (in bps) if synchronous time division multiplexing is used, if we use the standard analog to digital sampling rate, and if each sample i..

  Creating an oracle

The clique problem itself is NP-hard. Thus you may not wish to have an oracle that runs in exponential time. Consider approach to approximate the solution.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd