Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


how to do a sequential search in C

henshens Member Posts: 10
I'm trying to do a binarysearch in this piece of code that i have here. if the array has been sorted use a binary search, if it is not sorted use a sequential search. I've manage to get the binarysearch section of the code working, however, no joy with the sequential search.

Can anyone show show me to accomplish this? i'm a newbie to the language, and i'm still learning this stuff. maybe a starting point, then i might be able to figure out the rest of it out myself.


#include //needed for toupper function


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Well, simply look at each element in the array and if it matches your search element, then return its index, otherwise (if array is scanned, but no element located) - return -1:[/color]
    int FindIndexOf (int* array, int n, int find_this)
    int i;
    for (i=0; i<n; i++)
    if (array [i] == find_this) return i;
    return -1;

    [color=Green]// Using it:[/color]

    int a [5] = { 6, 93, 75, 332, 9 };

    int index1 = FindIndexOf (a, 5, 73663); [color=Green]// Will be -1, because 73663 is not there![/color]
    int index2 = FindIndexOf (a, 5, 75); [color=Green]// Will be 2, because 75 is there![/color]
Sign In or Register to comment.