Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
If I run my program like the way it is written below it won't give me the correct output,it won't search the number I want to search. But if I :
C#
list[i] = Convert.ToInt32(Console.ReadLine());
instead of
C#
Console.WriteLine("{0}", i);;

and input my own numbers it will find the numbers I want to find. I couldn't figure out why. Can anybody help me out?

C#
{
    class program
    {
        public class BinarySearch
        {
            public static int Search(int[] list, int x, int lower, int upper)
            {
                if (lower <= upper)
                {
                    int middle = (lower + upper) / 2;
                    if (x == list[middle])
                        return middle;
                    else if (x < list[middle])
                        return Search(list, x, lower, middle - 1);
                    else
                        return Search(list, x, middle + 1, upper);
                }
                return 0;
            }
                  public static void Main(String[] args)
                   {
                       int key;       // the search key
                       int index;     // the index returned
                       int[] list = new int[100];
                       Console.WriteLine("\nEnter arrays of 10 set numbers (strike enter after each set of numbers\n");
                       for (int i = 1; i <= 5; i++)
                           Console.WriteLine("{0}", i);
                    //   {
                      //     list[i] = Convert.ToInt32(Console.ReadLine()); 
                       //}
                       Console.WriteLine("...................................................\n");
                       Console.WriteLine("\nEnter the number to be searched in the list.");

                       key = Convert.ToInt32(Console.ReadLine());
                       index = Search(list, key, 0, 5);
                       Console.WriteLine("...................................................\n");
                       if (index == 0)
                           Console.WriteLine("Key {0} not found", key);
                       else
                           Console.WriteLine("Key {0} found at index {1}", key, index);
                   }
               }
           }
       }
Posted
Updated 2-Jul-14 23:34pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Jul-14 22:52pm    
Not clear. How about using the debugger and finding out?
—SA
StM0n 3-Jul-14 0:34am    
Is the binary search a homework assignment? And are you aware of the fact, that you need a sorted list?
Rob Philpott 3-Jul-14 3:16am    
Good point!

1 solution

Stop and think about what you are doing.
The code you show has the set load commented out - so the array is never set to a value, so you can never find any value other than zero (the default for an int)

And when you do have that code in, it doesn't fill the array - it reads five numbers from the user and uses them to fill the first five locations in the 100 you have allocated. And you prompt to the user is bad as well:
C#
Console.WriteLine("\nEnter arrays of 10 set numbers (strike enter after each set of numbers\n");
Because the way I would read that message is that I should enter:
1,2,3,4,5,6,7,8,9,10[ENTER]
And that will make your program crash.

And worse - your method would only work if I enter the numbers as a sorted list - if I enter then unsorted or reversed:
10,9,8,7,6,5,4,3,2,1
Then your code only find values exactly in the middle.

Seriously: think about the task you have been assigned, and work out on paper how you would do it manually. Because at the moment, you just seem to be trying to guess what works, and that is a recipe for a lot of trouble and wasted work!
 
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