Question
I am having trouble with allowing my binarySearch function use recursion. I have looked at examples online to get a general basis of what I
I am having trouble with allowing my binarySearch function use recursion. I have looked at examples online to get a general basis of what I need to do, but everytime I try to make changes, everything doesn't seem to coroporate with one another.
Here is my code with the changes I have made, I am getting multiple errors and have no idea where to start or what i should do!
// C program header files needed to run the program // Also, defining the size of our array to only 20 values #include
// Declaring function prototypes and their returning values size_t binarySearch(const int b[], int searchKey, size_t low, size_t high); void printHeader(void); void printRow(const int b[], size_t low, size_t mid, size_t high);
// Fuction main is required for all C programs int main(void) { int a[SIZE]; // create the array for our program // For loop to create the data being used in our array for (size_t i = 0; i < SIZE; ++i) { a[i] = 2 * i; } // end of for loop
printf("Enter a number between 0 and 28: "); // promt the user to enter a value to be binary searched in our array int key; // value to locate in array scanf_s("%d", &key); // scan their input and place into the "key"
printHeader();
// Display the result if (binarySearch(key, 0, SIZE - 1)) { printf(" %d was found at index %d ", key, middle); } // end of if statement else { printf(" %d was not found! ", key); } // end of else statement
getchar(); getchar(); } // end of int main
// Declaring function binarySearch to perform the binary search of the array size_t binarySearch(const int b[], int searchKey, size_t low, size_t high) { // While loop to go through the array until the low index is greater than high index while (low <= high) { // Determine the middle element of subarray size_t middle = (low + high) / 2;
// Display subarray used printRow(b, low, middle, high);
// If searchKey matches the middle element of array, return the middle if (searchKey == b[middle]) { return 1; } // end of if statement // if searchKey is less than middle element, set new high else if (searchKey < b[middle] && low < high) { return binarySearch(searchKey, low, middle - 1); } // end of else if statement // if searchKey is greater than the middle element, set new low else if (searchKey > b[middle] && low < high) { return binarySearch(searchKey, middle + 1, high); // search the high end of array } // end of else if statement else { return 0; } // end of else statement } // end of while loop } // end of function binarySearch // Declaring function printHeader to print headers for the outputs void printHeader(void) { puts(" Indices:");
// Output column headers for (unsigned int i = 0; i < SIZE; ++i) { printf("%3u ", i); } // end of for loop
puts(""); // start new line
// Output line of - characters for (unsigned int i = 1; i <= 4 * SIZE; ++i) { printf("%s", "-"); } // end of for loop
puts(""); // start new line } // end of function printHeader
// Declaring function printRow to print one row of output showing current part of array being processed void printRow(const int b[], size_t low, size_t mid, size_t high) { // For loop to go through entire array for (size_t i = 0; i < SIZE; ++i) { // If statement to display spaces if outside current subarray if (i < low || i > high) { printf("%s", " "); } // end of if statement // Display the middle element else if (i == mid) { printf("%3d*", b[i]); // mark the middle value } // end of else if statement else { printf("%3d ", b[i]); // display other elements in subarray } // end of else statement } // end of for loop
puts(""); // start new line } // end of function printRow
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started