0

So this is the begining step ive taken towards completeing the searching algorithm in helpers.c. What I wanted to know is if im heading in the right direction based off my code so far.

    bool search(int value, int values[], int n)

{ // TODO: implement a searching algorithm

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

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

}

I know next i need to implement a sorting algorithm of choice, and then to quicken my searching process, go back and redo my searching algorithm to a bubble sort of choice. Either recursive or iterative. Currently my search is suppose to be in a linear format. Am I checking out okay?

0

For linear search, this looks almost fine. You have to compare values[i] to value, not to values.

I would probably add some curly braces to the loop like

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

just to make it clear what is in the loop and what not, and have some consistent indentation (I don't know how much of it got messed up posting the code here, but you'd have more indentation for the for loop's or if's body).

3
  • thanks i misspelled meant to have value there and not values and thanks for the reminder of brackets. I'm just trying to chip away at this one and any little bit of help matters! – Chris Mar 17 '17 at 16:23
  • The curly braces are not required for single-statement body, but I think they add readability (and if you add an additional statement later and forget to add them, you break code) – Blauelf Mar 17 '17 at 17:40
  • I'm trying to move on to the sort portion of this program so i can create helpers and see where im at. But unfortunately the sort walkthrough video with zamyla isnt working. saying i dont have permission. did they remove it? – Chris Mar 17 '17 at 18:37

You must log in to answer this question.

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