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

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


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

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<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++) // loop 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); // n saves the number of elements in the array
int *arr = malloc (sizeof (int) * n); /* n is the length of the array */
srand(time(NULL));
;
int i;

for (i=0; i<n; i++)
{
arr =rand()%100; // intializing the elements of arr with the help of 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("\nWhich 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);// calls the function Arrayrotate to rotate arr

printf("This is element of your array: \n");
for (i=0; i<n; i++)// prints the final arr
{
printf("%d ",arr);

}


return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 kuldeep kanzariya · February 17, 2016
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
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<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++) // loop 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); // n saves the number of elements in the array
        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 of arr with the help of 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("\nWhich 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);// calls the function Arrayrotate to rotate arr

   printf("This is element of your array: \n");
        for (i=0; i<n; i++)// prints the final arr
        {
                printf("%d ",arr);

        }
        return 0;
}
0 Perverse Programmer · February 22, 2016
I'm in love with both of your programs, because they suffer from memory leaks! That's unusual, isn't it? Most people hate memory leaks...

Have you ever used valgrind? It's easy (takes 30 seconds to learn how to use) and immensely useful (tells you about leaks and numerous memory problems, even the difficult to debug ones).

If you use OS X you should use XCode instead of valgrind: https://developer.apple.com/library/ios/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html

If you use Windows, set up Linux inside a VM so you can use valgrind. I can recommend VMWare Player because it's free for personal use.

Rest assure, I've done this in the cleanest and least perverse (a.k.a. most boring) way, and that involves not using malloc and not leaking memory. Can you explain why you can't use variable length arrays, instead? For example:

#include <stdlib.h>

int 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
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator