I'm making a loop for searching a value from the array. If I find it, the position will be printed, if it's not "no such number" will be printed. But, if I have several same numbers, I will receive only one and then I am over the loop. How can I get the control, if the same number in the array? My best tries end with endless loop or completed loop from the start.
static public void Searching(int[] arr)
{
string x = "yes";
Console.WriteLine("Enter please searching number");
while (x == "yes")
{
bool found = false;
int target = Convert.ToInt32(Console.ReadLine());
int searchkey = target;
int mid = 0, first = 0, last = arr.Length - 1;
while (!found && first <= last)
{
mid = (first + last) / 2;
if (target == arr[mid])
found = true;
else
{
if (target > arr[mid])
{
first = mid + 1;
}
if (target < arr[mid])
{
last = mid - 1;
}
}
}
String foundmsg = found
? "Item " + searchkey + " was found at position " + mid
: "Item " + searchkey + " was not found";
Console.WriteLine(foundmsg);
Console.WriteLine("would you like to find another number?");
x = Console.ReadLine();
}
}
.IndexOf(inputNumber), you'll have what you want. Did I miss something on the language barrier?