Linear Search | Sequential Search In Data Structure Using C++

Data Structure Using C++

Array Data Structure

Searching

Linear Search | Sequential Search

void linearSearch(int arr[],int key, int n);
int main ()
{
 int arr[10], key, i;
 for(i=0;i<10;i++)
 {
  arr[i]=rand()%100;
 }
 cout<<"The unsorted array: ";
 for(i=0;i<10;i++)
 {
  cout<<arr[i]<<" ";
 }
 cout<<"\nEnter any number to find: ";
 cin>>key;
 linearSearch(arr, key, 10);
}
void linearSearch(int arr[],int key, int n){
 bool found=false;
 int i;
 for(i=0;i<10;i++){
  if(arr[i]==key){
   found=true;
   break;
  }
 }
 if(found!=true)
  cout<<"Value not found.";
 else
  cout<<"Value found at : "<<i;
}

Let me know in the comment section if you have any question.

Previous Post:
Priority Queue Using Heap In Data Structure Using C++

Comments