[C/C++] need a bit of help making a simple search engine.

  • Context: C/C++ 
  • Thread starter Thread starter Piddler
  • Start date Start date
  • Tags Tags
    Bit Engine Search
Click For Summary

Discussion Overview

The discussion revolves around creating a simple search engine in C and C++. Participants are exploring how to modify a linear search function to return all instances of a specified key in an array or vector, rather than just the first occurrence. The conversation includes both C and C++ implementations, focusing on arrays and vectors.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant seeks help to modify a linear search function to return all indices of a matching key in an array.
  • Another participant suggests creating a second array to store the indices of matches and returning the count of matches.
  • A third participant provides an adapted C function that fills an array with the indices of found keys and returns the count of keys found.
  • A fourth participant presents a C++ version using vectors, highlighting the advantages of vectors over arrays, such as automatic size management and simpler syntax for adding elements.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the search function to return all instances of the key. However, there are multiple approaches discussed, including using arrays versus vectors, and no consensus is reached on which method is superior.

Contextual Notes

Some limitations include the need for additional storage for indices and the differences in handling data structures between C and C++. The discussion does not resolve which implementation is preferable in all situations.

Who May Find This Useful

New programmers learning C or C++, particularly those interested in data structures and searching algorithms.

Piddler
Messages
1
Reaction score
0
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 a lot 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 */

the problem is that only returns the first one that it finds. how do I make it so that it returns all instances of the number matching the key?

Again, I am kind of a novice that just tinkers for fun, so go easy on me. Thanks, I really like your forums!
 
Technology news on Phys.org


Piddler said:
how do I make it so that it returns all instances of the number matching the key?
You need another array the same size as A[], perhaps name that second array B[]. Then you would fill up B[] with the indexes of A[] that matched the search value. You'd also need to return the actual number of matches, which would be the effective size of B[].
 


Hi Piddler! Welcome to PF. :smile:

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 */
 


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 */
}

The size of A doesn't have to be passed as a separate parameter, because vectors "know" how big they are. Also, push_back() causes foundLocations to "grow" to exactly the size needed to accommodate the number of matches. In general, with vectors you don't have to do as much fussy "bookkeeping" with index variables, as you do with arrays.

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.
 
Last edited:

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
20
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
47
Views
5K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
12
Views
3K