Answer:
Python method swapArrayEnds()
def swapArrayEnds(array):
size = len(array)
temp = array[0]
array[0] = array[size - 1]
array[size - 1] = temp
return array
Explanation:
You can call the method in main program to swap the first and last elements of sortArray as following:
def swapArrayEnds(array):
size = len(array)
temp = array[0]
array[0] = array[size - 1]
array[size - 1] = temp
return array
sortArray = [10, 20, 30, 40]
print(swapArrayEnds(sortArray))
This function works as following:
size stores the length of the array.
temp is a temporary variable that holds the first element of the array
The it swaps the first element of the array with the last element using this statement: array[0] = array[size - 1] where size-1 contains the last element.
It finally stores the last element of the array in temp and returns the array after this swapping is done.
Then in the main program an array sortArray is given the values 10,20,30,40 and then the method swapArrayEnds() is called to swap the first and last elements of sortArray.
The complete program with the output is attached in a screenshot.