Question: I am trying to write a recursive function in C# isPalindrome(int [] a). The only argument should be an array and the function should be
I am trying to write a recursive function in C# isPalindrome(int [] a). The only argument should be an array and the function should be recursive (no iteration). Can someone please modify the following code:
using System;
public class program { public static void Main() { int []arr1 = {1,4,5,7,8,8,7,5,4,1}; int []arr2 = {0,3,5,6,6,7,3,0,8,8}; Console.WriteLine("Consider array: "); printArray(arr1); Console.WriteLine("and array: "); printArray(arr2); bool result1 = isPalindrome(arr1, 10); bool result2 = isPalindrome(arr2, 10); if (result1 == true) Console.WriteLine("Array is a palindrome "); else Console.WriteLine("Array is not a palindrome "); if (result2 == true) Console.WriteLine("Array is a palindrome "); else Console.WriteLine("Array is not a palindrome "); } public static bool isPalindrome (int [] a, int size) { int[] newArray = new int[size-2];
if (size <= 1) return true; else { if (a[0] == a[size-1]) { for (int i=0; i < size-2; i++)
newArray[i] = a[i+1]; return isPalindrome(newArray, size-2); } else return false; } } public static void printArray(int[] array) { Console.WriteLine(string.Join(" ", array)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
