#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int *a,n,loc=-1,item;
cout<<"\nenter the no. of elements (max. 10):";
cin>>n;
a=new int[n]; //dynamically initialize array
cout<<"\nenter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"\nenter the element to be searched: ";
cin>>item;
for(i=0;i<n;i++)
if(a[i]==item)
{ loc=i;
break;
}
if(loc==-1)
cout<<"\n"<<item<<" not found!!!";
else
cout<<"location of "<<item<<" is: "<<loc+1;
getch();
}
By Er Gurpreet Singh,
Senior Software Engineer,
Sopra Steria India
Sunday, 2 August 2015
Linear Search Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment