c++ Write 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 less than 20 words.

Respuesta :

Limosa

Answer:

The following are the program in the C++ Programming Language:

#include <iostream> //header file

#include <string>  //header file

#include <vector>  //header file

//name space

using namespace std;

//main function

int main() {

  vector<string> word; //set variable

  vector<int> c;  //set variable

  int s, counts = 0; //set variable

  string strng;  //set variable

  cout<<"Enter the size of string: "; //print message

  cin >> s; //get input

  cout<<"Enter the string: "; //print message

  for(int i = 0; i < s; ++i) {  //set for loop

     cin >> strng; //get input

     word.push_back(strng);

  }

  for(int i = 0; i < s; ++i) { //set for loop

         counts = 0;

         for(int j = 0;j<word.size();j++){ //set for loop

       if(word[j] == word[i]){ //set if statement

          counts++; //increament in counts

       }        

     }

      c.push_back(counts);

  }

  for(int i = 0; i < s; ++i) {

      cout << word[i] << " " << c[i] << endl;//print result

  }

  return 0;

}

Output:

Enter the size of string: 2

Enter the string: hello  sir

hello 1

sir 1

Explanation:

Here, we define three header files <iostream>, <String>, <vector> and namespace "std" after that we define main() method inside it.

  • Then, we set two vector type variables first one is string variable "word" and the second one is integer variable "c".
  • Then we set two integer variable "s" and "counts" and variable "counts" assign to 0.
  • Then, we get input from the user in variable "s" for the length of the loop.
  • Then, we set for the loop inside it, we get input in the variable "strng" from the user.
  • Then, we set the for loop for count the string repetition if any word comes again in the string then the value of count will increase.
  • Finally, we set for a loop to print the result.

In this exercise we have to write a C++ code requested in the statement, like this:

find the code in the attached image

We can write the code in a simple way like this below:

#include <iostream>

#include <string>

#include <vector>

using namespace std;

//main function

int main() {

 vector<string> word; //set variable

 vector<int> c;  //set variable

 int s, counts = 0; //set variable

 string strng;  //set variable

 cout<<"Enter the size of string: "; //print message

 cin >> s; //get input

 cout<<"Enter the string: "; //print message

 for(int i = 0; i < s; ++i) {  //set for loop

    cin >> strng; //get input

    word.push_back(strng);

 }

 for(int i = 0; i < s; ++i) { //set for loop

        counts = 0;

        for(int j = 0;j<word.size();j++){ //set for loop

      if(word[j] == word[i]){ //set if statement

         counts++; //increament in counts

      }        

    }

     c.push_back(counts);

 }

 for(int i = 0; i < s; ++i) {

     cout << word[i] << " " << c[i] << endl;//print result

 }

 return 0;

}

See more about C++ at brainly.com/question/19705654

Ver imagen lhmarianateixeira