By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

argg, noob array question

P: n/a
I have an array with 50 elements in it, and a huge document with like
35,000 words on it. What I want to do is count the number of times each
element has appeared in the document. This is what I have, what am I
doing wrong?

int search(ifstream& inFile, string keywords[], int SIZE)
{
int counter = 0, k;
string target;
inFile >> target;
for(k = 0; k < SIZE; k++)
{
if(target == keywords[k])
{
counter++;
}
inFile >> target;
}

return counter;
}

Apr 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 12 Apr 2006 21:56:16 -0700, foker wrote:
I have an array with 50 elements in it, and a huge document with like
35,000 words on it. What I want to do is count the number of times each
element has appeared in the document. This is what I have, what am I doing
wrong?

int search(ifstream& inFile, string keywords[], int SIZE) {
int counter = 0, k;
string target;
inFile >> target;
for(k = 0; k < SIZE; k++)
{
if(target == keywords[k])
{
counter++;
}
inFile >> target;
}
}
return counter;
}
}


1) use an stl container like a map<string,int> to hold your list of words
being searched for and number of times each word in found, and
2) dont pass an array of strings by value; instead pass map<string,int>&
keywords to your search() function

learn to use stl containers and iterators and your life will be much
easier
Apr 13 '06 #2

P: n/a
"foker" <br************@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
:I have an array with 50 elements in it, and a huge document with like
: 35,000 words on it. What I want to do is count the number of times each
: element has appeared in the document. This is what I have, what am I
: doing wrong?
:
: int search(ifstream& inFile, string keywords[], int SIZE)
: {
: int counter = 0, k;
NB: it is best to only declare your variables at first
use ... k has nothing to do up here.

: string target;
: inFile >> target;
: for(k = 0; k < SIZE; k++)
: {
: if(target == keywords[k])
: {
: counter++;
: }
: inFile >> target;
: }
:
: return counter;
: }

You seem to have incorrectly merged two loops into one:
you want to read each word in the file, and compare it
to each word in the array. 2xeach => 2 loops:
while( inFile >> target )
for( int k = 0 ; k<Size ; ++k )
You should be able to easily fill up the rest.
Note also that the inner loop could be replaced by a call
to a standard library function (#include <algorithm>):
counter += std::count( keywords, keywords+SIZE, target );
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Apr 13 '06 #3

P: n/a
I havent learned STL containers or maps yet. Still having a few
problems.

while(inFile >> target)
for(int k = 0; k < SIZE; k++)
{
if(keywords[k] == target)
{
temp = k;
num_of_times_read[temp] += counter++;
}
}
for(int index = 0; index < SIZE; index++)
cout << num_of_times_read[temp];
return 0;
}

What I want it to do is read in every word from the document and test
it against every element in keywords array, if it matches i want the
index # from keywords assigned to a temp variable, then i want
num_of_times_counted[temp] to hold the number of times it was counted
each time it hits that keyword in the document. To me it makes complete
sense but it doesn't work haha, what am I doing wrong here?

say, target matches index #6 in keywords array. I want temp = 6, so
num_of_times_read[6] = count++. can I do this?

btw, counter += std::count( keywords, keywords+SIZE, target ); gave a
ton of errors.

Apr 13 '06 #4

P: n/a
"foker" <br************@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
:I havent learned STL containers or maps yet. Still having a few
: problems.
:
: while(inFile >> target)
: for(int k = 0; k < SIZE; k++)
: {
: if(keywords[k] == target)
: {
: temp = k;
: num_of_times_read[temp] += counter++;
You probably want to increment this only once,
not by the total previous count of all hits.
Therefore:
num_of_times_read[temp] += 1; // or just ++notr[k];
: }
: }
: for(int index = 0; index < SIZE; index++)
: cout << num_of_times_read[temp];

Did I not warn you, in my previous post, that you should
not declare variables at the beginning of your function,
but as late as possible (and in the innermost scope) ?

Here you are misusing the variable 'temp' within the second
loop, instead of index.

: return 0;
: }
:
: What I want it to do is read in every word from the document and test
: it against every element in keywords array, if it matches i want the
: index # from keywords assigned to a temp variable, then i want
: num_of_times_counted[temp] to hold the number of times it was counted
: each time it hits that keyword in the document. To me it makes complete
: sense but it doesn't work haha, what am I doing wrong here?
:
: say, target matches index #6 in keywords array. I want temp = 6, so
: num_of_times_read[6] = count++. can I do this?

Yes. Actually "noone"'s reply hinted towards something closer to what
you are looking for.

Here's a quick example thrown together:

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

void printCounts(ifstream& inFile, string keywords[], int size)
{
typedef map<string,int> Cnts; // sorted keyword -> its count
Cnts cnts;
// fill up a map for faster search than with array
for( int i = 0 ; i != size ; ++i )
cnts[keywords[i]] = 0;

// read file, for each word check if it is within our map
string target;
while( inFile>>target ) {
Cnts::iterator const pos = cnts.find( target );
if( pos != cnts.end() ) { // actually found
++pos->second; // increment the count
}
}

// now print keyword counts in original order
for( int i = 0 ; i != size ; ++i )
cout<< keywords[i] << " -> " << cnts[keywords[i]] <<endl;
}

Not tested, but will hopefully at least compile.
The best way to write such a function depends on what exactly
you want to do...

: btw, counter += std::count( keywords, keywords+SIZE, target );
: gave a ton of errors.
Well, there are many reasons for which this might have happened.
Good luck,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Apr 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.