Recursion Questions

I am stuck on a question about recursion


Two non-negative integers  x and y are equal  if either:
Both are 0, or
x-1 and y-1 are equal 
Write a bool -function named  equals  that recursively determines whether  its two int  parameters  are equal  and returns true  if they are and false  otherwise.

so far here is what i think it should be

1
2
3
4
5
6
7
8
9
bool equals (int x, int y)
{
if (x && y == 0)
	return true;
else if (x == y)
	return true;
else
return equals; false;
}


but myprogramminglab says its wrong can somebody help?
if (x && y == 0) is equivalent to: if ((x) && (y == 0)) which probably isn't what you meant to say.

1
2
3
4
5
6
7
bool equals(int x, int y)
{
    if ( x == 0 || y == 0 ) 
        return x == 0 && y == 0 ;

    return ?? ;
}
Last edited on
The code is x>=0 and y>=0 and x==y can be implement like this

1
2
3
4
inline bool equals (int x, int y)
{
return ((x==y) && (x>=0&&y>=0));
}



Recursion

1
2
3
4
5
6
bool equals (int x, int y)
{
if(x==0&&y==0) return 1;
if(x>0&&y>0) return equals (x-1, y-1);
return 0;
}
Last edited on
How does recursion come in to picture?
It is straight forward, I think you are missing some part of question.
Ya i was missing but now its Recursive.
Topic archived. No new replies allowed.