I have tried to search a data in a structure like 1D array, but it always tell me that the target data has not been found, although I have make the algorithm exactly as it is. now I am totally confused
the below is how I define function
int RecBinarySearch (int a[], int low, int high, int key) {
int mid{ };
if (high >= low ) {
mid = low + (high-low)/2;
if (key == a[mid]) return mid;
if (key > a[mid]) RecBinarySearch(a, mid + 1, high, key);
if (key < a[mid]) RecBinarySearch(a, low, mid - 1, key);
}
return -1;
}
and here you see how I define my array
int n{};
int* a = new int [n] {};
ctag, your code is C++, not C. Different languages, different solutions.int a[n];in C :).RecBinarySearchis C compatible (except thismid{}),ctag should stayint* a = new int [n] {};is not C.int mid{ };is not C. But it's written like you would write C code.