Search code examples
c++functionbinary-searchfreeze

Binary Search function that displays all matching values?


I have an assignment that requires me to create a binary search function that will search an array of structs that contain dates for a specified month and then print all of those entries with matching months.

I am having a very difficult time getting the binary search to work properly when I am searching for multiple values, and can't seem to figure out where I'm going wrong.

Here is my binary search function:

void binsearch(Event* ev_ptr[], int size, int month)
{
    int low = 0, high = size - 1, first_index = -1, last_index = -1;

    while (low <= high) //loop to find first occurence
    {
        int mid = (low + high) / 2;

        if (ev_ptr[mid]->date.month < month)
        {
            low = mid + 1;
        }
        else if (ev_ptr[mid]->date.month > month)
        {
            first_index = mid;
            high = mid - 1;
        }
        else if (ev_ptr[mid]->date.month == month)
        {
            low = mid + 1;
        }
    }

    low = 0; high = size - 1; //Reset so we can find the last occurence

    while (low <= high) //loop to find last occurence
    {
        int mid = (low + high) / 2;

        if (ev_ptr[mid]->date.month < month)
        {
            last_index = mid;
            low = mid + 1;
        }
        else if (ev_ptr[mid]->date.month > month)
        {
            high = mid - 1;
        }
        else if (ev_ptr[mid]->date.month == month)
        {
            high = mid + 1;
        }
    }

    for (int i = first_index; i <= last_index; i++)
    {
        cout << "\nEntry found: "
            << endl << ev_ptr[i]->desc
            << endl << "Date: " << ev_ptr[i]->date.month << '/' << ev_ptr[i]->date.day << '/' << ev_ptr[i]->date.year
            << endl << "Time: " << setw(2) << setfill('0') << ev_ptr[i]->time.hour << ':' << setw(2) << setfill('0') << ev_ptr[i]->time.minute << endl;
    }
}

and here is my main function:

const int MAX = 50;

int main()
{
    Event* event_pointers[MAX];
    int count, userMonth;
    char userString[80];

    count = readEvents(event_pointers, MAX);

    sort_desc(event_pointers, count);
    display(event_pointers, count);

    cout << "\n\nEnter a search string: ";
    cin.getline(userString, 80, '\n');
    cin.ignore();

    linsearch(event_pointers, count, userString);

    sort_date(event_pointers, count);
    display(event_pointers, count);

    cout << "\n\nEnter a month to list Events for: ";
    cin >> userMonth;
    cin.ignore();

    binsearch(event_pointers, count, userMonth);

    for (int j = 0; j < count; j++) //Cleanup loop
        delete event_pointers[j];

    cout << "\nPress any key to continue...";
    (void)_getch();
    return 0;
}

I've gotten everything else to work as I need to for this assignment, but it's just this binary search that seems to be causing problems. I have tried using some things I found online in the most recent iteration (What I posted above), but to no avail. Any help would be greatly appreciated!


Solution

  • Don't set theses indices with binsearch. Search for an occurence than loop downwards and upwards until the conditions fails. Something like

    else if (ev_ptr[mid]->date.month == month)
    {
                // mid = some occurence found 
                // increment and decrement mid until condition fails
    }```