Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,

the code below belongs to a binary search algorithm. The user enters numbers in textbox1 and enters the number that he want to find with binarysearch in textbox2. I have a problem with it, that is when I enter for example 15,21 in textbox1 and enter 15 in textbox2 and put breakpoint on the line I commented below and I understood that it doesn't put the number in textbox2 in searchnums(commented),for more explanation I comment in code.

Thanks in advance.

C#
public void button1_Click(object sender, EventArgs e)
   {

       int searchnums = Convert.ToInt32(textBox2.Text);//the problem is here,the value in textbox2 doesnt exist in searchnums and it has value 0.
       int result = binarysearch(searchnums);
       MessageBox.Show(result.ToString());
   }
   public int binarysearch(int searchnum)
   {
       string[] source = textBox1.Text.Split(',');
       int[] nums = new int[source.Length];
       for (int i = 0; i < source.Length; i++)
       {
           nums[i] = Convert.ToInt32(source[i]);
       }
       int first =0;
       int last = nums.Length-1;

       while (1<= nums.Length)
       {
       int mid = (int)Math.Floor(first+last / 2.0);
            if (first > last)
           {
               break;
           }
           if (searchnum < nums[mid])
           {
               last = mid - 1;
           }
           if (searchnum > nums[mid])
           {
               first = mid + 1;
           }
           else
           {
               return nums[mid];
           }
       }
       return -1;

   }
Posted
Updated 25-Dec-10 1:09am
v2

Try this:

C#
public void button1_Click(object sender, EventArgs e)
{
    try
    {
        int searchnums = Convert.ToInt32(textBox2.Text);
        int result = binarysearch(searchnums);
        MessageBox.Show(result.ToString());
    }
    catch (Exception ex)
    {
    }
}


or take steps to handle potential problems:

C#
public void button1_Click(object sender, EventArgs e)
{
    int searchnums;
    int result = -1;
    if (Int32.TryParse(textBox2.Text, out searchnums))
    {
        result = binarysearch(searchnums);
    }
    MessageBox.Show(result.ToString());
}


 
Share this answer
 
Comments
arashmobileboy 25-Dec-10 7:13am    
thanks that helping me,your second code gives output linke this:when for example i enter:15,21 in textBox1 and i enter each number between 1 to 15 it shows the output 15 when i enter each number between 16 to 21 it shows 21 and for each number greater than 21 it shows -1.and when i put breakpoint in if (Int32.TryParse(textBox2.Text, out searchnums)) line and i enter a for example 15 in textBox2 it shows that the searchnums has the value 0 yet
Manfred Rudolf Bihy 25-Dec-10 8:56am    
@arashmobileboy: If your break point is on the line with the if statement searchnums will be zero indeed. You have to press F10 to make that line execute, only then can you tell what Int32.TryParse made of your input.
arashmobileboy 25-Dec-10 10:11am    
oh yes,you said correctly.the problem was this.now for example when i enter 16 in textBox2 it shows 10(i think it converts to hexadecimal),can you help me more?
Try replacing your
int searchnums = Convert.ToInt32(textBox2.Text);
with
int searchnums =0;
if (!Int32.TryParse(textbox2.Text, out searchnums))
   {
   throw new ApplicationException("Could not convert \"" + textbox2.Text + "\" to an integer");
   {
It may be that you are not typing what you think...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900