Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

double* binary_search(double needle, double* hay_begin, double* hay_end) { int m

ID: 3545982 • Letter: D

Question

double* binary_search(double needle, double* hay_begin, double* hay_end)

{

   int middle;

   double* needptr; //assume won't be found

   bool found = false;

   while (!found && hay_begin <= hay_end)// not found and score might still be in list

   {

      middle = (hay_end - hay_begin) / 2; // center between left & right

      if (needle < *(hay_begin + middle))// searchValue might be found to the left of finalExams[mid]

         hay_end = hay_end - middle - 1;

      else if (needle > *(hay_begin + middle))// searchValue might be found to the right finalExams[mid]

         hay_begin = hay_begin + middle + 1;

      else if(needle == *(hay_begin + middle))// searchValue is finalExams[mid]

         found = true;

   }  // end while

   if (found)//found score

   {

       *needptr = *hay_begin + middle;

   }

   else

       *needptr = 0.0;

   return needptr;

}


Here is my code, I already point hay_begin and hay_end to the first element  and last element of array in main function. While I am running this code,it shows me Segmentation fault. I can't figure out which part I got a problem. This assignment ask me return a pointer if the value is found or return a 0. Can anybody help me?

Explanation / Answer

template class MyBinaryTree { T val_; std::list left_; std::list right_; template friend MyBinaryTree MakeMyBinaryTree (U v, MyBinaryTree *l = 0, MyBinaryTree *r = 0) { MyBinaryTree t; t.val_ = v; if (l) t.left_.push_back(*l); if (r) t.right_.push_back(*r); return t; } public: MyBinaryTree* left () { return left_.empty() ? 0 : &*left_.begin(); } MyBinaryTree* right () { return right_.empty() ? 0 : &*right_.begin(); } T & val () { return val_; } }; MyBinaryTree make_a_tree () { MyBinaryTree n1 = MakeMyBinaryTree(1); MyBinaryTree n3 = MakeMyBinaryTree(3); return MakeMyBinaryTree(2, &n1, &n3); }