Write the program for displaying the ten most frequent words
in a file such that your program should be efficient in all
complexity measures.

Answers were Sorted based on User's Feedback



Write the program for displaying the ten most frequent words in a file such that your program shoul..

Answer / kaka189

can u plzz explain your logic?

Is This Answer Correct ?    5 Yes 3 No

Write the program for displaying the ten most frequent words in a file such that your program shoul..

Answer / rob lange

Here ya go... It's not pretty...

#include <iostream>
#include <fstream>
#include <string>
#include <hash_map>
#include <algorithm>
using namespace std;


int main()
{
string line, file;
stdext::hash_map <std::string, int> words;
stdext::hash_map <std::string, int>::iterator wordsit;
ifstream myfile("example.txt");

// Load file into string
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline(myfile,line);
file.append( line );
file.append( " " );
}
myfile.close();
}

// Parse words into hashmap
char * threadsafe;
char * token = strtok_s((char*)file.c_str(), " ", &threadsafe);
while (token != NULL)
{
wordsit = words.find(token);
if (wordsit != words.end())
++wordsit->second;
else
words[token] = 1;

token = strtok_s(NULL, " ", &threadsafe);
}

// find top 10 threshold value
std::vector < int > topvalue;
int threshold = 0;
for ( wordsit = words.begin(); wordsit != words.end();
wordsit++ )
{
topvalue.push_back( wordsit->second );
}
sort( topvalue.begin(), topvalue.end() );
reverse( topvalue.begin(), topvalue.end() );

if ( topvalue.size() > 10 )
threshold = topvalue[9];

// Search hashmap against value and print word if its
within top10 threshold...
// ties also get printed, so the list might be longer than 10
for ( wordsit = words.begin(); wordsit != words.end();
wordsit++ )
{
if( wordsit->second >= threshold )
cout << wordsit->first << endl;
}
return 0;
}

Is This Answer Correct ?    5 Yes 4 No

Write the program for displaying the ten most frequent words in a file such that your program shoul..

Answer / mayank maheshwari

Hi,

you can do it in a cleaner way using STL map and getting rid
of all the messy strtok() functions. Just writing how to
construct the map. The idea of putting into a vector and
sorting can still work.

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

int main(int argc,char*argv[])
{
string word;
map <string, int> freq;
map <string, int>::const_iterator wordsit;
fstream myfile;
myfile.open(argv[1],ios::in);

// Load file into string
if (myfile.is_open())
{
while (myfile >> word)
{ freq[word]++;
}
myfile.close();
}

//To see the map created
for (wordsit=freq.begin();wordsit!=freq.end();wordsit++)
{cout<<"Key: "<<wordsit->first<<"Value:
"<<wordsit->second<<endl;
}

Is This Answer Correct ?    3 Yes 5 No

Post New Answer

More C Interview Questions

How do you determine whether to use a stream function or a low-level function?

0 Answers  


Write a programme to find even numbers without using any conditional statement?

3 Answers   ADD Software, Infosys,


What is omp_num_threads?

0 Answers  


write a program to find lcm and hcf of two numbers??

1 Answers  


Write a C program that computes the value ex by using the formula ex =1+x/1!+x2/2!+x3+3!+………….

1 Answers  






What is a null pointer in c?

0 Answers  


a character variable can at a time store a) 1 character b) 8 characters c) 254 characters d) none of the above

3 Answers  


How can you call a function, given its name as a string?

0 Answers  


advantages of pointers?

3 Answers  


Where is volatile variable stored?

0 Answers  


a direct address that identifies a location by means of its displacement from a base address or segment a) absolute address b) relative address c) relative mode d) absolute mode

0 Answers  


Explain the difference between call by value and call by reference in c language?

0 Answers  


Categories