Question: Write a function called elementShifter that accepts the arrays size and an int array as arguments. The function should create a new array that is

Write a function called elementShifter that accepts the arrays size and an int array as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a unique pointer to the new array.

Write only the function, not the entire program.

Here is the program (less your function, of course): /*

Purpose: This function accepts and integer array and the size of the array. It creates a dynamic array one element longer and shifts the original array by one from the left and sets element 0 to 0. Returns a unique pointer to the new dynamic array.

*/

#include

#include

using std::cout; using std::cin; using std::endl; using std::unique_ptr;

int main()

// Define a unique_ptr smart pointer, pointing // to a dynamically allocated array of ints. // unique_ptr is a small, fast, moveonly smart // pointer for managing resources with

// exclusiveownership semantics unique_ptr newCopy(new int[SIZE]);

// Passing the smart pointer as an argument // The get method will produce a raw pointer // and so actually creates a temporary copy // that is, moves the smart pointer to a local // Why do this? B/C 2 instances of a unique // pointer cannot manage the same (memory) showArray(newCopy.get(), SIZE + 1);

// All this so we could reuse the same // function for displaying

return 0; }

// precond: valid array of integers, and the size as an int // postcond: printed array contents // Display the contents of the array.

// YOUR CODE GOES HERE (elementShifter function)

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!