| New Reply |
[C/C++] need a bit of help making a simple search engine. |
Share Thread | Thread Tools |
| Nov14-12, 02:13 PM | #1 |
|
|
[C/C++] need a bit of help making a simple search engine.
Hi, new to the forum, it is a really spiffy place. I'm not really much of a programmer, but I have an interest in it and kind of like to piddle around with programming. I am learning C and C++ mostly by doing, browsing books, and asking dumb questions of people who know alot more about it than I do.
So here is a dumb question. I have been playing with arrays and I am trying to learn how to search through arrays. I managed to pull a piece of code from a book I've been reading ( I will show it below), and it works well. I want a search that will tell me if a number is in an array, and if so all of the locations that it can be found at. Here is the code I found, it is a function and it is in C. Code:
/* compare key to every element of array until the location is found
or until the end of array is reached; return subscript of element
if key or -1 if key is not found */
int linearSearch( const int A[], int key, int size )
{
int n; /* counter */
/* loop through array */
for ( n = 0; n < size; ++n ) {
if ( A[ n ] == key ) {
return n; /* return location of key */
} /* end if */
} /* end for */
return -1; /* key not found */
} /* end function linearSearch */
Again, I am kind of a novice that just tinkers for fun, so go easy on me. Thanks, I really like your forums! |
| Nov14-12, 04:39 PM | #2 |
|
Recognitions:
|
|
| Nov14-12, 05:17 PM | #3 |
|
Recognitions:
|
Hi Piddler! Welcome to PF.
![]() Here's and adaptation: Code:
/* compare key to every element of array; return the number of keys found.
The found locations are stored in the array foundLocations.
Note that foundLocations must be an array as big as A. */
int linearSearch( const int A[], int key, int size, int foundLocations[] )
{
int n; /* counter */
int nrFound = 0; /* number of keys found */
/* loop through array */
for ( n = 0; n < size; ++n ) {
if ( A[ n ] == key ) {
foundLocations[ nrFound ] = n; /* store location of key */
nrFound++;
} /* end if */
} /* end for */
return nrFound; /* number of keys found */
} /* end function linearSearch */
|
| Nov15-12, 08:46 AM | #4 |
|
Mentor
|
[C/C++] need a bit of help making a simple search engine.
Here's a version that uses C++ vectors instead of arrays.
Code:
/* compare key to every element of vector A; return the number of keys found.
The found locations are stored in the vector foundLocations. */
int linearSearch (const vector<int> &A, int key, vector<int> &foundLocations)
{
/* get rid of anything that's already in foundLocations and set its size to 0 */
foundLocations.clear();
/* loop through vector A */
for (int n = 0; n < A.size(); ++n)
{
if (A[n] == key)
{
foundLocations.push_back(n);
}
}
return foundLocations.size(); /* number of keys found */
}
Minor point: I put the declaration of n in the loop header instead of in a separate statement because n is used only inside the loop. I usually follow the principle that variables should be declared as "locally" as possible. |
| New Reply |
| Thread Tools | |
Similar Threads for: [C/C++] need a bit of help making a simple search engine.
|
||||
| Thread | Forum | Replies | ||
| New search engine | General Discussion | 23 | ||
| Search engine | Computing & Technology | 3 | ||