Hello ladies and gents,

I'm trying to do another exercise from Accelerated C++ in wich I have to write a program to count how many times each distinct word appears in its input.

What I have untill now is this,

int main()
{
	vector<string> theWords;
	string aWord;
	short count;
	short amount = 0;

	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	cout << "Amount of words is " << count << endl;

	for (int i = 0; i < count; ++i)
		for (int j = 0; j <= i; ++j)
			if (theWords[i] != theWords[j])
			{
				theWords[i] = theWords[j];
				++amount;
			}

	cout << "Different amount of words is " << amount;

	for (i = 0; i < amount; ++i)
		cout << theWords[i] << "\n";
			
	cout << "\n";

	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I'm getting the correct amount of different words, but, I'm having a problem in getting them displayed, I only get the first word in the list.

I know it has to do with this theWords = theWords[j];
but don't know how to get it right?

Could someone help me out. Thanks

Member Avatar for iamthwee

Hello ladies and gents,

I'm trying to do another exercise from Accelerated C++ in wich I have to write a program to count how many times each distinct word appears in its input.

What I have untill now is this,

int main()
{
	vector<string> theWords;
	string aWord;
	short count;
	short amount = 0;

	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	cout << "Amount of words is " << count << endl;

	for (int i = 0; i < count; ++i)
		for (int j = 0; j <= i; ++j)
			if (theWords[i] != theWords[j])
			{
				theWords[i] = theWords[j];
				++amount;
			}

	cout << "Different amount of words is " << amount;

	for (i = 0; i < amount; ++i)
		cout << theWords[i] << "\n";
			
	cout << "\n";

	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I'm getting the correct amount of different words, but, I'm having a problem in getting them displayed, I only get the first word in the list.

I know it has to do with this theWords = theWords[j];
but don't know how to get it right?

Could someone help me out. Thanks

here is one hasty solutionh, but it do not check 4 repeats. shud b simple tho. :cool:

#include<iostream.h>
#include<string.h>
#include<vector.h>
using namespace std;

int main()
{
vector<string> theWords;
string aWord;
short count;


for(int i=0; i<5; i++)
{
cin>>aWord;
theWords.push_back(aWord);
}

count = theWords.size();

cout << "Amount of words is " << count << endl;
int amount=0;
for (int i = 0; i < count; i++)
{
for (int j = 0; j<count; j++)
{
if (theWords == theWords[j])
{
amount++;
}
}
cout<<theWords<<" occured "<<amount<<" times"<<endl;
amount=0;
}

cout << "Different amount of words is " << amount;

for (int i = 0; i < amount; ++i)
cout << theWords << "\n";

cout << "\n";

std::cout << "Press enter to exit.\n";
std::cin.ignore(2, '\0');

return 0;
}

from memory, that exercise is in the chapter on associative arrays.

If you think about how a map (or 2) might be useful, you will find the task much simpler than using vectors.

Thanks for the help iamthewee, but, the count isn't the problem I have, it's putting the several words in the vector without the doubles.

from memory, that exercise is in the chapter on associative arrays.

If you think about how a map (or 2) might be useful, you will find the task much simpler than using vectors.

The exercise is in part 3, in wich you start working with batches of data, there is no explanation whatsoever about using maps, queus, ... The only example you get is a program in wich student grades are calculated.

Still haven't found the solution though.

Member Avatar for iamthwee

Thanks for the help iamthewee, but, the count isn't the problem I have, it's putting the several words in the vector without the doubles.

The exercise is in part 3, in wich you start working with batches of data, there is no explanation whatsoever about using maps, queus, ... The only example you get is a program in wich student grades are calculated.

Still haven't found the solution though.

I don't think the question lends itself to a trivial solution, checking for repeats is not as easy as u think, well not easy to do so efficiently. Perhaps, using kon_t's idea would be to use a map, or list container. Then employ something like

std::unique()

which should remove duplicates from the container. However, u won't no wats going on behind the function. Which may defeat the purpose of the exercise :o

Got it:

One solution is:

int main()
{
	vector<string> theWords;
	string aWord;
	short count, amount = 1;
	
	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	sort(theWords.begin(), theWords.end());

	for (int i = 0; i < count-1; ++i)
	{
		if (theWords[i] == theWords[i+1])
		{
			amount++;
		}
		else
		{
			cout << theWords[i] << " appears " << amount << " times." << endl;
			amount = 1;
		}
	}

	cout << theWords.back() << " appears " << amount << " times." << endl;

	cout << "Amount of words is " << count << endl;
			
	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I was just wondering what I could change this to make it better?

Meaning for instance, I have to use the count -1 in the loop, because, otherwise, I would go out of bound. Is there another way of solving that?

Thanks

Member Avatar for iamthwee

Got it:

One solution is:

int main()
{
	vector<string> theWords;
	string aWord;
	short count, amount = 1;
	
	while(cin >> aWord)
		theWords.push_back(aWord);
	
	count = theWords.size();

	sort(theWords.begin(), theWords.end());

	for (int i = 0; i < count-1; ++i)
	{
		if (theWords[i] == theWords[i+1])
		{
			amount++;
		}
		else
		{
			cout << theWords[i] << " appears " << amount << " times." << endl;
			amount = 1;
		}
	}

	cout << theWords.back() << " appears " << amount << " times." << endl;

	cout << "Amount of words is " << count << endl;
			
	std::cout << "Press enter to exit.\n";
	std::cin.ignore(2, '\0');

	return 0;
}

I was just wondering what I could change this to make it better?

Meaning for instance, I have to use the count -1 in the loop, because, otherwise, I would go out of bound. Is there another way of solving that?

Thanks

It looks gud 2 me, the count -1 is a trivial point to pick out.

U could probably now use std::unique as well to eliminate duplicates, but other than that it looks complete.

Ok, thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.