Wednesday, 19 October 2016

12-D Prog-13 Linear Search using 1-D Array

//PROG 13 LINEAR SEARCH
#include<iostream.h>
#include<conio.h>

int Lsearch(int AR[],int size,int item)
 { for(int i=0;i<size;i++)
  { if(AR[i]==item)
    return i;
  }
 return -1;
 }

void main()
{
int AR[50],ITEM,N,index;
cout<<"\nEnter Desired Array Size: ";
cin>>N;
cout<<"\nEnter Array Elements: ";
for(int i=0;i<N;i++)
{
cin>>AR[i];
}
cout<<"\nEnter Element to be searched for : ";
cin>>ITEM;
index=Lsearch(AR,N,ITEM);
if(index==-1)
cout<<"\nSOrry!! Given Element could not be found.";
else
cout<<"\nElement found at index : "<<index<<", Position :"<<index+1;
getch();
}

No comments:

Post a Comment