This is in C++ 3. Array Meets Pointers ( 20 Points) The goal of this problem is
ID: 3694677 • Letter: T
Question
This is in C++
3. Array Meets Pointers ( 20 Points) The goal of this problem is to perform operations on an array, without using the "subscript operator", but only with pointers. Step 1: Inside the main function, ask user to input a positive integer N less than or equal to 30. Input validation is necessary here. Create an array randomnums of N integer elements using new operator, and initialize it with unique random integers. Print the array. Step 2: Write a function int* arrayManipulator(int* arrayPoiner, const int size). This function receives a pointer to the array created in Step 1 Inside arrayManipulator create a new array reversedarray and dynamically allocate memory for this array using new operator with length as size. Initialize reversedarray with the elements pointed by arrayPoi ner. Now, reverse the elements of reversedarray strictly using pointers, you cannot use subscript operator Return the pointer to reversedarray · Step 3: nside the main function, receive the pointer returned in step 2. Print the elements of reversedarray just using the received pointer. Step 4: Deallocate memory used by randomnum and reversedarray before return 0 in main. Explicitly print the statement you used to deallocate memory.Explanation / Answer
/** C++ code using subscript operator with pointers to reverse the array **/
#include <iostream>
#include <stdlib.h>
using namespace std;
int* ArrayManipulator(int* arrayPoiner, const int size)
{
cout << " Inside ArrayManipulator: ";
cout << "The elemenmts of ReversedArray: ";
int *ReversedArray = new int[size];
for (int i = 0; i < size; ++i)
{
*(ReversedArray+i) = *(arrayPoiner+ i);
cout << *(ReversedArray+i) << ", ";
}
for (int i = 0; i < size; ++i)
{
*(ReversedArray+i) = *(arrayPoiner+ size - i -1);
}
cout << endl;
return ReversedArray;
}
int main()
{
int size,num;
cout << "Please input N in range[1,30]: ";
cin >> size;
while(size > 30 || size < 1)
{
cout << "Input out of range, Please input N in range[1,30]: ";
cin >> size;
}
int *randomnums = new int[size];
cout <<" The initial array elemenmts of randomnums are: "<< endl;
for (int i = 0; i < size; ++i)
{
num = rand() % 1000;
*(randomnums+i) = num;
cout << *(randomnums+i) << ", ";
}
cout << endl;
int *ReversedArray = new int[size];
ReversedArray = ArrayManipulator(randomnums,size);
cout << " Inside main: ";
cout << "The elemenmts of ReversedArray: ";
for (int i = 0; i < size; ++i)
{
cout << *(ReversedArray+i) << ", ";
}
cout << endl;
delete[] ReversedArray;
delete[] randomnums;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.