# I got help from someone for this malloc problem but its not compiling.

 Paul Kim · February 7, 2016 Problem 2:Write a program 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 49For 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#include #include #include void ArrayRotate(int arr[], int n,char dir, int times) // this function rotates the array arr in 'dir' direction by 'times' times{ int i=0; int *temp = malloc (sizeof (int) * n); // creates a temp array if(dir=='R') // if the direction is R { for(i=0;i

## Replies

 kuldeep kanzariya · February 17, 2016 #include #include #include void ArrayRotate(int arr[], int n,char dir, int times) // this function rotates the array arr in 'dir' direction by 'times' times{        int i=0;        int *temp = malloc (sizeof (int) * n); // creates a temp array        if(dir=='R') // if the direction is R        {                for(i=0;iint main(void) { int *non_VLA = malloc(rand() * sizeof *non_VLA); // OPTION A: malloc; not nice because: if (non_VLA == NULL) { exit(EXIT_FAILURE); } // 1. YOU MUST NOT USE non_VLA IF IT'S NULL! free(non_VLA); // 2. YOU MUST free(non_VLA) AT SOME POINT! /* ----- */ int VLA[rand()]; // OPTION B: VLAs; nice because: // 1. VLA can never be NULL, so you don't need to check that. // 2. VLA will be automatically freed when the function returns.}``
• 1

## C

128,097 followers

One of the most popular languages of all time.