I want to write a program to conduct a binary search and return all the occurrences of a number to the user. I succeeded in coming up with a binary search method but I don't know how to make return all the occurrences of the number. Here is my code
/*
*A method which searches for the position of a number
*specified by the user
*@param target, the number the user is looking for
*@return the position(s) in which the number is found
*/
int binarySearch(int target)
{
int left = 0;
int right = userArray.length - 1;
while (left <= right)
{
int midPoint = (left + right)/2;
if (userArray[midPoint] == target)
{
for (int i = 0; i <= userArray.length; i++)
{
}
return (midPoint + 1);
}
else if (userArray[midPoint] < target)
left += 1;
else
right -= 1;
}
return -1;
}