Question
For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use
For this program, we are going to expand a standard array by dynamically allocating a new
one with a larger footprint. The program will use a function that accepts an integer array and
the size of the integer array, and then increases it to hold one more item. It shifts all of the
original arrays values over by one into the new dynamic array, and sets the first element to
the value of zero. When the creation and move are complete, this function returns a (smart)
unique pointer pointing to the new dynamic array back to the calling program (main).
The program uses the declarations for the array and pointer (in main) as follows:
const int SIZE = 5;
int myNumbers[SIZE] = {18, 27, 3, 14, 95};
// Define a unique_ptr smart pointer, pointing
// to a dynamically allocated array of integers.
// A unique_ptr is a small, fast, move-only smart
// pointer usually used for managing resources
// with exclusive ownership semantics
unique_ptr
This program should display 0 18 27 3 14 95 after the element shift function does its work. It
does not work and, as you will see, it has a compilation error that we must fix.
he Functions:
1)
The first function will dynamically allocate an array to hold the additional element. It
will then copy each element into the new dynamic array. It will return the smart
pointer. NOTE: The function creates the dynamic array using the unique pointer.
2)
A function to display the elements in the array (called before and after).
This is the assignment here we need to make this showArray function work for the old (raw pointer) and new (smart pointer) array.
You need to have the showArray function also use the smart pointer, and print the resized array.
FYI this is completely doable - you can get by with just using one showArray function.
The task is to figure out how to make the second call to showArray work.
You will only be changing one line (51) in the
program.
If you can figure out what to change, and change just the function call, and then
have it render the correct output. Here is the code, use this for the assignment.
#include
#include
using std::cout;
using std::cin;
using std::endl;
using std::unique_ptr;
unique_ptr
void showArray(int*, int);
int main()
{
const int SIZE = 5;
int myNumbers[SIZE] = {18, 27, 3, 14, 95};
// Define a unique_ptr smart pointer, pointing
// to a dynamically allocated array of ints.
// unique_ptr is a small, fast, move-only smart
// pointer for managing resources with
// exclusive-ownership semantics
unique_ptr
showArray(myNumbers, SIZE);
newCopy = elementShifter(SIZE, myNumbers);
// Passing the smart pointer as an argument
showArray(newCopy, SIZE + 1);
// THIS (ABOVE) IS THE ONLY STATEMENT THAT YOU MAY CHANGE
// We are required to reuse the same
// function for displaying
return 0;
}
// pre-cond: array of ints to expand and the size as an int
// post-cond: pointer to the new allocated array
// Call the element shifter function to make a copy of
// the array, with the elements shifted one
// position toward the end if the array.
unique_ptr
{
int newSize = arraySize + 1; // The new size
unique_ptr
shiftedArray[0] = 0;
for (auto index = 0; index < arraySize; ++index)
{
shiftedArray[index + 1] = *(arrayOfInts + index);
}
return shiftedArray;
}
// pre-cond: valid array of integers, and the size as an int
// post-cond: printed array contents
// Display the contents of the array.
void showArray(int *numbers, int size)
{
cout << "The contents of the array are: ";
for (auto index = 0; index < size; ++index)
{
cout << *(numbers + index) << " ";
}
cout << endl << endl;
}
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