View Single Post
Old Jun 12th, 2005, 8:06 AM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 551
Rep Power: 4 Ancient Dragon is on a distinguished road
That seemed to work for all the values I could throw at it. using rand() by itself doesn't seem to produce large enough values, so I changed it to rand() * rand(), as in my previous tests.

I think you have the num_tries counter in the wrong place -- it doesn't count the number of tries correctly. In the code below I also typecast the array a to int when making the comparisons to insure integer test rather than double test.

      if ((k < lb) || (k > ub))
      { 
          return -1;
      }
      (*num_tries)++;
      if ((int)(a[k]) == key)
      {
         return k;
      }
      else if ((int)(a[k]) > key)
      {
//         *num_tries = *num_tries + 1;
         return search(a, lb, k-1, key, num_tries);
      }
      else
      {
//         *num_tries = *num_tries + 1;
         return search(a, k+1, ub, key, num_tries);
      }

Last edited by Ancient Dragon; Jun 12th, 2005 at 8:09 AM.
Ancient Dragon is offline   Reply With Quote