Search code examples
csequential

Trouble with a sequential search algorithm


Also why does this give me an error because I used bool?

I need to use this sequential search algorithm, but I am not really sure how. I need to use it with an array. Can someone point me in the correct direction or something on how to use this.

bool seqSearch (int list[], int last, int target, int* locn){
     int looker;

     looker = 0;
     while(looker < last && target != list[looker]){
                  looker++;
     }

     *locn = looker;
     return(target == list[looker]);
}

Solution

  • Looks like you'd use it like this...

    // I assume you've set an int list[], an int listlen and an int intToFind
    
    int where;
    bool found = seqSearch(list, listlen - 1, intToFind, &where);
    if (found)
    {
        // list[where] is the entry that was found; do something with it
    }