An array of ints, arr, has been declared and initialized. Write the statements needed to reverse the elements in the array. So, if the elements were originally 5, 13, 4, 97 then after your code executes they would be 97, 4, 13, 5.

Respuesta :

Answer:

The sample output is below.

The code is attached.

Explanation:

Sample Output:

Original array is: 1 2 457 4 Reversed array is: 4754 2 1

Code to copy:

import java.io.*;

class Main

{

    /*Define the function revArray passing the array

      and its indices.*/

    static void revArray(int arr[], int start, int end)

    {

         /*Declare an integer variable to store the

           first index.*/

         int temp;

         //Check if array is empty.

         if (start >= end)

             return;

         //Store the first index value.

         temp = arr[start];

         //Swap the last index to first index.

         arr[start] = arr[end];

         //Store the temp value to last index.

         arr[end] = temp;

         /*Call the function recursively to swap

           rest of the values.*/

         revArray(arr, start+1, end-1);

    }  

    public static void main (String[] args)

    {

         //Declare and initialize array.

         int[] arr = {1,2,4,5,7,4};

         /*Declare an integer variable to store the

         * length of array.

         */

         int count=0;

         //Set the length of array to count.

         for(int i=0;i<arr.length;i++)

             count++;

         System.out.println("Original array is:");

         ///Print the original array.

         for(int i=0; i<count;i++)

         {

             System.out.print(arr[i]+" ");

         }  

         /*Call the function to reverse the values in

         * the array itself.

         */

         revArray(arr, 0, count-1);

         System.out.println("\nReversed array is:");

         ///Print the reversed array.

         for(int i=0; i<count;i++)

         {

             System.out.print(arr[i]+" ");

         }  

    }

}

Ver imagen femotinga
Ver imagen femotinga
Ver imagen femotinga
Ver imagen femotinga