Use recursion to check if 2 numbers are equal

I am in CodeLab for school, and have worked and worked on this problem without being able to figure it out. The question is:


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.


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

}


CodeLab says that the function is returning true whether the two parameters are equal or not. It says there is a logical error but I have not been able to find it, even after going over the function on paper.

You need to return the function-call, as such

1
2
3
4
5
6
bool equals (int x, int y) 
{
	if ( (x == 0) && (y == 0) ) return true;
	if ( (x < 0) || (y < 0) ) return false;
	return equals (x-1, y-1);
}
Topic archived. No new replies allowed.