7 Replies - 225 Views - Last Post: 26 October 2013 - 05:03 PM Rate Topic: -----

#1 tweetie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 13

Allocate memory for x amount of random number in C

Posted 26 October 2013 - 02:48 PM

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!
Is This A Good Question/Topic? 0
  • +

Replies To: Allocate memory for x amount of random number in C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,254
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2074
  • View blog
  • Posts: 3,151
  • Joined: 21-June 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 tweetie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 13

Re: Allocate memory for x amount of random number in C

Posted 26 October 2013 - 03:52 PM

View Postsepp2k, 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!
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,254
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#6 tweetie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-October 13

Re: Allocate memory for x amount of random number in C

Posted 26 October 2013 - 04:17 PM

View Postjimblumberg, 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


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).
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3978
  • View blog
  • Posts: 12,254
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5747
  • View blog
  • Posts: 12,555
  • Joined: 16-October 07

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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1