0

This function aims to return the number of zeroes in a number, num. The function rCountZeros2() passes the result through the pointer parameter result. `

void rCountZeros2(int num, int *result)
{
    if (num==0)
        return;
    else
    {
        if (num%10==0){
            (*result)++;
            }
        rCountZeros2(num/10, result);

    }
}

`

5
  • 2
    can you show how you are calling it? This should work rather nicely Commented Feb 28, 2019 at 3:18
  • There doesn't seem to be any issues with the logic of your function. Commented Feb 28, 2019 at 3:19
  • please show how you called the function, what you saw, and what you expect. Commented Feb 28, 2019 at 3:21
  • 1
    My guess is you didn't initialize the pointer in the original call, or you didn't initialize the variable that it points to. Commented Feb 28, 2019 at 3:32
  • Why not just return the result, rather than writing it to a pointer? But if you want to do it this way, you should store the result to the pointer, rather than adding the result to what it previously pointed to. Commented Feb 28, 2019 at 3:35

1 Answer 1

2

See when you are invoking rCountZeros2() , my guess is value in variable result is not zero.It may be some garbage value or some other value from previous computation.However with details you have provided it is difficult to provide exact answer.

Kindly try the following standalone program, I got correct answer using your code

void rCountZeros2(int num, int *result)
{
    if (num==0)
        return;
    else
    {
        if (num%10==0){
            (*result)++;
            }
        rCountZeros2(num/10, result);

    }
}
int main()
{
    int result = 0;
    int num=12300000;
    rCountZeros2(num, &result);
    printf("number of zeros in %d = %d",num ,result);
}
Sign up to request clarification or add additional context in comments.

3 Comments

alright my guess is that the 'garbage value' in my code is an 8 for result. do you know how to get rid of it?
@King_Arthur doresult = 0; just before calling the function rCountZeros2(num, &result);
@King_Arthur mark answer as correct if it worked for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.