Heap Allocate Assignment

For this assignment you will write a function which allocates an array on the heap using malloc() or calloc(), fills the array with random integers within a certain range, and returns a pointer to the array.

Code Organization

The assignment contains a Makefilemain.csequence.h, and sequence.cmain.c will use the function that you write. sequence.h contains the prototype for the function. You will implement the function in sequence.c. There is no need to edit any file except for sequence.c.

Running make will produce the executable random_sequence


The main() function, which is already written for you, grabs the command line arguments, seeds the random number generator, calls your function to get the array, prints out the values in the array, and then frees the array with free().

The program takes 3 command line arguments: the count of numbers in the sequence, the minimum random value in the sequence, and the maximum random value in the sequence. Here is an example run:

$ ./random_sequence 10 1 20
Here is the sequence:
7 20 1 19 7 9 2 3 18 13

The create_random_sequence() function

Here is the prototype for the function:

int *create_random_sequence(int count, int min, int max);

Implement this function in sequence.c. Create the array on the heap using malloc(). The array must have enough space to hold count integers.

Here is the prototype for malloc():

void *malloc(size_t size)

The size is the number of bytes to allocate. Since an int takes up more than 1 byte of space, you need to multiply count by sizeof(int) to get the appropriate number of bytes.

malloc() returns a generic void * pointer, which you can cast and assign to an int *. See the class code for an example.

Once you have created the array, fill it by placing a random number at each index of the array. Recall that we generated a random number from 1 to 6 in the Pig project like this:

rand() % 6 + 1

You will need to generalize this so that it can produce random numbers within any range.


Push your submission when you are done. Tests will be run using your function. For full credit the tests must pass and you must have proper style.


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:itcsdx