Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ My code doesn't output what is required because my binary search numbers are too small. We are suppose to make the code be searched
C++
My code doesn't output what is required because my binary search numbers are too small. We are suppose to make the code be searched binarily 1000 times. Can anyone help me fix my errors?(exclude linear search. Only need binary search)
Note: The code is for a random number to be binary searched through so the required code is an example as long as it outputs something similar.
My Code:
My output:
Required output:
#include #include #include #include using namespace std; int binarySearch (int [], const int,const int ); int getRandomNumber (const int); void printArray(int [], const int); int main() { srand(time(NULL)); const int numSearches = 1000; const int arraySize L 400; int values[arraySize]; for (int i = 0; i < arraySize; i++) { values [1] =getRandomNumber (arraySize * 2); } I std::sort(values, values + arraySize); for (int i=0; i < numSearches; i++) { int target getRandomNumber (arraySize 2); int result = binarySearch (values, arraySize, target); if (result == -1) { cout < < "Target: < < target < < "not found in array using BINARY SEARCH." < < endl; } else { cout < < "Target: " < < target < < "found at index: al" < < result < < "1" < < endl;
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