Pages

Sunday, February 20, 2011

LINEAR SEARCH

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[50],n,x,flag;
cout<<"/////////////////// LINEAR SEARCH//////////////////";
cout<<"\n enter the size of array (max 50)";
cin>>n;
cout<<"\n enter array elements";
for(int i=1;i<=n;i++)
cin>>a[i];
cout<<"\n enter an element you want to search";
cin>>x;
for(int j=1;j<=n;j++)
{
if(a[j]==x)
{
flag=1;break;
}
else
flag=0;

}
if(flag==1)
cout<<"\n element found at position "<<j;
else cout<<"element not found";
getch();
}

No comments:

Post a Comment