Search



Thursday, 26 February 2015

linear search in array

In this program, the task is to take input of n no. of elements in , store it in array & then find whether the element entered by user is present in the array or not using linear search . Here , n is the no. of elements in array & Using for loop , we will get the values in order of a[0] , a[1] , - - - - till a[n] . Afterthat , we take input of the element to be searched . In linear search , we apply a for loop & then apply if condition that if value entered by user is equal to value in array then stop & print value exists in table else & value is not present in the array. 

#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],i=0,n,find,c=0;
clrscr();
printf("\n program to search an element in array");
printf("\n\n enter no. of elements in array (max 20) : ");
scanf("%d",&n);
printf(" now enter them : \n");
for(i=0;i<n;i++)
{ printf("enter a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n\n enter element to search : ");
scanf("%d",&find);
for(i=0;i<n;i++)
{ if(find==a[i])
{ printf("element = %d found at postion %d",a[i],i);
c=1;
}
}
if(c==0)
printf("element %d does not exist in array",a[i]);
getch();
}

No comments:

Post a Comment

Guys if you think something is wrong or should be edit than please do comment.