tags:

views:

79

answers:

4

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]);
}
+1  A: 
Matthew Flaschen
This is straight out of the book. He said this might be on the final and I want to make sure I know how to use it
shinjuo
I agree with 2, but that's pretty standard C though, the responsibility of allocating the storage for `*locn` is that of the caller
hhafez
hhafez, I think we both misunderstood. :) I thought `last` was the length of the array (it's apparently the `length - 1`), so it seemed like the element after the array could be illegally accessed. I was not talking about `locn`.
Matthew Flaschen
+1  A: 

It's pretty clear

list[] is the list you are searching last is the last index in the list target is what you are searching in list locn will contain the index at which target was found the return value is a boolean indicating if the target was found

for your question how to pass locn, do something like

int locn; /* the locn where the index of target will be stored if found */

bool target_found = seqSearch(blah blah blah, &locn);
hhafez
But what do I pass into locn?
shinjuo
'int locn; /* the locn where the index of target will be stored if found */bool target_found = seqSearch(blah blah blah, '
hhafez
+1  A: 

The problem with your code is if you search for an element not present in the array, looker will be equal to last and you try to access an array element at location last which is invalid.

Instead you can do:

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

    int looker;

    for(looker=0;looker<last;looker++) {

        // target found.
        if(list[looker] == target) {
            *locn = looker; // copy location.
            return true;    // return true.
        }
    }

    // target not found.
    *locn = -1;   // copy an invalid location.
    return false; // return false.
}

You call the function as follows:

int list[] = {5,4,3,2,1}; // the array to search in.
int size = sizeof(list)/sizeof(list[0]); // number of elements in the array.
int target = 3; // the key to search for.
int locn; // to hold the location of the key found..and -1 if not found.

if( seqSearch(list,size,target,&locn) ) {
  // target found in list at location locn.
} else {
  // target not found in list.
}
codaddict
How do you know that `last` isn't in the array? You could pass in the length - 1 for `last` and then it functions just fine. I think `last` here just refers to the fact that it's the last place you should check (if you only want to check part of an array for example), and that the responsibility for providing the correct endpoint falls on the caller.
MBennett
I am trying it like this, but it is giving me an error for using bool.
shinjuo
@shinjuo: You need to include `stdbool.h` here is a working example: http://www.ideone.com/pWmTx
codaddict
@unicornadict: stdbool.h is actually the answer that the question was asking for. You should repost as an answer.
nategoose
+1  A: 

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
}
cHao