views:

721

answers:

4

I am doing a C course. I need to do a recursive XOR binary but I have some limitations. I can't use loop or any math.h functions, nor can I call another function from the XOR function.

This is the function prototype:

int binaryXor(int firstnumber[], int secondnumber[], int length);

where firstnumber and secondnumber are arrays with the same length of 1s and 0s and length is their length.

The function should return the decimal value of the XOR of those two arrays. Doing the XOR is quite simple, but how can I convert it to decimal with all the limitations?

A: 

A recursive function call can be used in place of a loop.

Richard Pennington
True - I hate these sort of hw problems. Who would ever use recursion to iterate over an array?
Martin Beckett
In C, nobody. In functional languages, everybody. So arguably yes, the school should teach recursion either with a functional language or a more natural example. But a recursion which is not trivially reducible to a loop is not tail-recursive, and problems which can't be made tail-recursive might be a bit beyond the level of the course at the early stage where recursion is introduced. At least, I assume that's the thinking that leads to these "do something you'd never really do, just to illustrate a point" assignments.
Steve Jessop
+3  A: 

In order to write a recursive function, with no loops, you need to answer the following question:

"How can I express the answer to my problem in terms of a smaller problem?"

In this case, the problem is that you have length digits to look at, but you're not allowed to loop. So, how do you express an xor of size length in terms of a smaller xor, together with some amount of work that doesn't require a loop?

[Edit: hang on, just looked at your question again, and you say you already have the xor sorted, so I guess you've already done this. In that case my comment above is the only thing you need to know: you're finished. An int in C is not a decimal value, it's just a value. You don't need to convert anything to decimal in order to store or return it in an int.

If you're interested though, I can post code that does convert an int to a decimal value using a recursive function. One simple way is to work out on the way "down" how many digits are required, by comparing with bigger and bigger powers of 10, and then on the way back "up" print the digits out starting from the end.]

Steve Jessop
+2  A: 

This is a standard recursive question. The trick is to realize that the integer value of a a string of 1s and 0s followed by a 1 or 0, is 2 * the integer value of the string plus the value of the digit.

So you will want to do something like

if( length <= 0) return 0;

return 2 * binaryXOR(firstnumber, secondnumber, length - 1) + (firstnumber[length - 1] ^ secondnumber[length - 1]);
UncleO
What is the relative precedence of '+' and '^'? So why does your expression give the wrong answer? Use brackets (parentheses) when you mix arithmetic and bitwise operations!
Jonathan Leffler
Thanks Jonathan. I usually don't test these homework answers. The OP can learn something from my mistakes!
UncleO
Thank you very much, your helped me a lot.
Ariel
A: 

Sir/Madam

Hi, this is vishal

I need some recursive programs in C-lang. like factorial, fibonancci, dec to bin and bin to dec of full program not code snippet.

I have seen that bin to dec code snippet

please give me support for the same above

thank you

Vishal