Ask Question
8 July, 13:11

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

+4
Answers (1)
  1. 8 July, 16:20
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers