Linear search

Program to search for a key element in an entered array using linear search.


#include<stdio.h>
#include<conio.h>
main()
{
int a[30],i,j,key,n,m,flag=0;
clrscr();
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
j=i;
if(flag)
{
printf("The key element is found in the position %d\n",i+1);
printf("Enter the no which is to be replaced with the key element\n");
scanf("%d",&m);
a[j]=m;
printf("Array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
else
printf("Key element is not found in the array");
getch();
return 0;
}
Category:

0 comments: