Hi! I would truly appreciated if anyone could provide me an sample of how to allocate memory for x amount of random integer. I'm using rand() to get the random integer, but I'm not sure how to use malloc() with rand(). Thanks!
7 Replies - 922 Views - Last Post: 26 October 2013 - 05:03 PM
#1
Allocate memory for x amount of random number in C
Posted 26 October 2013 - 02:48 PM
Replies To: Allocate memory for x amount of random number in C
#2
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 03:29 PM
Quote
but I'm not sure how to use malloc() with rand().
Do you know how to use malloc without rand()?
You need to provide some code showing what you've tried.
Jim
#3
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 03:34 PM
You allocate it just like you would allocate memory for x non-random integers. The fact that you're planning to rand does not affect the call to malloc at all.
#4
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 03:52 PM
sepp2k, on 26 October 2013 - 03:34 PM, said:
You allocate it just like you would allocate memory for x non-random integers. The fact that you're planning to rand does not affect the call to malloc at all.
typedef struct {
int *arr; //the dynamic array
int len; //the size of the dynamic array
}Array;
Array *createArray (int len) {
int i;
srand(time(NULL));
for (i =0; i < len; i++) {
int n = rand() % 21; //random numbers from 0-20
}
arr = (int*)malloc(sizeof(int) * len); //arr - a pointer in the Array struct
}
Am I on the right track?
I just started learning C & dynamic array, so I truly appreciate any feedback you could give me. Thank you for all your help!
#5
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 04:01 PM
I suggest you learn one new thing at a time. First figure out how malloc works then figure out how rand works. At this time you're not even using anything from that loop so get rid of the loop and the srand(), rand() calls. You're also not using that structure so maybe you need to pass an instance of the structure into your function as well.
And in a C program you shouldn't be casting the return value from the malloc call.
Jim
And in a C program you shouldn't be casting the return value from the malloc call.
Jim
#6
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 04:17 PM
jimblumberg, on 26 October 2013 - 04:01 PM, said:
I suggest you learn one new thing at a time. First figure out how malloc works then figure out how rand works. At this time you're not even using anything from that loop so get rid of the loop and the srand(), rand() calls. You're also not using that structure so maybe you need to pass an instance of the structure into your function as well.
And in a C program you shouldn't be casting the return value from the malloc call.
Jim
And in a C program you shouldn't be casting the return value from the malloc call.
Jim
I'm using srand() and rand() because I need to generate some random integers, so my confusion is how to allocate memory for these integers. The struct that I have will be used to create a dynamic array that is in a separate Driver file (the struct is in a header file, and the function is in a function file).
#7
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 04:21 PM
Quote
The struct that I have will be used to create a dynamic array that is in a separate Driver file (the struct is in a header file, and the function is in a function file).
Post your actual code not some random snippets. The code you have posted doesn't make any sense, you're not using the structure, the random numbers or really anything else properly.
Jim
#8
Re: Allocate memory for x amount of random number in C
Posted 26 October 2013 - 05:03 PM
Array *createArray (int len) {
int i;
srand(time(NULL)); // not here
for (i =0; i < len; i++) {
// you are loading n len times. why?
int n = rand() % 21; //random numbers from 0-20
}
// when did you define arr?
arr = (int*)malloc(sizeof(int) * len); //arr - a pointer in the Array struct
return ???
Break it up. Use more functions.
Array *createArray (int len); // just creates an array of len size, non random going on here
void randomFill(Array *); // here's where you load it
void showArray (Array *); // this could be handy
void deleteArray (Array *);
int main() {
Array *a;
srand(time(NULL)); // always do this in main, only do it once
a = createArray (5);
randomFill(a);
showArray(a);
deleteArray(a);
return 0;
}
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|