55.2k views
1 vote
5.8.1: Modify an array parameter. 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}. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include using namespace std; /* Your solution goes here */ int main() { const int SORT_ARR_SIZE = 4; int sortArray[SORT_ARR_SIZE]; int i; int userNum; for (i = 0; i < SORT_ARR_SIZE; ++i) { cin >> userNum; sortArray[i] = userNum; } SwapArrayEnds(sortArray, SORT_ARR_SIZE); for (i = 0; i < SORT_ARR_SIZE; ++i) { cout << sortArray[i] << " "; } cout << endl; 1 test passed All tests passed Run

1 Answer

1 vote

Answer:

see explaination

Step-by-step explanation:

#include<stdio.h>

/* Your solution goes here */

//Impllementation of SwapArrayEnds method

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

//Declare tempVariable as integer type

int tempVariable;

if(SORT_ARR_SIZE > 1){

tempVariable = sortArray[0];

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

sortArray[SORT_ARR_SIZE-1] = tempVariable;

}

}

int main(void) {

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) {

printf("%d ", sortArray[i]);

}

printf("\\");

return 0;

}

Please go to attachment for the program screenshot and output

5.8.1: Modify an array parameter. Write a function SwapArrayEnds() that swaps the-example-1
5.8.1: Modify an array parameter. Write a function SwapArrayEnds() that swaps the-example-2
User Michael Cho
by
3.1k points