Question: Solve in C++, please; Complete the reverseArray function which takes in a dynamically allocated array of integers and its size as arguments, and returns a

Solve in C++, please;

Complete the reverseArray function which takes in a dynamically allocated array of integers and its size as arguments, and returns a new dynamically allocated array that contains the elements of the input array in reverse order.

Note: The function should not modify the original array.

Example

Here is the function signature:

int* reverseArray(int* arr, int size); 

Here is an example of how the function should behave:

int* arr = new int[5]{1, 2, 3, 4, 5}; int* reversed = reverseArray(arr, 5); // reversed should contain {5, 4, 3, 2, 1}

This is the starter code:

#include

#include

using namespace std;

int* reverseArray(int* arr, int size) {

//add code below this line

//add code above this line

}

void printArray(int* arr, int size) {

for (int i = 0; i < size; i++) {

cout<

}

}

int main(int argc, char* argv[]) {

int size = stoi(argv[1]);

int* arr = new int[size];

for(int i=0; i

int num = stoi(argv[i + 2]);

arr[i] = num;

}

int *reverse = reverseArray(arr, size);

printArray(reverse, size);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!