Linear search program in c++ using recursion

Linear search program in c++ using recursion
Di Posting Oleh : Simple Learning
Kategori : codeblocks compiler linear search recursive code c++ recursively c++ linear search code recursively linear search c++

This program runs linear search recursively in an array c++ code
Compiler used: C++ Codeblocks CompilerConcept used:
  • Recursive function Linear Search
  • if else statement

How Program Works
  • Program takes size of array
  • Input elements in array
  • Passing array, key and size to the recursive function recursiveLinearSearch(int array[],int key, int size)
  • Recursive function calls it self until certain conditions fulfill
  • Function returns 1 if record found in array else returns -1



C++ code:
C++ program linear search program using recursive function



    #include<iostream>
    using namespace std;
  
    int recursiveLinearSearch(int array[],int key,int size){
    size=size-1;
      if(size <0){
      return -1;
      }
      else if(array[size]==key){
      return 1;
      }
      else{
      return recursiveLinearSearch(array,key,size);
      }
    }

 
    int main() {
  
      cout<<"Enter The Size Of Array:   ";
    int size;
    cin>>size;
    int array[size], key,i;
    
    // Taking Input In Array
    for(int j=0;j<size;j++){
    cout<<"Enter "<<j<<" Element : ";
    cin>>array[j];
    }
    //Your Entered Array Is
    for(int a=0;a<size;a++){
       cout<<"array[ "<<a<<" ]  =  ";
       cout<<array[a]<<endl;
    }
    cout<<"Enter Key To Search  in Array";
    cin>>key;
    int result;
    result=recursiveLinearSearch(array,key,size--);
    if(result==1){
    cout<<"Key Found in Array  ";
    }
    else{
    cout<<"Key NOT Found in Array  ";
    }
       return 0;
    }
Linear search program in c++ using recursion code end


More Programs.
Linear Search in  C++

Find Prime Number in C++

For more learning change the program and examine the output

0 Response to "Linear search program in c++ using recursion"

Post a Comment