Ask Question

Write a function that, given an array of integers and its size, reverses the elements in the array. For example, if the original array was [10, 15, 5, 25, 0] the new array should be [0, 25, 5, 15, 10].

+3
Answers (1)
  1. 14 June, 11:05
    0
    import java. util. Arrays;

    public class num11 {

    public static void main (String[] args)

    {

    int [] originalArray = {10, 15, 5, 25, 0};

    System. out. println ("Original Arrays is: "+Arrays. toString (originalArray));

    reverseArray (originalArray, originalArray. length);

    }

    //Method reverseArray

    public static void reverseArray (int a[], int n)

    {

    int[] newArray = new int[n];

    int m = n;

    for (int i = 0; i < n; i++) {

    newArray[m - 1] = a[i];

    m--;

    }

    #Printing the Reverse

    System. out. println ("The Reversed Array is "+Arrays. toString (newArray));

    }

    }

    Explanation:

    Using Java programming language:

    The method is created to accept two parameters (an array and its size)

    Create a new array within the method int[] newArray = new int[n];

    Using a for loop iterate over the original array and place each of its elements in the new array from the last index, this places the elements reversively.

    Use Java's toString () to display the revesered Array
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function that, given an array of integers and its size, reverses the elements in the array. For example, if the original array was ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers