Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Linear search using function in C

/* Linear Search using function in C*/

long linear_search(long [], long, long);

int main()
   long array[100], search, c, n, position;

   printf("Input number of elements in array\n");
   scanf("%ld", &n);

   printf("Input %d numbers\n", n);

   for (c = 0; c       scanf("%ld", &array[c]);

   printf("Input number to search\n");

   position = linear_search(array, n, search);

   if (position == -1)
      printf("%d is not present in array.\n", search);
      printf("%d is present at location %d.\n", search, position+1);

   return 0;

long linear_search(long a[], long n, long find) {
   long c;

   for (c = 0 ;c       if (a[c] == find)
         return c;

   return -1;
Read More:
Simple Linear Search in C 
Linear search for multiple occurrences
Linear search function using pointers in C

This post first appeared on Ask For Program, please read the originial post: here

Share the post

Linear search using function in C


Subscribe to Ask For Program

Get updates delivered right to your inbox!

Thank you for your subscription