Linear Search Program

#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();
}

Comments

Popular posts from this blog