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

*****C Programming only ***** Write a program, using C, that allows users to inp

ID: 3774226 • Letter: #

Question

*****C Programming only *****

Write a program, using C, that allows users to input an integer for the size of an array. ( use malloc() ) Randomly generate an integer for each element of the array. Next, create function to rotate the array . Rotation of the array means that each element is shifted right or left by one index, and the last element of the array is also moved to the first place.

For example:

              Enter the number of slots needs in the array: 8

              This is element of your array: 91 57 18 96 16 49 31 83

              Which direction to shift R/L : R

              How many times: 2

              This is element of your array: 31 83 91 57 18 96 16 49

For example:

              Enter the number of slots needs in the array: 3

              This is element of your array: 31 83 91

              Which direction to shift R/L : L

              How many times: 2

              This is element of your array: 91 31 83

Explanation / Answer

Answer :

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

void ArrayRotate(int arr[], int n,char dir, int times) // It rotates the array arr in 'dir' direction by 'times' times

{

        int i=0;

        int *temp = malloc (sizeof (int) * n); // create a temp array

        if(dir=='R') // direction is R

        {

                for(i=0;i<n;i++)

                {

                        temp[(i+times)%n]=arr;

                }

        }

        else

        {

                for(i=0;i<n;i++)

                {

                        int newIndex=i-times;

                        if(i-times<0)

                                newIndex=newIndex+n;

                          temp[(newIndex)%n]=arr;

                }

        }

        for (i=0; i<n; i++) // to copy back the elements of 'temp' array to 'arr' array

        {

                arr=temp;

        }

}

int main(void)

{

        int n;

        printf("Enter the number of slots needs in the array:");

        scanf("%d",&n);

        int *arr = malloc (sizeof (int) * n); //n is the length of the array

        srand(time(NULL));

       int i;

        time_t t;

        srand(time(&t));

        for (i=0; i<n; i++)

        {

                arr =rand()%100; // intializing the elements using rand function which generates a random integer

        }

        printf("This is element of your array: ");

        for (i=0; i<n; i++) // prints the initial elements of the 'arr' array

        {

                printf("%d ",arr);

        }

        char dir;

        int times;

        printf(" Which direction to shift R/L : ");

        scanf(" %c",&dir);// saves the direction in variable c

        printf("How many times: ");

        scanf("%d",&times);// saves the no of times to be rotated in 'times'

        ArrayRotate(arr, n,dir,times);

        printf("This is element of your array: ");

        for (i=0; i<n; i++)// print the final arr

        {

                printf("%d ",arr);

     }

        return 0;

}

Output :

Enter the number of slots needs in the array: 8

This is element of your array: 91 57 18 96 16 49 31 83

Which direction to shift R/L : R

How many times: 2

This is element of your array: 31 83 91 57 18 96 16 49

....................................