C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

main()
{


int i, aSize;


int * randomNums;


time_t t;


double total = 0;
int biggest, smallest;
float average;


srand(time(&t));


printf("How many random numbers do you want in your array? ");
scanf(" %d", &aSize);


// Allocate an array of integers equal to the number of
// elements requested by the user


randomNums = (int ) malloc(aSize sizeof(int));


// Test to ensure that the array allocated properly


if (!randomNums)
{
printf("Random array allocation failed!\n");
exit(1);
}
// Loops through the array and assigns a random
// number between 1 and 500 to each element


for (i = 0; i < aSize; i++)
{
randomNums[i] = (rand() % 500) + 1;
}


// Initialize the biggest and smallest number
// for comparison's sake


biggest = 0;
smallest = 500;


// Loop through the now-filled array
// testing for the random numbers that
// are biggest, smallest, and adding all
// numbers together to calculate an average


for (i = 0; i < aSize; i++)
{
total += randomNums[i];
if (randomNums[i] > biggest)
{
biggest = randomNums[i];
}
if (randomNums[i] < smallest)

Free download pdf