In this assignment all functions must create and return a pointer to a dynamical
ID: 3723390 • Letter: I
Question
In this assignment all functions must create and return a pointer to a dynamically allocated array. Reverse Array Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Array Expander Write a function that accepts an int array and the array's size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Element Shifter Write a function that accepts an int array and the array's size 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 zero. Element 0 of the argument array should be copied to element1 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 pointer to the new array. Your main program should create an array to pass to each of the three functions above. Then using the pointer that is returned from each function print out the new array. You do not have to ask the user for the initial array, you can initialize it in the main function. Your functions must work for any size arrays.Explanation / Answer
#include<iostream>
#include<ctime>
using namespace std;
int* reverse(int arr[], int size);
int* expander(int arr[], int size);
int* elementShifter(int arr[], int size);
void print(int *arr, int size);
int main()
{
srand(time(NULL));
int num;
int arr[10];
int *newArr;
//assign random values to elements of an array
for (int i = 0; i < 10; i++)
{
num = rand() % 100 + 10;
arr[i] = num;
}
//print original arry
cout << "Orinal array: ";
print(arr, 10);
//call reverse and print
newArr = reverse(arr, 10);
cout << "Reverse of an array: ";
print(newArr, 10);
//expand the array
int *expandedArr;
expandedArr = expander(arr, 10);
//print array after expanding
//add some values to expande array
expandedArr[10] = rand() % 100 + 10;
expandedArr[11] = rand() % 100 + 10;
cout << "Print array after expanding array and adding elements to expanded array: ";
print(expandedArr, 12);
//now check elementshifter
int *elmShifter;
elmShifter = elementShifter(arr, 10);
//print the arra after element has beeen shifted
cout << "Print array after shifting an element : ";
print(elmShifter, 11);
}
//print array
void print(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int* reverse(int arr[], int size)
{
int *newArr = new int[size];
for (int i = size - 1,j=0; i >= 0; i--)
{
newArr[j++] = arr[i];
}
return newArr;
}
int* expander(int arr[], int size)
{
int *newArr;
newArr = new int[2 * size];
//copy elemets from arr ro newArr
for (int i = 0; i < size; i++)
{
newArr[i] = arr[i];
}
return newArr;
}
int* elementShifter(int arr[], int size)
{
int *newArr;
newArr = new int[size + 1];
newArr[0] = 0;
for (int i = 0, j = 1; i < size; i++)
{
newArr[j++] = arr[i];
}
return newArr;
}
/*output
Orinal array: 79 81 22 49 15 106 60 68 95 59
Reverse of an array: 59 95 68 60 106 15 49 22 81 79
Print array after expanding array and adding elements to expanded array: 79 81 22 49 15 106 60 68 95 59 26 90
Print array after shifting an element : 0 79 81 22 49 15 106 60 68 95 59
*/