xor function

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 894 Views - Last Post: 24 March 2013 - 05:02 PM Rate Topic: -----

#1 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

xor function

Posted 21 March 2013 - 12:23 AM

hello guys,

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[length-1] place and double it by 2 each time, but I have no idiea how do implement it....


Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: xor function

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: xor function

Posted 21 March 2013 - 03:12 AM

Sounds like recursion might be relevant here. Have you been taught about that?
Was This Post Helpful? 0
  • +
  • -

#3 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 21 March 2013 - 03:50 AM

i wrote that on the second line of my question...

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...
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: xor function

Posted 21 March 2013 - 05:44 AM

Out of curiousity, the prototype says that the function returns an int. What is that return value supposed to be?

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.
Was This Post Helpful? 0
  • +
  • -

#5 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 21 March 2013 - 07:08 AM

well, i wrote it, but still, something is missing:

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
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: xor function

Posted 21 March 2013 - 07:31 AM

Hmm, well,

the ^ symbol is the exclusive OR function in c/c++

that might help you ..


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#7 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 21 March 2013 - 10:01 AM

can you explain yourself please?
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: xor function

Posted 21 March 2013 - 02:04 PM

If you were writing a function to sum an array, how would you sum the values?


int sum(int numbers[], int length)
{
  if(length == 0)
   return 0;

  
}


Was This Post Helpful? 0
  • +
  • -

#9 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 22 March 2013 - 03:34 AM

View Post#define, on 21 March 2013 - 02:04 PM, said:

If you were writing a function to sum an array, how would you sum the values?


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,length-1,0);

how does it helps me now?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: xor function

Posted 22 March 2013 - 05:14 AM

The point that #define was trying to make was that if you wanted to add:
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 N-1 items plus the N-th item.

This post has been edited by Skydiver: 22 March 2013 - 05:14 AM

Was This Post Helpful? 0
  • +
  • -

#11 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 22 March 2013 - 07:50 AM

ok, but i still dont get what is wrong in my code?
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: xor function

Posted 22 March 2013 - 08:35 AM

Hmm well,

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.
Was This Post Helpful? 1
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: xor function

Posted 22 March 2013 - 08:47 AM

View Postchenfire, 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,length-1,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[length-1] + sumarry(arr,length-1);
	}
	
	return thesum;
}


Was This Post Helpful? 0
  • +
  • -

#14 chenfire  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 13-March 13

Re: xor function

Posted 22 March 2013 - 09:18 AM

View PostCTphpnwb, on 22 March 2013 - 08:47 AM, said:

View Postchenfire, 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,length-1,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[length-1] + sumarry(arr,length-1);
	}
	
	return thesum;
}


you didn't look at evryting I wrote right? :P
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: xor function

Posted 22 March 2013 - 10:58 AM

For summing the array and returning the values using the return statement.

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

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2