1 Replies - 2634 Views - Last Post: 21 April 2016 - 03:08 AM Rate Topic: -----

#1 Pony1487  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-October 15

Binary Search on Struct Array in C

Posted 21 April 2016 - 01:56 AM

Hello there,

I have been trying to implement binary search on an array of structs in C for a college project. I am trying to search by second name, however it finds every name correctly except the middle one ( Smith in this case ). It returns the correct index for each name except "Smith" returns -1.
I think I missing something obvious but I cannot see where. Any help would be great!

Thanks
Ro

/*
    binary search test
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 6


//struct templates
struct student
{
    char first_name[11];
    char surname[21];
    char fund_type;
};

//function protoype
int binary_search(struct student fxn_stud[], char surname[]);

    
main()
{
    char surname[21];
    int found = 0;
    
    
    struct student students[SIZE];
        
    strcpy(students[0].first_name, "Rob");
    strcpy(students[0].surname, "Flynn");
    students[0].fund_type = 'i';
    
    strcpy(students[1].first_name, "Mick");
    strcpy(students[1].surname, "Lamb");
    students[1].fund_type = 'f';
    
    strcpy(students[2].first_name, "Alice");
    strcpy(students[2].surname, "Moore");
    students[2].fund_type = 'n';
    
    strcpy(students[3].first_name, "Gar");
    strcpy(students[3].surname, "Smith");
    students[3].fund_type = 'f';
    
    strcpy(students[4].first_name, "Shane");
    strcpy(students[4].surname, "Oswell");
    students[4].fund_type = 'f';
    
    strcpy(students[5].first_name, "Sharon");
    strcpy(students[5].surname, "Tobin");
    students[5].fund_type = 'f';
    
    printf("Enter a surname to search:\n");
    gets(surname);
    
    found = binary_search(students, surname);
    
    printf("found = %d\n", found);
    
    
    getchar();
    getchar();
}//end main


//not finding middle element!!! Why???
int binary_search(struct student fxn_stud[], char surname[])
{
    int low = 0;
    int high = SIZE - 1;
    int middle;
    
    while( low <= high )
    {
        middle = (low + high) / 2;
        
        if( strcmp(fxn_stud[middle].surname, surname) == 0)
        {
            return middle;
        }
        else if ( surname[0] < fxn_stud[middle].surname[0] )
        {
            high = middle - 1;
        }
        else if ( surname[0] > fxn_stud[middle].surname[0] )
        {
            low = middle + 1;
        }
    
    }//end while
    
    return -1;
}//end binary_search()




Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search on Struct Array in C

#2 Pony1487  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-October 15

Re: Binary Search on Struct Array in C

Posted 21 April 2016 - 03:08 AM

Ok, I made a dumb mistake and the struct array was not sorted. Once I sorted it, the binary search worked fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1