0

I cannot get my linear search programm to work and it seems to be a problem with the return of false if the needle is not found. My coding for the search is

bool search(int value, int values[], int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (values[i] == value)
        {
           return true;
           break;
        }
    }
    //return false;
}

If I comment out the the return false; line as per the code above the compiler gives me an error message:

helpers.c:37:3: error: control may reach end of non-void function [-Werror,-Wreturn-type]

But if I run the code with an active return false; line, check50 gives me error messages saying that the code is generating exit codes of 1 when it should have been 0.

Here is a copy of the check50's result

jharvard@appliance (~/Dropbox/pset3/find): check50 2014.fall.pset3.find helpers.c

:) helpers.c exists

:) helpers.c compiles

:) finds 42 in {42,43,44}

:( finds 42 in {41,42,43}
   \ expected an exit code of 0, not 1

:( finds 42 in {40,41,42}
   \ expected an exit code of 0, not 1

:( finds 42 in {41,42,43,44}
   \ expected an exit code of 0, not 1

:( finds 42 in {40,41,42,43}
   \ expected an exit code of 0, not 1

:( finds 42 in {39,40,41,42}
   \ expected an exit code of 0, not 1

:) doesn't find 42 in {39,40,41}

:) doesn't find 42 in {39,40,41,43}

check50 (if I read it right) is saying that the return false line is executing even when the needle has been found. So I think I would need to either

a) Add some code so that return false executes only after the loop, if the loop had completed and there was no return true. This seems a bit complicated and does not seem to be the approach that others have taken when I was looking at other questions here.

b) Leave out the false condition and have a return of some kind that would allow the satisfy the compiler to overcome its error (i.e., control may reach end of non-void function).

Can anyone help point me in the right direction please? Thanks!

  • 1
    Your search function works great the way you had it, i.e. the return false should not be commented. As and improvement you don't need the break; command after the return true statement, as the flow has already been returned to the caller function, and so break never executes. Also take care to return false right away if n is negative (per the spec). [--->] – ChrisG Oct 2 '15 at 14:04
  • Now, that said, you problem must reside in find.c itself. Have you changed anything else inside find.c or helpers.c? Could you post the whole codes? – ChrisG Oct 2 '15 at 14:05
  • Thanks so much for your help. I made the changes you suggested and then looked again at the other files I found an error in my bubble sort coding in helpers.c . There was a line of code which was outside a closing curly brace when it should have been inside.(This was due to bad copy and pasting on my part from the file I had been working in to code for bubble sort) ! I corrected that and now check50 is not showing errors. – salim Oct 2 '15 at 16:39
  • 1
    Glad you could solve it! Keep coding! :) – ChrisG Oct 2 '15 at 16:50

You must log in to answer this question.

Browse other questions tagged .