my program generates an array of random numbers.
I want to then search a specific number within the array.
If the number is in the array then a message apopears on the console saying that its there.
I'm using the binary search algorithm to do this.
However im having no luck, can you see whats wrong with my code??
cout << "Please search for a value within the array: ";
cin >> userValue;
int result;
binarySearch(numArray, size, userValue);
if(userValue == numArray[i])
{
cout << "The number " << userValue << " was found in the array" << endl;
}
else
{
cout << "The number " << userValue << " was not found. " << endl;
}
cin.ignore(2);
}
1) In C, indices for an array of size N go from 0 to N-1. All your loops looking like for( i=1; i <= N; i++ ) need to be changed to for( i=0; i < N; i++ ).
2) You declare "result" but forget to assign it with the return value of binarySearch().
3) You should compare "userValue" with numArray[result], not numArray[i].