Knowee
Questions
Features
Study Tools

7.22 LAB: Word frequenciesWrite a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain fewer than 20 words.Ex: If the input is:5 hey hi Mark hi markthe output is:hey - 1hi - 2Mark - 1hi - 2mark - 1Hint: Use two vectors, one vector for the strings and one vector for the frequencies.

Question

7.22 LAB: Word frequenciesWrite a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with an integer indicating the number of words that follow. Assume that the list will always contain fewer than 20 words.Ex: If the input is:5 hey hi Mark hi markthe output is:hey - 1hi - 2Mark - 1hi - 2mark - 1Hint: Use two vectors, one vector for the strings and one vector for the frequencies.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to write a program that reads a list of words and outputs those words and their frequencies:

  1. First, you need to include the necessary libraries. In this case, you will need to include the vector and string libraries.
#include <iostream>
#include <vector>
#include <string>
  1. Next, declare your main function.
int main() {
  1. Inside the main function, declare an integer variable to hold the number of words that will be inputted. Also, declare two vectors: one to hold the words and another to hold the frequencies.
    int numWords;
    std::vector<std::string> words;
    std::vector<int> frequencies;
  1. Use std::cin to get the number of words from the user.
    std::cin >> numWords;
  1. Create a loop that will run for the number of words specified by the user. Inside this loop, get each word from the user and add it to the words vector. Also, initialize the corresponding frequency to 1.
    for (int i = 0; i < numWords; i++) {
        std::string word;
        std::cin >> word;
        words.push_back(word);
        frequencies.push_back(1);
    }
  1. Now, create a nested loop to calculate the frequencies of the words. The outer loop will iterate over each word, and the inner loop will compare the current word with all the other words. If a match is found, increment the corresponding frequency.
    for (int i = 0; i < numWords; i++) {
        for (int j = i + 1; j < numWords; j++) {
            if (words[i] == words[j]) {
                frequencies[i]++;
                frequencies[j]++;
            }
        }
    }
  1. Finally, create a loop to output the words and their frequencies.
    for (int i = 0; i < numWords; i++) {
        std::cout << words[i] << " - " << frequencies[i] << std::endl;
    }
  1. Close the main function.
    return 0;
}

This program will correctly calculate and output the frequencies of the words inputted by the user.

This problem has been solved

Similar Questions

You are given a string word and an integer k. We consider word to be k-special if |freq(word[i]) - freq(word[j])| <= k for all indices i and j in the string. Here, freq(x) denotes the frequency of the character x in word, and |y| denotes the absolute value of y. Return the minimum number of characters you need to delete to make word k-special. Example 1: Input: word = "aabcaba", k = 0 Output: 3 Explanation: We can make word 0-special by deleting 2 occurrences of "a" and 1 occurrence of "c". Therefore, word becomes equal to "baba" where freq('a') == freq('b') == 2. Example 2: Input: word = "dabdcbdcdcd", k = 2 Output: 2 Explanation: We can make word 2-special by deleting 1 occurrence of "a" and 1 occurrence of "d". Therefore, word becomes equal to "bdcbdcdcd" where freq('b') == 2, freq('c') == 3, and freq('d') == 4. Example 3: Input: word = "aaabaaa", k = 2 Output: 1 Explanation: We can make word 2-special by deleting 1 occurrence of "b". Therefore, word becomes equal to "aaaaaa" where each letter's frequency is now uniformly 6. Constraints: 1 <= word.length <= 105 0 <= k <= 105 word consists only of lowercase English letters. c++

Create a list b with elements 'apple', 'banana', 'cherry', and concatenate them into a single string using a space as a separator. Count the occurrences of the word 'is' in the string a. Create a dictionary d with word frequencies from the list b.

Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']

Write a function top5_words(text) that takes a single argument text (a non-empty string), tokenises text into words based on whitespace (once again, without any stripping of punctuation or case normalisation), and returns the top-5 words as a list of strings, in descending order of frequency. If there is a tie in frequency at any point, the words with the same frequency should be sub-sorted alphabetically (e.g. if 'turtle' and 'grok' both occur 5 times, 'grok' should come first). If there are less than five distinct words in text, the function should return all words in descending order of frequency (with the same tie-breaking mechanism). For example:>>> top5_words("one one was a racehorse two two was one too")["one", "two", "was", "a", "racehorse"]>>> top5_words("buffalo buffalo buffalo chicken buffalo")["buffalo", "chicken"]>>> top5_words("the quick brown fox jumped over the lazy dog")["the", "brown", "dog", "fox", "jumped"]

Write a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input   1 : 6                1 2 2 3 3 3Output 1 : 1 - 1                2 - 2                3 - 3Input   1 : -6Output 1 : Invalid InputExplanation:  NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 61 2 2 3 3 31 - 12 - 23 - 32 -6Invalid Input

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.