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}. The array's size may differ from 4.
#include
using namespace std;
/* Your solution goes here */
int main() {
const int SORT_ARR_SIZE = 4;
int sortArray[SORT_ARR_SIZE];
int i = 0;
sortArray[0] = 10;
sortArray[1] = 20;
sortArray[2] = 30;
sortArray[3] = 40;
SwapArrayEnds(sortArray, SORT_ARR_SIZE);
for (i = 0; i < SORT_ARR_SIZE; ++i) {
cout << sortArray[i] << " ";
}
cout << endl;
return 0;
}.

Respuesta :

Answer:

Replace /* Your solution goes here*/ with the following

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

   int temp = sortArray[0];

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

   sortArray[lent-1] = temp;

}

Explanation:

This defines the function SwapArrayEnds with two parameter (the array and the array length)

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

This declares an integer variable "temp" and initializes it with the first element of the array

   int temp = sortArray[0];

The next two lines swap the last element with the first

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

   sortArray[lent-1] = temp;

}

See attachment for full program

Ver imagen MrRoyal