Ask Question
30 November, 15:05

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

+1
Answers (1)
  1. 30 November, 17:13
    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?
Get 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 & 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