87.7k views
2 votes
Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter.

User WisZhou
by
4.8k points

1 Answer

6 votes

Answer:

Step-by-step explanation:

The function was written in Java. It takes in an int array returns the same array but with the first and last elements swapped. A test array was used in the image below to demonstrate the output which can be seen in the bottom of the image in red.

public static int[] SwapArrayEnds(int[] myArr) {

int temp = myArr[myArr.length-1];

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

myArr[0] = temp;

return myArr;

}

Write a function SwapArrayEnds() that swaps the first and last elements of the function-example-1
User Janderson
by
5.3k points