Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Array Meets Pointers (20 Points) The goal of this problem is to perform opera

ID: 3695191 • Letter: 3

Question

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 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.

Explanation / Answer

#include<iostream>
#include<cstdlib>

using namespace std;

// function to check whether given number is already there in array or not
bool check(int *arr, int key, int size);

int* arrayManipulator(int* arrayPointer, const int size);

void print(int *arrayPointer, const int size);

int main(){
  
   int N;
   cout<<"Please input N in range [1, 30]: ";
   cin>>N;
  
   // input validation
   while(N<1 || N>30){
       cout<<"Input out of range, please input N in range [1, 30]: ";
       cin>>N;
   }
  
   // allocating an array of size N
   int *randomnumbers = new int[N];
  
   // filling randomnumbers array with unique random numbers in range 1-100
   int i=0;
   for(i=0; i<N; ){
       int random = rand()%100 + 1; //getting random number in range 1-100
       // if current generated random number in alreay there in array then skip
       if(check(randomnumbers, random, i))
           continue;
       *(randomnumbers + i) = random;
       i++;
   }
  
   //printing randomnumbers
   print(randomnumbers, N);
  
   // getting reversedarray
   int *reversedarray = arrayManipulator(randomnumbers, N);
  
   // printing reversedarray
   print(reversedarray, N);
  
   //deleting both arrays
   delete [] randomnumbers;
   delete [] reversedarray;
  
  
   return 0;
}

bool check(int *arr, int key, int size){
   for(int i=0; i<size; i++){
       if(*(arr+i) == key)
           return true;
   }
   return false;
}

int* arrayManipulator(int* arrayPointer, const int size){
  
   // creating reversedarray
   int *reversedarray = new int[size];
  
   // copying elements from arraypointer
   for(int i=0; i<size; i++){
      
       *(reversedarray+i) = *(arrayPointer + i);
   }
  
   //now reversing reversedarray
   for(int i=0,j=size-1; i<j; i++,j--){
       int temp = *(reversedarray + i);
       *(reversedarray + i) = *(reversedarray + j);
       *(reversedarray + j) = temp;
   }
  
   //returning reversedarray
   return reversedarray;
}

void print(int *arrayPointer, const int size){
   for(int i=0; i<size; i++)
       cout<<*(arrayPointer + i)<<" ";
   cout<<endl;
}

/*

Sample run:

Please input N in range [1, 30]: 8
84 87 78 16 94 36 93 50
50 93 36 94 16 78 87 84

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote