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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started