The goal of this problem is to perform operations on an array, without using the
ID: 3769787 • 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
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int* ArrayManipulator(int* arrayPoiner, const int size)
{
int ReversedArray[20];
int *rpointer;
rpointer= &ReversedArray[0];
int i;
for(i=0;i<size;i++)
{
ReversedArray[i]=*(arrayPoiner+i);
}
return rpointer;
}
void main()
{
int a[20],i,*rp;
for(i=0;i<20;i++)
{
a[i]=rand()%20;
}
cout<<"Original Aray is=";
for(i=0;i<20;i++)
{
cout<<a[i]<<" ";
}
rp=ArrayManipulator(&a[0],20);
cout<<"Reversed Array is =";
for(i=19;i>=0;i--)
{
cout<<*(rp+i)<<" ";
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.