C Programming

Linear Search on Array of 10 elements

linear search is a search algorithm, also known as sequential search, that is suitable for searching a list of data for a particular value.It operates by checking every element of a list one at a time in sequence until a match is found.

Language : C

[sourcecode language='c']
//To conduct linear Search on an array of 10 elements

#include
#include

void main()
{ int a[10],x,i,found;
char ch;

found=0;

for(i=0;i<10;i++)
{
printf(“\nThe the %d element : “,i+1);
scanf(“%d”, &amp;amp;amp;a[i]);
}
do{
printf(“\nEnter the element to be searched :”);
scanf(“%d”, &amp;amp;amp;x);

for(i=0;i<10;i++)
{
if(a[i]==x)
{found=1;
break;
}

}
if(found==1)
printf(“\nThe element has been found at position %d!”,i+1);
else
printf(“\n %d was not found!”,x);

fflush(stdin);
printf(“\nSearch another element ? (Y/N) : “);
scanf(“%c”, &amp;amp;amp;ch);
found=0;
}while(ch==’y'||ch==’Y');
}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal