Wednesday, 29 October 2014

linear search in an array

#include<stdio.h>
#define MAX 20
int main()
{
     int a [MAX];
     int i,search,n;
     printf("enter no. of elements:");
     scanf("%d",&n);
     printf("enter the elements\n");
     for(i=0;i<n;i++)
     {
         scanf("%d",&a[i]);
     }
     printf("enter element to be searched\n");
     scanf("%d",&search);
     for(i=0;i<n;i++)
     {
     if(search==a[i])
     {
       printf("element %d found at location %d\n",search,i+1);
      break;
     }
     }
     if(i==n)
      printf("searched element %d is  not found in the array",search);
   getch();
   return 0;
}           

No comments:

Post a Comment

Note: only a member of this blog may post a comment.