In my main file I have an array of character strings, char names[320][30], and after I sort that with a bubble sort. I want to be able to do a recursive binary search to determine if a word is present in the names array and what its index is. The index is represented by a set of unsigned ints, unsigned int Set[10]. If the word is not present, the function should return -1.
#include "set.h"
#include "sortAndSearch.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int binarySearch(Char A[][30], char *, int, int){
//this gets passed the global array names
//and the spot we're looking for is the char pointer
//other 2 ints are low = 0, and high = 319
//then it finds mid, a point between high and low
//and then does the same thing on whichever half it needs
//until it finds the index its looking for
//this is recursive because of the low and high values provided
int mid, low, high, result;
//calculate midpoint to cut set in half
mid = (high + low)/2;
//comparison
result = strcmp(A[mid], key);
//if result < 0, A[mid] < key
if(result < 0)
return binarySearch(A, key, mid+1, high);
//if result > 0, A[mid] > key
else if(result > 0)
return binarySearch(A, key, low, mid-1);
//if result == 0, A[mid] == key
else if(result == 0)
return mid;
//couldnt find it
else
return -1;
//this should return int, either
//the index where the string being searched for is stored in the array
//or -1 to indicate that the string beinng sought is not in the array
}
And in my main function, I call the function:
char *key;
binarySearch(names, key, 0, 319);
When I try to compile, I get the following errors:
- search.c:7: parse error before “A”
- search.c: In function `binarySearch’:
- search.c:7: number of arguments doesn’t match prototype
- sortAndSearch.h:3: prototype declaration
- search.c:22: `A’ undeclared (first use in this function)
- search.c:22: (Each undeclared identifier is reported only once
- search.c:22: for each function it appears in.)
- search.c:22: `key’ undeclared (first use in this function)
So my question is why am I getting these errors, as I do not see any typos, and what’s up with the number of arguments not matching prototype? I copied it right from the sortAndSearch.h file I was given.
That
Charshould probably bechar(C is a case-sensitive language). Because this was wrong, most of the other compilation errors are consequences of the compiler being confused.You also need to give names to the other arguments in a function definition:
You then don’t need
lowandhighas separately declared local variables (indeed, you aren’t allowed to declare them as local variables as well as parameters).There is also a bug in the logic in the function, I believe:
All values are either greater than, less than, or equal to zero; the
elseclause is redundant.However, you also have a problem with terminating your search. You need to check whether
lowis greater thanhighbefore computingmid, and you should then return the-1for not found if the range to be searched is empty.