binary search

This is a discussion on binary search within the C++ Programming forums, part of the General Programming Boards category; once again probably trivial, and i can't seem to find this stuff anywhere. i've created a binary search Code: int ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    binary search

    once again probably trivial, and i can't seem to find this stuff anywhere.
    i've created a binary search
    Code:
    int Narray::find(int low, int high, int num)
    {
          while (low == high)
          {
          int mid = (low + high)  2;
          if (num >= marray.a[mid])
         {
          low = mid + 1;
         }
        else if (num <= marray.a[mid])
        {
           high = mid - 1;
          }
           else
        {
                cout << The number was found in slot <<  marray.a[mid];
    	}
        }
    	return -1;
    }
    it is part of a class function.my question is how do i get the values low and high, to pas to the function from main(). i know that the high and low should be the contents of the min slot number and the max slot number but i just can't seem to pass them right...

    thanks for any help
    Last edited by xxmetalheadxx; 02-25-2006 at 07:32 AM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    firstly
    while (low = high)
    should be low==high

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    secondly
    i fail to see the usage of the return 0 right after the return -1
    Sent from my iPadŽ

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    um... if low was eqaul to high, then your number was not found eh? and why are you saying
    Code:
    if (num  marray.a[mid])
    ?
    there is no comparison made. unless you want to know if num is just as spaced out as marray.a[mid] but this is still the wrong syntax...
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    thirdly
    else if (num marray.a[mid])
    this wont even compile

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    sorry about that when i coupied the code it musta deleted some of my equal signs cuz they were all there i swear...i'll fix it in the first post...

  7. #7
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    jlf029 you're a copycat... what part of wrong syntax don't you understand? of course wrong syntax won't compile. anyway it doesn't matter. just letting you know that i know that you copied me...
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 04:15 PM
  2. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 06:19 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21