0

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.

9
  • So you would like to know how you could translate this to VisualBasic? Commented Apr 3, 2014 at 0:24
  • Huh? No This is C++, I am confused about string searches in C++ Commented Apr 3, 2014 at 0:29
  • Remove the checking for position and output lines from your searchList function and just return the value. The searchList function shouldn't know or care what you want to do with the return value. All it needs to do is return the value. Then call this function from your code, test the return value, and then output the results. See my answer for the C++ solution using algorithms, if you really want to start learning that aspect of C++ (which is highly important anyway, so learn using a straightforward example). Commented Apr 3, 2014 at 0:41
  • Okay I understand that but still the question given to me gave me originally that and I still do not understand how this grabs the NAME and the MARK, two different variables. As far as I understand this array would contain only names because it isn't a 2D array so should I just turn it into a 2D array? =/ Commented Apr 3, 2014 at 0:50
  • I think you should write the program first that you say you don't understand. Believe me, you're reading too much into this. The position is an index into the name array. So how do you get the name given those two values? Do you know how arrays work? Look at my answer, and start with the second version. Now given that, you don't see how to output the name? int main() { int foundPos = searchList(yourArray, number_of_items, "Bob"); if ( foundPos != -1 ) { //output the name and position } In other words, and again, the function searchList doesn't do output -- all it does is return info. Commented Apr 3, 2014 at 0:56

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

I don't really understand how this solves my question, how is this outputting the name and mark. The whole point it has to be a linear search as welll, I don't understand your use of *'s or what pPos is. We haven't gone over pointers in class, in fact we skipped that chapter.
A good learner doesn't make a book or a skipped chapter stop them from learning. See my comment above. That function you wrote does too much, and I commented as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.