# Thread: Help with my recursion

1. ## 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. 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)

3. 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. Remember this, the function returns a value to itself in each recursive call. That value is just thrown away in your code.

5. Hmm, is my recursive function now what I am coding is completely wrong . Where is the part I need to edit

6. 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.

7. 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. 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);
}```

9. 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. Originally Posted by evildotaing
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"?

11. No, i did try but still cant get it right. My recursion is quite weak.

12. 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. Try "if (count >= 1)"

14. 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. 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.

Popular pages Recent additions