I need to display the top ten words that the program reads from the file.
but I have no idea how to start printing the top ten words and tell the user how many times the word has been used. can anyone help me? thanks!
What I don't see you doing is incrementing the corresponding instance of count if you find the word.
Line 60:
1 2 3 4
else
{ // found word
count[pos]++;
}
Now the interesting part is going to be to find the top 10 words.
One way to do that would be to keep a multimap<int,int>.
The first int (key) is the number of times a word occurs.
A multimap allows for more than one word to have the same number of occurrances.
The second int is the position of the word in your vector.
You want to build the multimap after you've passed the entire file and know the counts of each word. To print the top 10 words, simply iterate from the end of the multimap for 10 occurrances.
i applied your code, thank you. but i have another question, why does when i implement this code, it gives me such high numbers i dont even know how it got it. shouldnt it supposed to sort from smallest to biggest and print the numbers?
The sort statement is going to reorder your count array.
That will cause you to lose the positional relationship with the word vector.
i.e. Assume that "Fred" is the 5th word and occurs once. Sorting count will cause the count in the 5th position to go to the front of the count vector. Now, how do you now that refers to Fred?
You're still sorting count and losing the association between the count's position in the vector and the position of the word in the word vector.
Additionally, you're now sorting the word vector. What do you think the result of that will be? It will order the word vector in alphabetical order, which will also have no relation to the count vector.