How to Handle Multiple search results

I know how a basic linear search works. What I need to know is how to adapt a linear search function to collect and display results of a search that has more than one result. Lets say I store tasks, dates and classes in an array of structs. I need to search for tasks by class and list all the tasks found for a particular class. I need to store these in an array rather than just output them in the fuction call.
I'd prefer to understand this in terms of arrays/structs first. I know I can use vectors, but I'm trying to learn arrays/structs. This was an assignment last semester that I didn't clearly understand.
Thanks for any help.
In a loop, just store your answer in an array and keep track of the index. that index will tell you how many results you have:

Example, search a string for the letter F:
1
2
3
4
5
6
7
char SearchMe[] = "AEHFNGMEPFGUDLESF"; // This is the string we will search
int ResultLocations[100] = {999}; // Fill the array with a number that you can recognize as invalid.
int nb_results = 0;

for (int i = 0; SearchMe[i] != '\0' ; i++)
  if (SearchMe[i] == 'F')
    ResultLocations[nb_results++] = i;


Now ResultLocations will contain all of the array indices for the character 'F'. nb_results will tell you how many indices you found. This is useful for looping through ResultLocations to process the results.

In your case you are searching a structure array. This uses the same principle. Another ideas is that instead of storing the array index for each occurance of your search criteria, you can store the structure itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct MyStruct
{
  char myClass;
  char task;
};

MyStruct SearchMe[100];
MyStruct FilteredResults[100];
int nb_results = 0;

...  // Your code here.

for (int i = 0; i < 100 ; i==)
  if (SearchMe[i].myClass == 'A')
    FilteredResults[nb_results++] = SearchMe[i];
Last edited on
I tried this code, and I might be misunderstanding something about indices and the actual content in the array. I used your code to search (the first example) and added this code to output the contenst of ResultLocations[].
1
2
3
cout << " The search results are: " << endl;
for( int count = 0; count < nb_results; count++)
	 cout << ResultLocations[nb_results];

I got a set of zeros, one for each occurence of the searched letter. So, if the string in your example was searched for 'F', I got "The search results are: " 000 Press any key.." I was expecting to see the contents of array elements that == 'F', which would print out as "FFF Press any key.."

I appreciate your patience. I gotta get this understood, and then use it when I output results from a struct.
When I change my code to
1
2
3
cout << " The search results are: " << endl;
for( int count = 0; count < nb_results; count++)
	 cout << ResultLocations[count];

I get the array locations (indices) of the 'F's, but not the 'F' output. How would I output the contents of these locations instead of the indices?

My goal is to be able to search for occurances of a course, and then output all the tasks related to that course. So I want to output the content of the found items, not the indicies, right?

I'm missing something.
Yep, your second method would be the correct way of processing the results. In my example I only stored the applicable indices instead of the applicable characters because a bunch of F's isn't really interesting. If you wanted the data instead of it's location you could modify the code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
char SearchMe[] = "AEHFNGMEPFGUDLESF"; // This is the string we will search
int ResultLocations[100] = {999}; // Fill the array with a number that you can recognize as invalid.
int nb_results = 0;

for (int i = 0; SearchMe[i] != '\0' ; i++)
  if (SearchMe[i] == 'F')
    ResultLocations[nb_results++] = SearchMe[i]; // Set to the data instead of the index here

// To read the results:
cout << " The search results are: " << endl;
for( int count = 0; count < nb_results; count++)
  cout << ResultLocations[count];


Now, I kept the array name "ResultLocations" for sake of comonality even though that is not what the array represents anymore. It would be better to rename it "FilteredResults" for sake of clarity.
Last edited on
Your question is incomprehensible.
HAH! Thank you Stewbond. I'll try it tomorrow morning to make sure I get it and can write code to do it myself. But I see the difference, and I think that was what I was getting confused about.
THanks for your patience!
Topic archived. No new replies allowed.