1

In pset3's find. I have implemented Binary search. Everything works fine when I test it. But when I test it with check50, the last test fails saying: \ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 176...".

When I test it using the same cases everything works fine as it should.enter image description here

And then this happens when using check50

enter image description here

EDIT: here is my binary search code. I will remove it after some time or if I get a fix.

bool search(int value, int values[], int n)
{
    // base condition

    // check if only one left and equal to required value
    if (n == 1 && value != values[0])
    {
        return false;
    }
    // if odd and middle
    else if (n%2 == 1 && values[(n+1)/2] == value)
    {
        return true;
    }
    // else if even and middle
    else if (n%2 == 0 && (value == values[(n/2)-1] || value == values[(n/2)]))
    {
        return true;
    }
    // if odd
    else if (n%2 == 1)
    {
        if (value < values[(n+1)/2])
        {
            return search(value, values, ((n+1)/2));
        }
        else if (value > values[(n+1)/2])
        {
            for (int i = 0; i < (n+1)/2; i++)
            {
                values[i] = values[((n+1)/2)+1+i];
            }
            return search(value, values, ((n+1)/2));
        }
    }
    // if even
    else if (n%2 == 0)
    {
        if (value < values[(n/2)-1] || value < values[n/2])
        {
            return search(value, values, (n/2)-1);
        }
        else if (value > values[(n/2)-1] || value > values[n/2])
        {
            for (int i = 0; i < (n/2); i++)
            {
                values[i] = values[(n/2)+i];
            }
            return search(value, values, (n/2));
        }
    }
    return false;
}
  • Anyone? No one? – Mustaghees Aug 12 '15 at 6:31
  • I guess it's because no one has seen the issue :( having said that, the /opt/sandbox50/bin/run.sh does not exist on my appliance, so I assume its from the system that the check50 runs from. Why yours should error like that I am not sure. Perhaps emailing one of the course staff might elicit a better response. – John Finnigan Aug 15 '15 at 21:20
  • How can I email someone from staff and should I also email my code? – Mustaghees Aug 16 '15 at 1:10
  • Try emailing heads@cs50.harvard.edu for advice on how to proceed in locating help - I wouldn't send the code (unless asked) but rather a link to here. An alternative would be to seek help via the Facebook community - someone there may have seen the same issue - facebook.com/groups/cs50 – John Finnigan Aug 16 '15 at 12:06
0

Just reading the error message, I would go back and look at the specifications to double check and make sure the output you're supposed to provide when the element is not in the haystack is the same as what the specs require. That said, When reading you're code, I don't understand how you're actually trying to implement binary search; I see no recursion and no loops which would allow you to continue the search in the event that you don't find the element you're looking for right away. You'll need to post more information if you want someone to try and look further into it.

| improve this answer | |
  • I have used recursion. Included my code. Please see if you can find any problems or any improvements. Thank you! – Mustaghees Apr 21 '17 at 5:31

You must log in to answer this question.

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