0

So I've started working on pset3 after a long break, I couldnt even start making a binary search function. Tried to do a linear search function but it doesnt seem to work when i run "./generate 1000 50 | ./find 127"

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

            else
            return false;
        }
    }
    else
    return false;

return false;    
}

1 Answer 1

1

That code will only test the first element in the list. If the first element is not the target, it will return false. Instead, if the first, or any element in the list is not the target, it should do nothing in the for loop. It should just loop and test the next element. It should only return false after the loop has completed and the target has not been found.

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

0

You must log in to answer this question.

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