1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Bingel [31]
4 years ago
6

A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are sa

me when scanned forward or backward) Write a recursive, boolean-valued method, isPalindrome, that accepts an integer-valued array, and a pair of integers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome. The function returns whether that portion of 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.

Computers and Technology
1 answer:
tia_tia [17]4 years ago
8 0

Answer:

Here is the JAVA program:

class Main {    

static boolean isPalindrome(int array[], int starting, int ending) {  /*a boolean method that takes an integer-valued array, and a pair of integers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome */

   if (starting >= ending) {  //base case

       return true;     }  //returns true

   if (array[starting] == array[ending]) {  //recursive case

       return isPalindrome(array, starting + 1, ending - 1);     }  //calls isPalindrome recursively to find out palindrome

   else {  

       return false;    }  }  //returns false if array is not palindrome

 

   public static void main (String[] args) {  //start of main function

   int array[] = { 1,2,3,2,1};  //creates an array

   int size = array.length;   //computes the size of array

 System.out.print(isPalindrome(array, 0, size - 1));    } } //calls method to test array for being  a palindrome

Explanation:

The program works as follows:

array = { 1,2,3,2,1}

starting = 0

ending = size-1 = 5-1 = 4

if (starting >= ending) condition evaluates to false because starting i.e. 0 is not greater or equal to ending i.e. 4. So the program moves to the next if part

if (array[starting] == array[ending])

This becomes:

if (array[0] == array[4])

The element at 0th index is the first element of the array i.e. 1 and the element at 4th index of array is the last element of array i.e. 1. So both these elements are equal so the following statement executes:

return isPalindrome(array, starting + 1, ending - 1);

Now the starting and ending become:

starting+1 = 0+1 = 1

ending-1 = 3

if (starting >= ending) base condition evaluates to false because starting  is not greater or equal to ending. So the program moves to the next if part

if (array[starting] == array[ending])

This becomes:

if (array[1] == array[3])

The element at 1st index is 2 and the element at 3rd index of array is 2. So both these elements are equal so the following statement executes:

return isPalindrome(array, starting + 1, ending - 1);

Now the starting and ending become:

starting+1 = 1+1 = 2

ending-1 = 2

if (starting >= ending) base condition evaluates to true because starting  is  equal to ending. So the following statement executes:

       return true;    

This means the function returns true and this shows that array is a palindrome.

The screenshot of the program and its output is attached.

You might be interested in
You have opened the properties dialog box for an installed printer on your computer system. on what tab will you find informatio
ollegr [7]
You can find it on the general tab.
4 0
3 years ago
Why is E in DELL tilted?​
BigorU [14]

Answer:

The letter “E” in Dell logo is slightly turned towards D. It represents that the company founder Michael Dell's wish to “turn the world on its ear.” Others believe that the slanted 'E' represents a floppy disk.

Explanation:

hope it's help u

6 0
3 years ago
Read 2 more answers
Which 2 tools are useful to remote employees and coworkers.
dsp73
Two big useful tools are one, File Storage tools, and two, Project Management Tools.
5 0
3 years ago
I need to know the answer to this question
RoseWind [281]

It's D because all of this are TRUE.

3 0
3 years ago
Name three technologies that begin with the same letter of the alphabet
qaws [65]
Active-Matrix 
Applet
ATM
8 0
4 years ago
Read 2 more answers
Other questions:
  • What does ACCU stand for?
    13·2 answers
  • What protocol allows us to use a domain name like .com instead of an ip address?
    6·1 answer
  • Explain why it might be more appropriate to declare an attribute that contains only digits as a character data type instead of a
    11·1 answer
  • Fill in the blank
    15·1 answer
  • Which of the following statements is NOT true regarding the Security Configuration and Analysis (SCA) tool?
    12·1 answer
  • What is the function of ctrl+f​
    15·2 answers
  • Which statement about digital certificates is​ FALSE? A. The CA verifies a digital certificate​ user's identity online. B. Digit
    7·1 answer
  • You can not give an exact size for a height for column width true or false
    14·1 answer
  • How many 60 KB jpeg files can be stored on a 2 MB folder in your hard drive?​
    7·1 answer
  • Which of the following factors is least likely to result in delivery delays?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!