7 Replies - 380 Views - Last Post: 04 March 2012 - 11:45 AM Rate Topic: -----

#1 nlamba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 12

Question With c++

Posted 03 March 2012 - 06:59 PM

Write a program that reads in words from cin (ending with EOF, i.e. ctrl-d, as usual) and then prints the word that occurs most frequently (and also print its frequency count). If more than one word ties for the most frequent, then print each of those words in alphabetical order.

I have tired many thing, but the alphabet that it shows it sometimes wrong. But I am pretty sure the count is good, as in what is the most frequent one.

 void ques1(){
vector<string> word;
string w;
int count = 1;
vector<string> word2;

cout << "Please enter some words: ";
while (cin >> w){
word.push_back(w);
}//while

int x = 0;
sort(word.begin(),word.end());
word2.push_back(word[0]);
for(int i = 1;i < word.size();i++){

if(word[i] == word[i-1]){
count++;
if(i == 1){
x = count;
}
}
else if(count > x){
word2.clear();
word2.push_back(word[i]);
x = count;
count = 1;
}
else if(count == x){
word2.push_back(word[i]);
count = 1;
}
else { 
count = 1;
}
if(i == word.size() - 1){
if(count > x){
word2.clear();
word2.push_back(word[i]);
x = count;
count = 1;
}
else if(count == x){
word2.push_back(word[i]);
count = 1;
}
}//if

}//for

cout << endl << "The most frequently occuring words are: " << endl << endl;
for(int i = 0; i < word2.size();i++){
cout << x << " " << word2[i] << endl;
}//for
}//ques1




Is This A Good Question/Topic? 0
  • +

Replies To: Question With c++

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,551
  • Joined: 27-December 08

Re: Question With c++

Posted 03 March 2012 - 07:34 PM

Can you be more specific in your description of your problems or errors?
Was This Post Helpful? 0
  • +
  • -

#3 nlamba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 12

Re: Question With c++

Posted 03 March 2012 - 07:37 PM

View Postmacosxnerd101, on 03 March 2012 - 07:34 PM, said:

Can you be more specific in your description of your problems or errors?


When i run this program, it does not output the correct word. I have no idea what I am doing wrong.
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1530
  • View blog
  • Posts: 5,519
  • Joined: 03-August 09

Re: Question With c++

Posted 03 March 2012 - 08:16 PM

learn to format your code; I took the liberty of formatting it for you. In future posts you should do so. you can have a look at this link which goes over several formatting styles. also A-Style has an option called 'java' which I use; there are more styles to be had than just whats on that page.

void ques1() 
{
	vector<string> word;
	string w;
	int count = 1;
	vector<string> word2;

	cout << "Please enter some words: ";
	while (cin >> w) {
		word.push_back(w);
	}//while

	int x = 0;
	sort(word.begin(),word.end());
	word2.push_back(word[0]);
	for(int i = 1;i < word.size();i++) {
		if(word[i] == word[i-1]) {
			count++;
			if(i == 1){
				x = count;
			}
		} else if(count > x) {
			word2.clear();
			word2.push_back(word[i]);
			x = count;
			count = 1;
		} else if(count == x) {
			word2.push_back(word[i]);
			count = 1;
		} else { 
			count = 1;
		}
		if(i == word.size() - 1) {
			if(count > x){
				word2.clear();
				word2.push_back(word[i]);
				x = count;
				count = 1;
			} else if(count == x) {
				word2.push_back(word[i]);
				count = 1;
			}
		}//if

	}//for
	cout << endl << "The most frequently occuring words are: " << endl << endl;
	for(int i = 0; i < word2.size();i++) {
		cout << x << " " << word2[i] << endl;
	}//for
}//ques1


I formatted this in K&R which is closet to my personal style.

This post has been edited by ishkabible: 03 March 2012 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 nlamba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 12

Re: Question With c++

Posted 03 March 2012 - 10:11 PM

View Postishkabible, on 03 March 2012 - 08:16 PM, said:

learn to format your code; I took the liberty of formatting it for you. In future posts you should do so. you can have a look at this link which goes over several formatting styles. also A-Style has an option called 'java' which I use; there are more styles to be had than just whats on that page.

void ques1() 
{
	vector<string> word;
	string w;
	int count = 1;
	vector<string> word2;

	cout << "Please enter some words: ";
	while (cin >> w) {
		word.push_back(w);
	}//while

	int x = 0;
	sort(word.begin(),word.end());
	word2.push_back(word[0]);
	for(int i = 1;i < word.size();i++) {
		if(word[i] == word[i-1]) {
			count++;
			if(i == 1){
				x = count;
			}
		} else if(count > x) {
			word2.clear();
			word2.push_back(word[i]);
			x = count;
			count = 1;
		} else if(count == x) {
			word2.push_back(word[i]);
			count = 1;
		} else { 
			count = 1;
		}
		if(i == word.size() - 1) {
			if(count > x){
				word2.clear();
				word2.push_back(word[i]);
				x = count;
				count = 1;
			} else if(count == x) {
				word2.push_back(word[i]);
				count = 1;
			}
		}//if

	}//for
	cout << endl << "The most frequently occuring words are: " << endl << endl;
	for(int i = 0; i < word2.size();i++) {
		cout << x << " " << word2[i] << endl;
	}//for
}//ques1


I formatted this in K&R which is closet to my personal style.



Thank you :) i learned something
Was This Post Helpful? 0
  • +
  • -

#6 akhena  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 69
  • Joined: 20-January 11

Re: Question With c++

Posted 04 March 2012 - 10:14 AM

I didn't look hard enough in your code probably. But personnally I would suggest to use some algorithms from the stl. You might find usefull the std::equal_range function.

My personnal algorithm to solve this probably goeas basically like this :
- create one "begin" iterator pointing at the begining of the word vector and one "end" iterator pointing at the last element.
- while the begin iterator has not reached the end iterator
- get the bounds with equal_range() for the value pointed by begin iterator.
- the count is the distance between the bounds iterators.
- if count > max : clear your word2 vector and push_back the value of begin iterator. max = count.
- else if count == max : push_back in word2 the value of begin iterator
- make begin iterator equal to the upper bound if the bounds pair of iterator.
- and repeat with the while loop.

PS : I'm no expert. Just a hobbyist who found your problem interesting and gave it a try. :)
Was This Post Helpful? 1
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3876
  • View blog
  • Posts: 11,415
  • Joined: 18-April 07

Re: Question With c++

Posted 04 March 2012 - 11:37 AM

Start x at 1 and then take out these lines...

if (i == 1) {
   x = count;
}



They are not needed. This causes x to always be reset. X should only ever be updated when it has been determined that count is greater than X.

Here we set X = 1 to start because there is always going to be 1 count of a word. Then when words are the same, we update just "count". When the word ends up being different, that is when we determine if count > x and if so, now set X = count, reset count, clear the word2 vector and push on the new word. If count == x then we simply push the word onto the word2 list and reset count.

:)
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: Question With c++

Posted 04 March 2012 - 11:45 AM

Are you required to use a vector of strings for this assignment? If not, you should look into the std::map associative container. It holds pairs of data which you specify when you create a map. So to declare a map to count word frequencies, it would look something like:
#include <map>

int main()
{
     std::map<std::string, int> wordCount;
     std::string word;
     std::cout << "Enter word> ";
     std::cin >> word;
     // This increments the int part using the string as an index into the map:
     wordCount[word]++;
}


This is just a simple example, there are member functions of class map to help you perform other operations. One thing to keep in mind is that if you try to access the map using a string that isn't in the map, then that string is inserted into the map, and the integer is default constructed to zero.

Here is a link to a reference page for std::map.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1