# C++ Function with Array Parameters Problem: Write a function SwapArrayEnds() that swaps the first…

##### Home/Education/Expert Q&A/C++ Function with Array Parameters Problem: Write a function SwapArrayEnds() that swaps the first...

**Question:**

C++ Function with Array Parameters Problem:

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.

Here is what is given:

#include <iostream>

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;

}

**Click the button below to view answer!**

**Technifi Expert’s Answer:**

#include <iostream> using namespace std; void SwapArrayEnds(int sortArray[],int size){ int temp = sortArray[0]; sortArray[0] = sortArray[size-1]; sortArray[size-1] = temp; } 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; }

If you happen to run into some problem while following the steps, please make sure to let us know in the comment section below, and we’ll do our best to solve it. Apart from that, you can contact us on Facebook and Twitter, however we can’t guarantee a rapid reaction time over those platform

## 0 Comments