Ask Question
22 August, 10:37

6.9.1: Modify an array parameter. Write a function SwapArrayEnds () that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.

+4
Answers (1)
  1. 22 August, 11:21
    0
    public static void SwapArrayEnds (int[] arr) {

    int temp = arr[0];

    arr[0] = arr[arr. length-1];

    arr[arr. length-1] = temp;

    }

    Explanation:

    - Create a function called SwapArrayEnds that takes one parameter, an integer array

    - Inside the function, declare a temporary variable and set it to the first element of the array

    - Assign the value of the last element of the array to the first element, the first element in the array is swapped

    - Assign the value of the temporary variable to the last element, the last element in the array is swapped
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “6.9.1: Modify an array parameter. Write a function SwapArrayEnds () that swaps the first and last elements of the function's array ...” 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