Palindrome Recursive Checker

I am trying to write a recursive function that checks if an array is palindrome (if it's reversed, still the same).

I hate two testers in main, first one should return 1 (true) but it returns 0 (false).

I need your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
using namespace std;

bool isPanlindrome(int a[], int n)
{
	int first = 0;
	int last = n - 1;

	if (n == 0 || n == 1)
	{
		return true;
	}
	else if (a[first] == a[last])
	{
		return isPanlindrome(a + 1, n - 1);
	}
	else
	{
		return false;
	}
}

int main()
{

	int x[] = { 1, 2, 3, 2, 1 };
	int y[] = { 1, 2, 3, 4, 5 };

	cout << "First array is: " << isPanlindrome(x, 5) << endl;
	cout << "Second array is: " << isPanlindrome(y, 5) << endl;

	system("Pause");
	return 0;
}
1
2
return isPanlindrome(a + 1, n - 1);
return isPanlindrome(a + 1, n - 2);
My suggestions:

1) With each recursion, you are only reducing the length by 1. You need to look at 2 less characters since the first and last are equal.
2) Or redesign your function to pass in the original array location and the first and last indices. That would be three parameters.
Thanks,

It's working perfectly now.
Topic archived. No new replies allowed.