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

Develop a program that dynamically creates an integer array of size n, where n i

ID: 3873919 • Letter: D

Question

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 library 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 After performing one of the three tasks, the program displays the array content on the screen. If the user enters anything other than 'r', 'f, or 's', then the program exits, else the program repeats i.e. 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 'r', 'f or 's" Hints: Use the new keyword to dynamically allocate memory for the array Before your program ends, you need to use the delete keyword to de-allocate the memory . . To swap array values, you create a loop that starts by swapping values in the outermost elements, moving A[0] and A[19] swap values, then A[1] and inwards towards the middle with every iteration (e.g. if n=20, A[18] swap, until A[9] and A[10] swap). To generate a random number you shall include the header file stdlib .h (#include use the function rand ) (e.g. myvar-rand ();) ) and · To initialize an arra y using a Fibonacci sequence, the first two elements need to be set to a value of 1, and every other element thereafter is the sum of the previous two elements, thus Value of array element 0 should be l Value of array element 1 should be l Value of array element 2 should be 1+1 = 2 Value of array element 3 should be 2+1 = 3 Value of array element 4 should be 3+2 = 5 Value of array element 5 should 5+3 8 And so on till element (n-1)

Explanation / Answer

Program in C:

#include<stdio.h> //standard libraries

#include<stdlib.h>
void fib(int [],int); //function declaration to calculate and assign fibonacci numbers to array
void swap(int[],int); //function to swap array elements
void call() //Function to perform required functionalities
{
int n,i; //variable declaration
char ch;
printf("Enter array size");
scanf("%d",&n);
int* array = malloc(n*sizeof(*array)); //dynamic array declaration
for(i=0;i<n;i++) //loop to assign random numbers to array elements
{
array[i]=rand();
}
printf("Enter your choice"); //enter the desired choice
scanf("%c",&ch);
if(ch=='r') //if choice is 'r'

for(i=0;i<n;i++) // assign random numbers to array elements
{
array[i]=rand();
}

else if(ch=='f') // if choice is 'f'

fib(array,n); // call fibonacci function to generate and assign fibonacci numbers to array elements

else if(ch=='s') // if choice is 's'

swap(array,n); //swap array elements

else if(ch!='r' && ch!='s' && ch!='f') // if choice is not 'r','f' or 's',exit the program

exit(-1);

else
free(array); // deallocate array memory size
call(); //call function again

}
void fib(int array[],int n) //function to calculate fibonacci elements and assign it to array
{
int fr=1,s=1,next,c;
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
else
{
next=fr+s;
fr=s;
s=next;
array[c]=next;
}
printf("%d",array[c]);


}
}
void swap(int array[],int n) //function to swap array elements
{
int j=n/2;
int k=0,i;
for(i=n-1;i>=j;i--)
{
array[k]=array[i];
k++;
}
printf("Modified array is:");
for(i=0;i<n;i++)
{
printf("%d",array[i]);
}

}

int main() //main function to call the functio to perform desired functionalities
{
call();
}