0

i see plenty of examples on how to search arrays to find a specific instance what i want to do is find all the instances and print them for example i have this struct

struct BookInfo
{
    char title[50];
    int numAuthors;
    char authors[50][50];
    int year;
    int checkedout;
};

struct BookInfo library[500];

and i have a function to search within the years but it only gives me the first instance it finds how do i get it to give me bot instances??? heres the function

int yearsearch()
{
int target, i, l, r, mid;
    l = 0;
    r = 14-1;
    printf("type a year to search for book");
    scanf("%d", &target);

    while(l <= r)
    {
        mid = (l+r)/2;
        if(library[mid].year == target)
        {
            printf("\n%s",library[mid].title);
            printf("  %d",library[mid].year);
            printf("These are all the books with that year");
            break;
        }
        else if (library[mid].year < target)
        {
            l = mid + 1;
        }
        else
        {
            r = mid - 1;

        }

        if(l > r)
            printf("The target is not in the array.\n");

    }
menu();

}
2
  • (in response to a deleted comment suggesting removing the 'break') Removing the break would cause this to loop forever without terminating in some instances. This is not the correct solution as it is as it may miss certain elements if 'mid' happens to land in the middle of the entries for a given year. Commented May 7, 2013 at 3:56
  • when i did that the programs never stops printing the first instance it finds lol Commented May 7, 2013 at 3:58

1 Answer 1

3

You're doing a kind of binary search over the array, which is not designed to find all instances without modification. Have you considered just doing a linear search (i.e., a for loop) over the length of the array and printing if the array element matches your search criteria? One might implement a naive linear search like this:

for (int i = 0; i<500; i++) {
   if (library[i].year == target) {
       // Do your printing here
   }

   // Otherwise do nothing!
}
Sign up to request clarification or add additional context in comments.

6 Comments

did not know that about the binary search how does one implement a linear search (looking it up myself)
A binary search is only useful for determining if a particular entry exists in a sorted list.
That's not strictly true. A binary search with added linear probing can be used to determine all occurrences in a sorted list, too. You binary search until you find a matching element, then you seek back to find the start of the matching range, and then seek forward to find the end of the matching range. I just suggested going for a linear search because of the small number of elements involved and because it's much simpler.
since i have your help at the moment what if i was searching one of the char arrays instead can i use the same code by just switching target to a char instead of an int
No, the index will remain an 'int', but the general technique will be the same. In C the indexes to an array are always of type 'int'. If you want to post some code you can post a new question and I will help you there.
|

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.