473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

argg, noob array question

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
4 1879
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1986
by: NewsHound | last post by:
I've been playing around with Perl for a while, but I'm still something of a beginner. I was wondering what happens in the following case. Take a "foreach" loop iterating through the sorted keys...
2
1702
by: anti-guru | last post by:
This is killing me. How can I make a static array of two strings in my class? Nothing I've tried works. Here is what I currently have, which also doesn't work, but gives the least amount of...
8
2131
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
3
1599
by: newatthis | last post by:
hi am new at c++ and am trying my best to learn but am having a little trouble understand one aspect that has been repeated in many text books all with very vague explanations. i am hoping someone...
7
1911
by: DJP | last post by:
Hi, I had sort of a noob question on memory allocation for strings. char *str1 = "Hello World!"; char str2 = "Hello World!"; In the above bit are both str1 & str2 stack allocated or heap...
3
1224
by: ernie.bornheimer | last post by:
Okay, I know how to: - construct a SQL query and get a result set - loop through the result set and echo the fields I need But what I really need to do is get a value from a field and put it in...
5
1604
by: Milan Krejci | last post by:
the thing is that descentant branches i dont want to expand do expand. $id variable contains an array of branches i want the program to go through (alcohol's id -beer id etc) function...
8
1248
by: azz131 | last post by:
Hi, i want to access an array of objects inside a method like this using System; using System.Collections.Generic; namespace ObjectArray { class MainClass{ class MyClass
2
1475
by: tavspamnofwd | last post by:
I'm a total noob, and I'm trying to understand this code: var newsi = { name:"newsi", dom:false }; newsi.Client=function(){ //stuff }
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.