The goal of this problem is to perform operations on an array, without using the
ID: 3768576 • Letter: T
Question
The goal of this problem is to perform operations on an array, without using the “subscript operator”, but just with pointers.
Step 1:
Inside the main function, crate an array of 20 elements, and initialize it with unique random elements. 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.
* Further, inside ArrayManipulator create a new array “ReversedArray” and dynamically allocate memory to this array using new operator with length as “size”.
* Initialize “ReversedArray” with the elements pointed by the arrayPoiner.
* Now, reverse the elements of “ReversedArray” strictly using pointers, you cannot use
subscript operator.
* Return the pointer to “ReversedArray”
Step 3:
Inside the main function, receive the pointer returned in step 2. Print the elements of “ReversedArray” just using the received pointer.
Example:
The initial array elements are: 45, 25, 78, 32, 56, 74, 89, 22
Inside ArrayManipulator
The elements of ReversedArray: 45, 25, 78, 32, 56, 74, 89, 22
Inside main:
The elements of ReversedArray: 22, 89, 74, 56, 32, 78, 25, 45
Explanation / Answer
If you have any further queries, just get back to me.....
#include <iostream>
#include <array>
using namespace std;
void printArray(int Array[], const int size)
{
for(int i = 0; i < size; i++)
cout<<(Array[i])<<" ";
cout<<endl;
}
int* ArrayManipulator(int *arrayPointer, const int size)
{
// Step 2: Write a function “int* ArrayManipulator(int* arrayPoiner, const int size)” .
// This function receives a pointer to the array created in Step 1.
// Further, inside ArrayManipulator create a new array “ReversedArray” and dynamically allocate memory to this array
// using new operator with length as “size”.
// Initialize “ReversedArray” with the elements pointed by the arrayPoiner.
// Now, reverse the elements of “ReversedArray” strictly using pointers, you cannot use subscript operator.
// Return the pointer to “ReversedArray.”
int *ReversedArray = new int[size];
ReversedArray = arrayPointer;
for(int i = 0; i < size/2; i++)
{
int temp = *(ReversedArray+i);
*(ReversedArray+i) = *(ReversedArray + size - i - 1);
*(ReversedArray+size-i-1) = temp;
}
return ReversedArray;
}
int main()
{
//Step 1: Inside the main function, crate an array of 20 elements, and initialize it with unique random elements.
//Print the array.
int Array[20], *ReversedArray;
srand(time(NULL));
for(int i = 0; i < 20; i++)
Array[i] = rand() % 1000;
printArray(Array, 20);
ReversedArray = ArrayManipulator(Array, 20);
//Step 3: Inside the main function, receive the pointer returned in step 2.
//Print the elements of “ReversedArray” just using the received pointer.
printArray(ReversedArray, 20);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.