Respuesta :

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.

Ver imagen mahamnasir

Functions are collection of codes or instructions merged together to act as one.

The function, where comments are used to explain each line is as follows:

//This defines the function

void SwapArrayEnds(int sortArray[],int SORT_ARR_SIZE){  

//This increments the array element at index 0

       sortArray[0] += sortArray[SORT_ARR_SIZE-1];

//This replaces the last element with the first

       sortArray[SORT_ARR_SIZE-1]= sortArray[0]- sortArray[SORT_ARR_SIZE-1];

//This replaces the first element with the last

       sortArray[0]= sortArray[0]- sortArray[SORT_ARR_SIZE-1];

   

}

At the end of the function, the first and the last elements of the array are swapped.

Read more about similar programs at:

https://brainly.com/question/15582117