0

this is my code for the top part

{
    for (int i = 0; i < n; i++)
        {

        if (values[i] == value)
            {
            return true;
            }
        else
            {
            return false;
            }
        }
        return 0;
}

It compiled when I typed make all. However, when I try testing the code with the CS50 example which was supposed to work every time, it returned false.

1 Answer 1

1

Hard to see what exactly is going on without getting a look at your wider code but the logic in this loop can't be right.

Your "for" loop will only ever run once because it will return either true or false on its first run. I would suggest that you remove the "else{return false;}" and change "return 0;" to "return false;" at the end - that way false will only be returned if the "for" loop has iterated all comparisons from values[0] to values[n-1].

2
  • thx for pointing it out
    – BooFluff
    Jun 2, 2015 at 2:28
  • Oh, it works now!
    – BooFluff
    Jun 2, 2015 at 2:28

You must log in to answer this question.

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