Question

You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided main function to test your search function. Be sure not to use a loop of any kind in any of the functions you write.

#include using namespace std; #include "BinarySearch.h" int main() { int choice = 1; cin >> choice; if (choice == 1) { int arr[] = {1, 5, 8, 10, 14, 15}; cout << bSearch(arr, 6, 1) << endl << bSearch(arr, 6, 0) << endl << bSearch(arr, 6, 15) << endl << bSearch(arr, 6, 14) << endl; } else if (choice == 2) { int arr2[] = {-178, -148, -99, -87, -75, -42, -23, -2}; cout << bSearch(arr2, 8, -2) << endl << bSearch(arr2, 8, -178) << endl << bSearch(arr2, 8, -148) << endl << bSearch(arr2, 8, 1) << endl << bSearch(arr2, 8, -179) << endl << bSearch(arr2, 8, -1) << endl; } else { int arr3[] = {100}; cout << bSearch(arr3, 1, 100) << endl << bSearch(arr3, 1, -15) << endl << bSearch(arr3, 1, 115) << endl; } return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// BinarySearch.h
int bSearchHelper(int array[], int left, int right, int target)
{
if (right >= left)
{
int mid = left + (right - left)/2;

if (array[mid] == target) return mid;

if (array[mid] > target) return bSearchHelper(array, left, mid-1, target);

return bSearchHelper(array, mid+1, right, target);
}

return -1;
}

int bSearch(int array[], int size, int target)
{
   return bSearchHelper(array,0, size-1, target);
}



// main.cpp

#include <iostream>
#include "BinarySearch.h"
using namespace std;

int main ()
{
   int choice = 1;
   cin >> choice;

   if(choice == 1)
   {
       int arr[] = {1,5,8,10,14,15};
       cout << bSearch(arr,6,0) << endl
           << bSearch(arr,6,0) << endl
           << bSearch(arr,6,15) << endl
           << bSearch(arr,6,14) << endl;
   }
   else if(choice == 2)
   {
       int arr2[] = {-178,-148,-99,-87,-75,-42,-23,-2};
       cout << bSearch(arr2,8,-2) << endl
           << bSearch(arr2,8,-178) << endl
           << bSearch(arr2,8,-148) << endl
           << bSearch(arr2,8,1) << endl
           << bSearch(arr2,8,-179) << endl
           << bSearch(arr2,8,-1) << endl;
   }
   else
   {
       int arr3[] = {100};
       cout << bSearch(arr3,1,100) << endl
           << bSearch(arr3,1,-15) << endl
           << bSearch(arr3,1,115) << endl;
   }
return 0;
}

Add a comment
Know the answer?
Add Answer to:
You are making a .h file. Implement a recursive binary search function. bSearch passes in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

    DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.    Task 1 – Reverse array (3 pts) Given an integer array like: const...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT