Hi,
I have problems with my binary search function;
I have a 2D char array- with multi-word strings in it.
I have 5 strings, each string is of length 30.
e.g.
arr[0][30]={String number one};
arr[1][30]={Number two};
:
:
---
char *s =is the string to search for
char a[][MAX] = is the 2d array
int size = length of the string (30 or MAX)
---
If I try to search for word [two] I get not found,
but if I search for Number two, I get found at index 1.
I need to find the word inside the array and report the location of the word.
Like found in index 1 at position 8.
I really need help.
thank you
int binery_search(char *s, char a[][MAX], int size)
{
int low = 0;
int high = size;
int mid, value;
while (low <= high)
{
mid = (high + low)/2;
value = strcmp(s, a[mid]); //*************
if (value == 0)
return mid; // found
else if (value < 0)
high = mid-1;
else
low = mid+1;
}
return -1; // not found
}
thank you again.
I have problems with my binary search function;
I have a 2D char array- with multi-word strings in it.
I have 5 strings, each string is of length 30.
e.g.
arr[0][30]={String number one};
arr[1][30]={Number two};
:
:
---
char *s =is the string to search for
char a[][MAX] = is the 2d array
int size = length of the string (30 or MAX)
---
If I try to search for word [two] I get not found,
but if I search for Number two, I get found at index 1.
I need to find the word inside the array and report the location of the word.
Like found in index 1 at position 8.
I really need help.
thank you
int binery_search(char *s, char a[][MAX], int size)
{
int low = 0;
int high = size;
int mid, value;
while (low <= high)
{
mid = (high + low)/2;
value = strcmp(s, a[mid]); //*************
if (value == 0)
return mid; // found
else if (value < 0)
high = mid-1;
else
low = mid+1;
}
return -1; // not found
}
thank you again.