0

I'm working on linear search. When I test manually, it works correctly, and "echo $?" will show a 0 and 1 as expected. I have tried it with piped-in data sets, with the 127 and 128 examples from the instructions, and with the "find 42" things in the check50 results. I verify with "echo $?". All return as expected.

When I run it through check50, it fails as below. It's not a timeout issue or an intermittent issue, same exact results across 3 days and many attempts.

I have used the same function declaration as originally provided in helpers.h.

Command: check50 2015.fall.pset3.find helpers.c
:) helpers.c exists
:) helpers.c compiles
:( finds 42 in {42,43,44}
\ expected an exit code of 0, not 1
:( 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}
\ expected an exit code of 1, not 0
:( doesn't find 42 in {39,40,41,43}
\ expected an exit code of 1, not 0
https://sandbox.cs50.net/checks/27f2ac1ae0094ccebecbfaefcd5fbe73


My code:

bool search(int value, int values[], int n)
[removed for honor code purposes]
        return 0;  
        }  
    }  
    return 1;  
}  

1 Answer 1

2

What bool does 0 represnet - true or false?

Your search() routine returns a bool, meaning either true or false. However, you have fallen into the trap of using numbers instead of true or false. The compiler treats 0 as false and any non-0 as true, so you are returning false where you want to return true and vice versa.

This is understandable, because there is another standard practice of returning 0 for successful completion and a non-zero number for any of many error codes, depending on the program. Do not confuse returning a bool with returning a number for success or fail/error code.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • 1
    So much jumping up and down here! You are wonderful. Lovely. I'm going to go up and strip out most of my code above for future honor code purposes. Can't thank you enough! Jul 18, 2016 at 23:36

You must log in to answer this question.

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