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

in c++ Develop a program that dynamically creates an integer array of size n, wh

ID: 3587026 • Letter: I

Question


in c++

Develop a program that dynamically creates an integer array of size n, where n is an integer that is obtained after prompting the user for the desired array size and then storing his keyboard input in variable n The code shall then initialize the array with a random value for each element, drawn using the c standard li function rand , and hence the function rand () shall be called n times. Your code shall then ask the user to type either 'r', 'f or 's', and your program should react to the user's input by performing one of the tasks below and then repeat (i.e. asks the user for his input again): 1. If the user enters r', the program re-initializes the array using a new set of random values (as described in assignment 1). 2. If the user enters 'f, the program initializes/re-initializes the array using a Fibonacci sequence (as described in assignment 2) 3. If the user enters 's', the program swaps values of array elements as described in lab 2. 4. If the user enters 'o', the program rotates the array, i.c. clement i+ I takes the value in element i and element 0 (i.e. the first element) takes the value in element (n-1) (i.e. the last element). In doing so, you must ensure values are not overwritten, ie. use temporary storage values as necessary Each one of the tasks should be implemented using a separate function, and your code shall call the correct one according to the user's input. After performing one of the tasks (via calling one of the functions, the program displays the array content on the screen. If the user enters anything other than,'f','s, or "o., then the program exits, else the program repeats te asks the user for his input, and performs one of the three previously mentioned tasks again. The program continues to repeat till the user enters a character that is not T'. f or S Hints: Use the new keyword to dynamically allocate memory for the array -

Explanation / Answer

/* option "s" swap function is not described in the question. */

#include<iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;

void randomize(int data[], int size){
   for (int i = 0; i<size; i++)
       data[i] = rand();
   cout << "Array reinitialized with new set of random numbers ";
}

void fibonacci(int data[], int size){
     data[0] = 1;
     data[1] = 1;
     for (int i = 2; i<size; i++)
         data[i] = data[i-1] + data[i-2];
     cout << "Array reinitialized with fibonacci numbers ";
}

void rotate(int data[] , int size){

         int temp = data[size-1];
         for (int i = size-1; i>0; i--)
             data[i] = data[i-1];
         data[0] = temp;
         cout << "Array rotated ";
}

void disp(int data[], int size){
     for (int i = 0; i<size; i++)
         cout << data[i] << " ";
     cout << endl;
}


int main(){

int n;
string ch;
srand(time(NULL));
cout << "Enter number of elements: ";
cin >> n;
int *data = new int[n];

for (int i = 0; i<n; i++)
      data[i] = rand();

while (1){
   
     cout << "Enter r,f,s,o to continue or any other character to quit : ";
     cin >> ch;
     if (ch[0] == 'r'){
        randomize(data,n);
        disp(data,n);
     }
     if (ch[0] == 'f'){
        fibonacci(data,n);
        disp(data,n);
     }
     if (ch[0] == 'o'){
        rotate(data,n);
        disp(data,n);
     }
     if (ch[0] != 'r' && ch[0] != 'f' && ch[0] != 's' && ch[0] != 'o')
        break;
}

return 0;
}