Progarm to Search an Integer in an Array of N integers using linear search

Tuesday, March 03, 2020

//progarm to search an integer in an array of N integers using linear search
#include<iostream.h>
#include<conio.h>
int linear(int A[],int N,int ELEMENT)
{ for(int i=0;i<N;i++)
  { if(A[i]==ELEMENT)
return i;
  }
  return(-1);
}
void main()
{ clrscr();
  int a[20],n,element,i,p;
  cout<<"\n Enter the number of elements";
  cin>>n;
  cout<<"\n Enter the elements of array";
  for(i=0;i<n;i++)
  { cout<<"\n Enter a["<<i<<"]:";
cin>>a[i];
  }
  cout<<"\n Array is:";
  for(i=0;i<n;i++)
  cout<<a[i]<<"\t";
  cout<<"\n Enter the element to be searched:";
  cin>>element;
  p=linear(a,n,element);
  if(p==-1)
  cout<<"\n Unsucessful search";
  else
  cout<<"\n Element "<<element<<" is present at index "<<p<<" and at position "<<(p+1);
  getch();
}

You Might Also Like

0 Comments

Like us on Facebook

Flickr Images

Flickr Short Films