Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Function with Array Parameters Problem: Write a function SwapArrayEnds() tha

ID: 3683336 • Letter: C

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;
}

Explanation / 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote