Friday, 8 February 2013

PROGRAM TO PERFORM LINEAR SEARCH ON THE LIST OF ELEMENTS | Data Structures Tutorial pdf

LINEAR SEARCH-One by one comparison of each element in an array with the data item to be searched is known as linear search. The linear search continues until the elements to be searched is found or the end of the array is reached.
/*PROGRAM TO PERFORM LINEAR SEARCH ON THE LIST OF ELEMENTS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,loc,item,n,count=0;
clrscr();
printf("enter the size of array:");
scanf("%d",&n);
printf("\n enter the elments of array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the element to be searched:");
scanf("%d", &item);
for(i=0;i<n;i++)
{
if(a[i]== item)
{
loc=i;
printf("%d is present at %d location:",item, loc+1);
count++;
}
}
if(count!=0)
{
printf("\n the number is present %d times",count);
}
else
printf("\n item is not present");
getch();
}

No comments: