Here is my homwork problem: Problem 3. In the following array of C-strings char* names[] = { “DEF”, “BBB” , “EE”, “AA”, “CAB”, “AAA”, “DDDD”, “BBB”, “FFFF”, “CCCC”}; char* favoriteName= “AA”; Use generic binary search both iterative and recursive to find the favoriteName in the names array. If char names[][5] = { “DEF”, “BBB” , “EE”, “AA”, “CAB”, “AAA”, “DDDD”, “BBB”, “FFFF”, “CCCC”}; char* favoriteName= “AA”; What will be your call for search and explain any difference? Now dynamically allocate char**names and copy with all the names above and dynamically allocate char*favoriteName and copy with “AA” Make a call for the search _______________ *I am using C programming language for this problem* I made some code with iterative and recursive binary search functions, but only the iterative one finds the favorite name. Can you let me know what is wrong with my recursive binary search function as well as how to get it to work with the double array names[][5]? Here is my code: #include #include #include #include void* iterativeBinarySearchG(void* key, void* arr, int size, int elemSize, int(*compare)(void* a, void* b)) { //key is value you are trying to find //binarySearch is for already sorted arrays int low = 0, high = size - 1, mid; while (low <= high) { mid = (low + high) / 2; void* elemAddress = (char*)arr + mid * elemSize; //this puts the address of the mid element of arr in elemAddress; mid is the number of the element and elemSize is size of each element in bits giving the bytes in which the value of the mid element is stored if (compare(key, elemAddress) == 0) { return elemAddress; } else if (compare(key, elemAddress) < 0) { high = mid - 1; } else low = mid + 1; } return NULL; } void* recursiveBinarySearchG(void* key, void* arr, int start, int size, int elemSize, int(*compare)(void* a, void* b)) { //key is value you are trying to find //binarySearch is for already sorted arrays int low = start, high = size - 1, mid; if (low <= high) { mid = (low + high) / 2; void* elemAddress = (char*)arr + mid * elemSize; //this puts the address of the mid element of arr in elemAddress; mid is the number of the element and elemSize is size of each element in bits giving the bytes in which the value of the mid element is stored if (compare(key, elemAddress) == 0) { return elemAddress; } else if (compare(key, elemAddress) < 0) { high = mid - 1; recursiveBinarySearchG(key, arr, start, high, elemSize, compare); } else { low = mid + 1; recursiveBinarySearchG(key, arr, low, size, elemSize, compare); } } return NULL; } int compareString(void* a, void* b) { char* c1 = a; char* c2 = b; return strcmp(c1, c2); } int main() { char* names[] = { "DEF", "BBB" , "EE", "AA", "CAB", "AAA", "DDDD", "BBB", "FFFF", "CCCC"}; char* favoriteName = "AA"; int* found = iterativeBinarySearchG(& favoriteName, &names, 10, sizeof(char*), compareString); printf("Iterative Binary Search:\n"); if (found != NULL) { printf("Found\n"); } else printf("Not Found\n"); int* found1 = recursiveBinarySearchG(&favoriteName, &names, 0, 10, sizeof(char*), compareString); printf("Recursive Binary Search:\n"); if (found1 != NULL) { printf("Found\n"); } else printf("Not Found\n"); printf("\n\n"); char names1[][5] = { "DEF", "BBB" , "EE", "AA", "CAB", "AAA", "DDDD", "BBB", "FFFF", "CCCC"}; char* favoriteName1 = "AA"; int* found2 = recursiveBinarySearchG(&favoriteName1, &names1, 0, 10, sizeof(char*), compareString); printf("Recursive Binary Search:\n"); if (found2 != NULL) { printf("Found\n"); } else printf("Not Found\n"); int* found3 = iterativeBinarySearchG(&favoriteName1, &names1, 10, sizeof(char*), compareString); printf("Iterative Binary Search:\n"); if (found3 != NULL) { printf("Found\n"); } else printf("Not Found\n"); return 0; }
Here is my homwork problem:
Problem 3. In the following array of C-strings
char* names[] = { “DEF”, “BBB” , “EE”, “AA”, “CAB”, “AAA”, “DDDD”, “BBB”, “FFFF”, “CCCC”};
char* favoriteName= “AA”;
- Use generic binary search both iterative and recursive to find the favoriteName in the names array.
If char names[][5] = { “DEF”, “BBB” , “EE”, “AA”, “CAB”, “AAA”, “DDDD”, “BBB”, “FFFF”, “CCCC”};
char* favoriteName= “AA”;
- What will be your call for search and explain any difference?
Now dynamically allocate char**names and copy with all the names above and dynamically allocate char*favoriteName and copy with “AA”
- Make a call for the search
_______________
*I am using C
I made some code with iterative and recursive binary search functions, but only the iterative one finds the favorite name. Can you let me know what is wrong with my recursive binary search function as well as how to get it to work with the double array names[][5]? Here is my code:
The purpose of this programming work is to create iterative and recursive binary search routines in C to search for a target string in an array of C-strings and a double array. A target string, favoriteName, is provided along with an array of C-strings, char* names[. In addition, a double array, char names[][5,] with its associated target string, favoriteName1, is provided. The goal is to provide generic binary search routines that can handle both instances and determine if the target strings are present or not.
Step by step
Solved in 4 steps