A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward) Write a recursive, bool-valued function, isPalindrome, that accepts an integer -valued array , and the number of elements and returns whether the array is a palindrome.

An array is a palindrome if:

the array is empty (0 elements ) or contains only one element (which therefore is the same when reversed), or

the first and last elements of the array are the same, and the rest of the array (i.e., the second through next-to-last elements ) form a palindrome.

Respuesta :

Answer:

This code is written in MATLAB.

function [result] = isPalindrome(array,length)

if length == 0 || length == 1 %if the array is empty or contains only one element, the array is a palindrome

result = 1;

else

for i = 1:length/2 %check all the elements forward and backward until the middle is reached

if array(i) ~= array(end+1-i)

result = 0;

return

end

end

result = 1;

end

Explanation: Please read the comments in the code. In MATLAB, Boolean values are 1 or 0 instead of True or False.