A dynamic array is an array that you allocate the memory for yourself with either malloc, calloc, or realloc:
Code:
int *array;
array = malloc ( 11 * sizeof *array );
if ( array != NULL ) {
/* Fill and work with the new array */
}
To create a random number which is passable for this program, include both time.h and stdlib.h. You want to use the srand function like so to seed the random number generator with every running of the program:
srand ( (unsigned)time ( NULL ) );
And then call rand and assign the returned value of it to each element of your array. For large numbers, simply use:
array[x] = rand();
Here's one solution to simply creating the array and filling it with random numbers with a decent range:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
int x;
int *array;
srand ( (unsigned)time ( NULL ) );
if ( ( array = malloc ( 11 * sizeof *array ) ) != NULL ) {
for ( x = 0; x < 11; x++ ) {
array[x] = ( rand() * 500 ) / RAND_MAX;
printf ( "%d\n", array[x] );
}
free ( array );
}
return 0;
}
-Prelude