tinywolves
tinywolves

Reputation: 55

C++ Searches with strings in array

Hi so I am having trouble figuring out how to search though arrays, in particular with string. I have more than one question but here goes for now.

My first question is linear search. Modify the searchList function given below so that it searches for a given name (not an int) the functions returns an int which is the index of the name found. If -1 is returned then say name is not found otherwise write out the name and the mark for that name. So this is what I did and I have no clue whether it is correct. I am also confused because the wording of this sounds like a 2D array, how does it store the name AND the mark?

int searchList (const string list[], int numElems, string value)
{
int index = 0;      // Used as a subscript to search array
int position = -1;  // To record position of search value
bool found = false; // Flag to indicate if value was found

while (index < numElems && !found)
{
    if (list[index] == value) // If the value is found 
    { 
    found = true; // Set the flag 
    position = index; // Record the value's subscript
    }
    index++; // Go to the next element
}
if (position ==-1)
{
cout << “ Name not found.” << endl; << endl;
}
else
{
cout <<  list[position]; // Confused here, how do I output the name and mark?
}  
return position; // Return the position, or -1

I cannot really build it in VS because I don't know how I would at this point. My book doesn't touch on string searches so I am confused.

Upvotes: 0

Views: 3569

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

A C++ approach using algorithms:

1) Searches for values in arrays can be accomplished by using std::find() and std::find_if()

2) I suggest to not name your variables "list", since there already is a std::list class in C++, and your code will just confuse someone taking a quick glance at it.

#include <algorithm>
//...
int searchList (const string thelist[], int numElems, string value)
{
   string* pPos = std::find(theList, theList + numElems, value);
   if ( pPos != theList + numElems )
      return std::distance(theList, pPos);
   return -1;
}

The above searches the array for a value, and if found, returns a pointer to the value. If not found, then the pointer will point to one item after the last item. Note the usage of distance() to return "how far" the found position is from the beginning position.

You then just call this function and test the return value. If it's -1, then the name is not found, otherwise the return value is the index of the found name.

I believe your original attempt was doing too much in terms of input/output. All you needed to do was write a function that either returned -1 or an index value, nothing more, nothing less. Then you were supposed to call that function and whatever it returned, you output the results.

Using your code:

int searchList (const string list[], int numElems, string value)
{
    int index = 0;      // Used as a subscript to search array
    while (index < numElems)
    {
       if (list[index] == value) // If the value is found 
          return index;
       ++index;
    }
    return -1;
}

You see how simple that is? You return as soon as you find a match. Now given that, how do you call it and process the return value?

The algorithm approach is more verbose, but less chance of a careless error from happening (such as not looping enough or looping too much, forgetting to increment the index, or some other stupid error that compiles fine but then running the program, it fails).

Upvotes: 2

Related Questions