1

so I have structure for competitors. I use binary search to search competitors by their surnames. This is the code:

int binary(tekmovalec abc[], int n, char x[20])
{
 int start = 0;
 int end = n-1;

 int a= strlen(x);

 while(start <=end)
 {

     int mid = (start+end)/2;

     if(abc[mid].surName[0]== x[0])
     { 

     print(tekmovalci[mid]);
     return mid;

     }


     else if(x[0]<abc[mid].surName[0])
     {
         end = mid -1;


     }
     else
     {start = mid +1;}

 }

return -1;

 }

I have a problem, that function checks only first letter of surname and input array, so if surname is Obrien, and user input is "Obrb" it prints Obrien. I dont know how to expand function to check for all letters of user input. Thank you.

2
  • 1
    std::strcmp? Commented Dec 8, 2013 at 11:51
  • 1
    std::string, to complete the set. Commented Dec 8, 2013 at 12:24

1 Answer 1

2

Use strcmp to compare the strings in the loop.

int res = strcmp(x, abc[mid].surName);
if(!res)
 { 

 print(tekmovalci[mid]);
 return mid;

 }


 else if(res < 0)
 {
     end = mid -1;


 }
 else
 {start = mid +1;}
Sign up to request clarification or add additional context in comments.

Comments

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.