how to do a sequential search in C - Programmers Heaven

Howdy, Stranger!

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

Categories

how to do a sequential search in C

henshens Posts: 10Member
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.

thanks


[code]#include
#include //needed for toupper function
#include
#include
#define SIZE 5


char menuVersion1 ();
void menuVersion2 (char *);
void fill ( int [] , int * );// void fill ( int * );
void print ( int [] );
void sort ( int [] , int * );

int main ()
{
char selection;
int A [SIZE] = {0}; // A is an array of 5 integers
int sorted = 0;

srand ( time (NULL) );

do
{
//selection = menuVersion1 ();
menuVersion2 ( &selection );



switch ( selection )
{
case 'F' : fill (A , &sorted);
break;

case 'P' : print (A);
break;

case 'S' : sort (A, &sorted);
break;


case 'Z' : printf ("Goodbye!

");
break;

case 'Q' : printf ("Query coming up!

");
if ( sorted == 0 ) printf ("the array is not sorted!
");
else printf ("the array is sorted!
");
break;

default : printf ("aa Invalid selection!!!

");
}

}while ( selection != 'Z');

system ("pause");
return 0;
}
//----------------------------

char menuVersion1 ()
{
char option;

printf ("
--------
F-Fill the array
");
printf ("P-Print the array
");
printf ("S-Sort the array
");
printf ("Z-Exit
");
printf ("Enter your selection :");
scanf(" %c", &option);

return toupper(option);


}

//--------------------------------
void menuVersion2 (char * option)
{


printf ("
--------
F-Fill the array
");
printf ("P-Print the array
");
printf ("S-Sort the array
");
printf ("Q-Query the array
");
printf ("Z-Exit
");
printf ("Enter your selection :");
scanf(" %c", option);

*option = toupper(*option);
}
//---------------------------------
void fill ( int A [] , int * sorted)
{
int walker;
for (walker = 0 ; walker < SIZE ; walker ++)
{
A[walker] = rand ()%999 + 1;
}
*sorted = 0;

printf ("ARRAY LOADED!!!
");

}

//----------------------------------------
void print ( int A [])
{
int walker;
printf ("
Here are the elements ...
");
for (walker = 0 ; walker < SIZE ; walker ++)
{
printf ("%d
", A[walker] );
}

printf ("a ARRAY PRINTED!!!
");

}
//------------------------
void sort ( int A[] , int * sorted )
{

int i , j , temp ;
*sorted = 1;

for ( j= 0; j A[i+1])
{
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
}



}


[/code]

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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]
    [code]
    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]
    [/code]
Sign In or Register to comment.