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

c++ nothing more complex than arrays Write a function SwapArrayEnds() that swaps

ID: 3576481 • Letter: C

Question

c++ nothing more complex than arrays

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 <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 SORT_ARR_SIZE);

void SwapArrayEnds(int sortArray, int SORT_ARR_SIZE)
{
int temp
temp=sortArray[0];
sortArray[0]=sortArray[SORT_ARR_SIZE-1];
sortArray[SORT_ARR_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