ishere now - 2 months ago 32

C# Question

Hi guys I have a binary search, there is a listbox of sorted numbers, and the user enters a value in a textbox to check its position within the listbox. However the number entered in the textbox shows as the positon within the listbox to e.g. 5 entered, "The number you are searcing is found in position 5".

`int Number = (Convert.ToInt16(textBox1.Text));`

int a = lstHoldValue.Items.Count;

int start = 0, end = a - 1, mid = (start + end) / 2;

while (start <= end)

{

mid = (start + end) / 2;

if (Number == Convert.ToInt16(lstHoldValue.Items[mid]))

{

MessageBox.Show("The number you are searching is found in position " + (Number));

return;

}

Answer

The binary search block is correct

The error is in the message you print. It should be:

```
MessageBox.Show("The number you are searching is found in position " + (mid));
```

**mid** instead of *Number*