# Thread: Recursion binary to decimal - question.

1. ## Recursion binary to decimal - question.

Hi,
I was asked to program a recursion (in C), which converts a binary to decimal.
The below code works well, yet I am not sure it fully exploits the potential of recursion (in particular the first two lines):

Code:
```int convert_bin_to_dec(int num)
{
iter++;
if (iter != 1) count*=2;
if (!(num / 10)) return (num * count);
return ((num % 10) * count + convert_bin_to_dec(num /10));
}```
Iter and count are global variables set to 0 and 1 respectively.
I'd sincerely appreciate any feedback/suggestions.

2. Originally Posted by peripatein
Hi,
Code:
```int convert_bin_to_dec(int num)
{
iter++;
if (iter != 1) count*=2;
if (!(num / 10)) return (num * count);
return ((num % 10) * count + convert_bin_to_dec(num /10));
}```
Where is the input of the binary number?
What is the format of the binary number?
Why are you using global variables?
Most good Instructors considered unneeded global variables to be wrong.

Edit: This might be a case where your convert_bin_to_dec calls a recursive helper function instead of being the recursive function.
It depends on the assignment requirements.

Tim S.

3. Why are global variables considered wrong in this case? We weren't explicitly forbidden to make use of global variables. How would you advise approaching it otherwise? The input of the binary number is in main(). Would you need that to assist me? I'd gladly post it if needed.

4. Is there a real reason why you CAN NOT pass the value of iter and count to the recursive function?
If no, then using Global Variables is wrong; note, I think it is still wrong if you have what you think is a real reason.

Tim S.

5. You didn't really elaborate how it ought to be handled differently. Would you recommend using another function to yield the number of binary digits the input has?

6. A helper function would allow you to set up iter and count inside and not have them as globals.

Code:
```int convert_bin_to_dec(int num)
{
if (!(num / 10)) return (num);
return (num % 10 + convert_bin_to_dec(num / 10) * 2);
}```
Is this valid?

8. I do not think the code you pasted now accounts for place value:

For example

2^0 2^1 2^2 2^3 2^4
1 0 1 0 1

which is 21.

It's a specific multiplication you need to perform in each place.

9. But it works perfectly! You may run it and see for yourself.

10. And with your input (viz. 10101) 21 was indeed the answer the program yielded.

11. Originally Posted by peripatein
Code:
```int convert_bin_to_dec(int num)
{
if (!(num / 10)) return (num);
return (num % 10 + convert_bin_to_dec(num / 10) * 2);
}```
Is this valid?
Looks good to me; but, you need to test it and verify it works for input values.

Tim S.

12. It does! (As I have verified)

13. If you have no further remarks or comments to make, I would like to thank you sincerely for your kind assistance!

14. I'd appreciate your comments on the following recursion, confirming that the number is a binary number (defined to be a positive number of 0's and 1's). Note, I am not allowed to use arrays, strings, pointers. We were instructed to use recursion!

Code:
```int is_valid_bin_num(int num)
{
if (num / 10 > 0 && num % 10 <= 1) return (is_valid_bin_num (num / 10));
if (num == 1 || num == 0) return 1;
return 0;
}```
It feels slightly cumbersome to me.

15. Just checking: are you sure that your functions are supposed to have an int parameter and an int return type?

I ask because it is sensible to talk about say, converting the binary representation of an integer as a string into the decimal representation of the integer as a string, or converting the binary representation of an integer as a string into the int value, but converting from int to int does not make sense to me.