Question

without using map 1. Write a C++ program to find out the top 10 words in...

without using map

1. Write a C++ program to find out the top 10 words in terms of number of appearances in a given file, named “picasso.txt”. The data file is to be downloaded from iLMS system (http://lms.nthu.edu.tw). (Hint: The most efficient way to handle this problem is to build a word dictionary using class map in STL (Standard Template Library) if you know how to do it. On the other hand, without using map, it is still possible to solve this problem using only class vector in STL.) (Demonstrate it to one of your classmates, and then tell one of our TAs that you are done with this homework, and then you can get the 3 score points). (Results to be reported) Total no. of lines: 11 The total number of words in “picasso.txt”: 674 The 0-th frequent word is (the) with no. of appearances (87) The 1-th frequent word is (of) with no. of appearances (61) The 2-th frequent word is (and) with no. of appearances (48) The 3-th frequent word is (in) with no. of appearances (45) The 4-th frequent word is (a) with no. of appearances (38) The 5-th frequent word is (to) with no. of appearances (28) The 6-th frequent word is (Picasso) with no. of appearances (18) The 7-th frequent word is (was) with no. of appearances (17) The 8-th frequent word is (by) with no. of appearances (16) The 9-th frequent word is (is) with no. of appearances (15)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

C++ Code

#include <iostream>
#include <map>
#include <fstream>
using namespace std;

int main()
{
std::map<std::string, int> wordCount;
ifstream input;
input.open("test.txt");

int counter = 0;

std::string word;
while(input >> word)
{
counter++;
++wordCount[word];
}
std::map<int, string, std::greater<int> > numMap;
  
  

for (std::map<std::string, int>::iterator it = wordCount.begin(); it != wordCount.end(); ++it)
{
numMap.insert(make_pair(it->second, it->first ));

}

cout << " Total words: " << counter << endl << endl;

int output = 0;
for (std::map<int, string>::iterator it = numMap.begin(); it != numMap.end(); ++it)
{

  
cout << "The " << output << "- th frequent word is (" << it->second << ") with no. of appearances (" << it->first << ")" <<endl;
output++;

if(output == 10) {
return 0;
}

}
}

Output:-

Add a comment
Know the answer?
Add Answer to:
without using map 1. Write a C++ program to find out the top 10 words in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT