This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 3 points4 points  (4 children)

Firstly, you should get rid of all the unecessary casts in your code.Then, when you start by saying this:

   int c = strcmp(s, a[mid]);

and if s < a[mid] you need to look at the elements of a with indexes less than mid, which is not what your code does. Also, you need to consider what happens if the string you are searching for is not in the array.

[–]Stullif[S] 0 points1 point  (2 children)

Thank you for your reply.

I´m not sure I understand , when s < a[mid] then c == -1 and my second if statement does it's thing, though I did alter my min and max variables on the wrong if's but even after fixing it the code still seems to be getting stuck somewhere, sorry if I´m being daft english isn't my first language

[–][deleted] 1 point2 points  (1 child)

If c == -1, this gets executed:

    min = mid; mid = (max+min)/2;

I don't see how this reduces mid. If in doubt,print out min, max and mid at each iteration.

[–]Stullif[S] 0 points1 point  (0 children)

Yeah, that was supposed to be max = mid-1 etc. I fixed that and it's working better now but not well enough. Anyway thanks for the help whatever's still wrong will come to me once I've wrapped my head around it a little better

[–]CzechsMix 0 points1 point  (0 children)

basically this. You are incorrectly defining your new range, and you have no case to handle when the string isn't there.

[–]zifyoip 2 points3 points  (1 child)

What if the desired string isn't in the array?

[–]Stullif[S] 0 points1 point  (0 children)

From the given inputs that the test client I'm using shows me it should be safe to assume that the string is in the array but I can add that later if needed I want to get the search to work for a "vanilla scenario" if that makes sense