Thread: Help with my recursion

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    Help with my recursion

    Code:
    #include <iostream>
    
    using namespace std;
    
    int countZero(int a[], int count);
    
    
    void main(void)
    {
    	int size = 10;
    	int Myarray[10] = {6, 0, 0, 7, 9, 3, 8, 2, 1, 8};
    	
    	cout << "The numbers of zero in the array is " ;
    	cout << countZero(Myarray, size);
    	cout << endl;
    	system ("pause");
    }
    
    
    int countZero(int a[], int count)
    {
    	int s = 0;
    	if (count == 1)
    	{
    		if (a[count] == 0)
    		{
    			s ++;
    		}
    		else
    		{
    			countZero(a, count - 1);
    		}
    		
    	}
    	else
    	{
    		countZero(a, count - 1);
    	}
    	
    	return s;
    }
    I am doing a code where the recursion function count the number of zero in an array, but the recursive function did not work well. What is wrong with my code, some1 help me, thank you.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Your recursion should somehow add something up. In your case, the number of zeros.

    a[count] will never exist. You need to check a[count-1] for the last element of the array.

    You call your function recursively, but somehow you forgot to do something with the result. You need to add up the result of the recursive calls.

    Edit: I think it gets a lot easier if you terminate your function at count == 0:

    // check if count is zero... if so, return 0, "no" element can not contain 0

    // check if last element is zero or not and keep in mind 1 (if it's zero) or 0 (if it's not)

    // return the value you kept in mind plus the value of zeros in the rest of the array (recursion happens HERE)
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    hmm, i still a bit unclear how to implement the recursive function. I already added the count - 1 to check all the array to get the number of zero but somehow the recursive does not work.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Remember this, the function returns a value to itself in each recursive call. That value is just thrown away in your code.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Hmm, is my recursive function now what I am coding is completely wrong . Where is the part I need to edit

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What you have been given already is enough to fully solve the problem. Read what has been posted very carefully, update your code and then post the updated code.
    Ask very specific questions is you need more help.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    int countZero(int a[], int count);
    
    
    void main(void)
    {
    	int size = 10;
    	int Myarray[10] = {6, 0, 0, 7, 9, 3, 8, 2, 1, 8};
    	
    	cout << "The numbers of zero in the array is " ;
    	cout << countZero(Myarray, size);
    	cout << endl;
    	system ("pause");
    }
    
    
    int countZero(int a[], int count)
    {
    	int s = 0;                            //s to count the number of zero in an array
    	if (count != 0)
    	{
    		if (a[count - 1] == 0)
    		{
    			s ++;                          //to count the number of zero in an array
    			countZero(a, count - 1);
    		}
    		else
    		{
    			countZero(a, count - 1);      //go back to the function
    		}
    		
    	}
    	else
    	{
    		if (a[count - 1] == 0)
    		{
    			s ++; 
    		}
    	}
    	
    	return s;                               //return the number of zero back to the function
    }
    This is my latest code. The output print 0 when it should be printing 2 instead. What is wrong with my recursion?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your recursive function returns a value, which you're ignoring.

    So perhaps the essential part of your code should be.
    Code:
            if (a[count - 1] == 0)
            {
                return 1 + countZero(a, count - 1);
            }
            else
            {
                return 0 + countZero(a, count - 1);
            }
    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.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    cant work , i received this error "recursive on all control paths, function will cause runtime stack overflow" . hmm i am stuck in this question

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by evildotaing View Post
    cant work , i received this error "recursive on all control paths, function will cause runtime stack overflow" . hmm i am stuck in this question
    Hmm, why do I get the feeling that you copy-paste everything we write and just press "compile"?
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    No, i did try but still cant get it right. My recursion is quite weak.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    int countZero(int a[], int count);
    
    
    void main(void)
    {
    	int size = 10;
    	int Myarray[10] = {6, 0, 0, 7, 9, 3, 8, 2, 1, 8};
    	
    	cout << "The numbers of zero in the array is " ;
    	cout << countZero(Myarray, size);
    	cout << endl;
    	system ("pause");
    }
    
    
    int countZero(int a[], int count)
    {
    	if (count < 1)
    	{
    		if (a[count - 1] == 0)
    
    		{
    			return 1 + countZero(a, count - 1);
    		}
    		else
    		{
    			return 0 + countZero(a, count - 1);
    		}
    	}
    	else
    	{
    		return 0;
    	}
    
    }
    My latest code, still got problem.

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Try "if (count >= 1)"
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    int countZero(int a[], int count);
    
    
    void main(void)
    {
    	int size = 10;
    	int Myarray[10] = {6, 0, 0, 7, 0, 3, 0, 2, 1, 8};
    	
    	cout << "The numbers of zero in the array is " ;
    	cout << countZero(Myarray, size);
    	cout << endl;
    	system ("pause");
    }
    
    
    int countZero(int a[], int count)
    {
    	if (count > 1)
    	{
    		if (a[count - 1] == 0)
    
    		{
    			return 1 + countZero(a, count - 1);
    		}
    		else
    		{
    			return 0 + countZero(a, count - 1);
    		}
    	}
    	else
    	{
    		return 0;
    	}
    
    }
    Yes, it finally work, some carless mistake on that count. Haha thank you, my final code.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    As you didn't use the correct fix that you were shown, it still has a bug.
    Try it with:
    Code:
        int size = 1;
        int Myarray[1] = {0};
    or
    Code:
        int size = 3;
        int Myarray[3] = {0, 6, 3};
    Spot the problem?

    You need a higher level of attention to detail, to be a programmer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion
    By arctic_blizzard in forum C Programming
    Replies: 9
    Last Post: 10-26-2008, 04:37 PM
  2. Recursion
    By trevordunstan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 11:36 PM
  3. recursion
    By GaPe in forum C Programming
    Replies: 0
    Last Post: 06-03-2003, 01:35 AM
  4. can someone help me out with recursion
    By JOsephPataki in forum C Programming
    Replies: 10
    Last Post: 05-13-2003, 04:55 PM
  5. Recursion
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2002, 08:33 PM