18 Replies  1350 Views  Last Post: 24 March 2013  05:02 PM
#1
xor function
Posted 21 March 2013  12:23 AM
I need some help to write a recursive Xor function.
The function gets two arrys of int\char, which include the binary numbers.
the function should calculate thier Xor, and return the result in a decimal number.
the prototype of the function should be: int binaryXor(int n1[],int n2[],int length);
There are some limitations:
a. you can't use any loops.
b. you can't use pointers or static variables.
c.You can't use any sub function.
d.you can't change anything in the prototype.
I thought of an idiea: let's say that 0110 is the Xor of the two numbers, so we just need to move each
digit to the n[length1] place and double it by 2 each time, but I have no idiea how do implement it....
Thanks
Replies To: xor function
#2
Re: xor function
Posted 21 March 2013  03:12 AM
#3
Re: xor function
Posted 21 March 2013  03:50 AM
I dont know how should I do that, I dunno how to convert the number into a decimal form
meanwhile the function makes the Xor...
#4
Re: xor function
Posted 21 March 2013  05:44 AM
Anyway the question still stands, have you been taught recursion? One of the ideas behind recursion is taking a big problem and breaking it down into smaller pieces and solving the smaller problem.
#5
Re: xor function
Posted 21 March 2013  07:08 AM
int binaryXor(int num1[],int num2[],int length){ if(length==0) return 1; if(num1[length]!=num2[length]){ length; return 2*binaryXor(num1,num2,length); } length; return binaryXor(num1,num2,length); }
thanks
#6
Re: xor function
Posted 21 March 2013  07:31 AM
the ^ symbol is the exclusive OR function in c/c++
that might help you ..
Snoopy.
#8
Re: xor function
Posted 21 March 2013  02:04 PM
int sum(int numbers[], int length) { if(length == 0) return 0; }
#9
Re: xor function
Posted 22 March 2013  03:34 AM
#define, on 21 March 2013  02:04 PM, said:
int sum(int numbers[], int length) { if(length == 0) return 0; }
like this:
int sum(int arr[],int length,int total){ if(length<0) return total; total=total+arr[length]; }
and in the main function I send to the sum function the following arguments: (arr,length1,0);
how does it helps me now?
#10
Re: xor function
Posted 22 March 2013  05:14 AM
23 + 74 + 49 + 9
You could do this by:
( 23 + 74 + 49) + 9 ( ( 23 + 74) + 49) + 9 ( ((23) + 74) + 49) + 9 ( ( 97) + 49) + 9 ( 148) + 9 157
Notice the recursive nature where to add N items, you can get the sum of N1 items plus the Nth item.
This post has been edited by Skydiver: 22 March 2013  05:14 AM
#11
Re: xor function
Posted 22 March 2013  07:50 AM
#12
Re: xor function
Posted 22 March 2013  08:35 AM
For one thing why return 1 when length = 0 shouldnt you be returning 0 ?
It just seem you have copied this code and dont really get what it is you have to do...
for one thing it would be easier to have to arrays of type char to do this and then output an int as your result
What everyone is trying to do is get you to focus on the problem
if I said 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 number.
would that mean anything to you ?
would it help ?
Snoopy.
#13
Re: xor function
Posted 22 March 2013  08:47 AM
chenfire, on 22 March 2013  06:34 AM, said:
int sum(int arr[],int length,int total){ if(length<0) return total; total=total+arr[length]; }
and in the main function I send to the sum function the following arguments: (arr,length1,0);
how does it helps me now?
First, arrays start at index = 0 and end at length  1, so arr[length] is undefined. Second, you need to change the index if you want to access more than one element and keep the function from calling itself until the system runs out of memory. Third, your function should always return a value!
int sumarry(int arr[], int length) { int thesum = 0; if(length > 0) { thesum = arr[length1] + sumarry(arr,length1); } return thesum; }
#14
Re: xor function
Posted 22 March 2013  09:18 AM
CTphpnwb, on 22 March 2013  08:47 AM, said:
chenfire, on 22 March 2013  06:34 AM, said:
int sum(int arr[],int length,int total){ if(length<0) return total; total=total+arr[length]; }
and in the main function I send to the sum function the following arguments: (arr,length1,0);
how does it helps me now?
First, arrays start at index = 0 and end at length  1, so arr[length] is undefined. Second, you need to change the index if you want to access more than one element and keep the function from calling itself until the system runs out of memory. Third, your function should always return a value!
int sumarry(int arr[], int length) { int thesum = 0; if(length > 0) { thesum = arr[length1] + sumarry(arr,length1); } return thesum; }
you didn't look at evryting I wrote right?
#15
Re: xor function
Posted 22 March 2013  10:58 AM
int sum(int arr[], int length);
The call from main.
int main() { int total = 0; int array[2] = {10, 20}; /* length of array is 2 */ total = sum(array, 2); }
Function construction :
/* array is {10, 20} length 2 */ int sum(int arr[], int length) { /* if length is 0 there is no data left so return 0 */ if(length <= 0) return 0; /* initial length is illegal so decrement length. */ length; /* return current value plus the total of the rest of array */ return arr[length] + sum(arr, length); }
So each call is like :
total = sum({10, 20}, 2); return 20 + sum(arr, 1); return 10 + sum(arr, 0); return 0;
.
This post has been edited by #define: 22 March 2013  10:59 AM
