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; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question
100%

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<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
 
 
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;
}
Expert Solution
Step 1: Determine an introduction for given query:

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.

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Depth First Search
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education