0

I'm stuck with my pset3 helpers.c function, I'm getting the following error when I compile without the last return statement

error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
make: *** [find] Error 1

But if I use the last return statement, program is not printing any output in the terminal. I'm really confused about the use of return statement. Is it necessary to have a return call at the end of this do-while loop, because I have a return call inside the loop for all possible outputs ? Where am I going wrong?, any help is appreciated.

bool search(int value, int values[], int n)
{
int start = 0; int end = n-1; bool check = false; int k = 0; 
do
{
   k = (int) (start + end)/2;
   if (values[k] == value)
   {
       check  = true;
       return true;
   }
   else if (values[k] > value)
   {
       end = k - 1;
   }
   else if (values[k] < value)
   {
       start = k + 1;
   }
   else if (start >= end)
   {
       check = true;
       return false;
   }
   else 
   {
       check = true;
       return false;
   }
}
while(check == false);
return true;
}
0

Yes, it is necessary to have the return, otherwise the program won't compile, full stop.

So why is it "not printing any output to the terminal"? Because it is stuck in an infinite loop (when value is not found). You know program should be done looking ("base case") when start > end. This program will never reach that test! If start changes or end changes, control returns to the do loop.

Notice the difference between start > end and start >= end. If start == end don't you have one more test?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .