Ask Question
10 September, 08:15

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].

+1
Answers (1)
  1. 10 September, 11:04
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers