Write a program that reads a list of words. Then, the program outputs those words and their frequencies. If the input is 5 hey hi Mark hi mark (the first number indicates the number of words that follow), the output is:

hey 1
hi 2
Mark 1
hi 2
mark 1

Hint: Use two vectors, one for the strings, another for the frequencies.

Your program must define and use the following function: int GetFrequencyOfWord(vector wordsList, string currWord)

Respuesta :

Answer:

/ declare the necessary header files.

#include <iostream>

#include <string>

#include <vector>

using namespace std;

// declare the main function.

int main()

{

// declare a vector.

vector<string> words;

vector<int> counts;

// declare variables.

int size;

string str;

cin >> size;

// start the for loop.

for(int i = 0; i < size; ++i)

{

// input string.

cin >> str;

words.push_back(str);

}

// start the for loop.

for(int i = 0; i < size; ++i)

{

int count = 0;

// start the for loop.

for(int j = 0; j < words.size(); ++j)

{

// check the condition.

if(words[j] == words[i])

{

count++;

}

}

counts.push_back(count);

}

// start the for loop.

for(int i = 0; i < size; ++i)

{

// display result on console.

cout << words[i] << "\t" << counts[i] << endl;

}

return 0;

}

Explanation: