Thread: finding number 7 in array using recursion

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    Post finding number 7 in array using recursion

    i want to find a number 7 in a array using recursion
    here is my programme
    Code:
    bool iscontainseven(int a[],int size){
        if(size == 0)
        {
            a[0] == 7;
            return true;
        }
    
    
        if(size == 1)
        {
            if((a[0]||a[1]) == 7)
                return true;
        }
    
    
          int smallerarray[100];
          for(int i = 1;i < size ;i++)
          {
    
    
              smallerarray[i-1] = a[ i];
          }
          bool issmallerarraycontain7 = iscontainseven(smallerarray,size - 1);
          if(issmallerarraycontain7)
            return true;
          else
            return false;
    }
    
    
    
    int main()
    {
    cout<<"enter no of elements"<<endl;
        int n;
        int a[50];
        cin>>n;
        for(int i =  0;i < n;i++)
        {
            cout<<"enter next element"<<endl;
            cin>>a[i];
        }
        cout<<endl;
        cout<<"printing an array"<<endl;
        for(int i = 0;i < n;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
      
     
        cout<<"is contain seven"<<endl;
       // here i call a function
        cout<<iscontainseven(a,n);
        cout<<endl;
    
    }
    but it gives error
    could someone plz correct me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > but it gives error
    You should tell us what that error is.


    Your size == 0 test is redundant. There are no elements in an array of size zero.

    Your size == 1 test must ALWAYS return either true or false. At the moment, it goes onto make copies of size -1, size -2, size -3 etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    sir it does not give error but i did not get desired result

    Quote Originally Posted by Salem View Post
    > but it gives error
    You should tell us what that error is.


    Your size == 0 test is redundant. There are no elements in an array of size zero.

    Your size == 1 test must ALWAYS return either true or false. At the moment, it goes onto make copies of size -1, size -2, size -3 etc.
    whenever i give input for eg 1 2 3 4 5 6
    output = 1
    if i give input 1 2 3 4 1 7
    output = 1
    so could u please give me the code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well the thing to do is run the program in a debugger and observe the flow of the program.

    Or perhaps more crudely, you could at least start with
    Code:
    bool iscontainseven(int a[],int size){
      cout << "Debug: iscontainseven: size=" << size << endl;
    and see if you gain any enlightenment.

    Then you add other debug output where you suspect there may be problems.

    > a[0] == 7;
    > return true;
    These two lines just return true. The comparison of a[0] with 7 is thrown away.

    > if((a[0]||a[1]) == 7)
    > return true;
    This is NOT how you compare two values with 7.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I highly recommend either writing all your functions and variabes in_snake_case or youShouldUseSomethingLikeCamelCase.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Highest Number in Array
    By nlp412 in forum C++ Programming
    Replies: 1
    Last Post: 08-12-2014, 09:14 PM
  2. Finding the biggest number in an array.
    By LSD in forum C Programming
    Replies: 11
    Last Post: 02-02-2014, 08:59 AM
  3. finding max in array using RECURSION
    By wonderwall in forum C Programming
    Replies: 5
    Last Post: 10-27-2011, 09:46 AM
  4. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  5. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM

Tags for this Thread