Ask Question

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

+2
Answers (1)
  1. 2 February, 11:15
    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 & 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