Sunday, March 12, 2017

Programming Challenge 9.1 - Array Allocator

/* Array Allocator - This program uses a function that dynamically
   allocates an array of integers. The function accepts an integer
   argument indicating the number of elements to allocate. The
   function returns a pointer to the array. */

#include "Utility.h"

/* Function prototypes */
int getNumber();
int *dynamicArray(int);
void displayNumbers(const int[], int);


int main()
{
   /* Points to rndNumbers */
   int *rndNumbers = nullptr;
  
   int numels = 0;

   /* Get the number of elements from the user */
   numels = getNumber();

   /* Gets an array filled with random numbers */
   rndNumbers = dynamicArray(numels);

   /* Display the numbers */
   displayNumbers(rndNumbers, numels);

   /* Free the memory */
   delete [] rndNumbers;
   rndNumbers = nullptr;  

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getNumber

   This function asks the user for the number of elements he
   or she wishes to be contained in rndNumbers.
   ********************************************************** */

int getNumber()
{
   int numels = 0;

   cout << "\n\t\tArray Allocator\n\n"
        << "\tPlease enter the number of elements you wish your array to\n"
        << "\tcontain. After accepting your choice, I will call a function\n"
        << "\tthat dynamically allocates an array to be filled with that\n"
        << "\tamount of random numbers.\n\n"
        << "\tNumber of elements: ";
   cin >> numels;

   return numels;
}

/* **********************************************************
   Definition: dynamicArray

   This function dynamically allocates an array of integers
   being filled with random numbers. The function returns a
   pointer to this array. numbers passed to this function as
   an argument contains the number of elements the user has
   requested.
   ********************************************************** */

int *dynamicArray(int numels)
{
   /* Array containing the randomly generated numbers */
   int *rndNumbers = nullptr;

   /* Returns a null pointer if numels is 0 or negative */
   if (numels <= 0)
   {
      return nullptr;
   }

   /* Dynamically allocate the array */
   rndNumbers = new int[numels];

   /* Seeding the random number generator */
   srand((unsigned int) time(NULL));

   /* Fill the array with random numbers */
   for (int index = 0; index < numels; index++)
   {
      rndNumbers[index] = rand();
   }
  
   /* Returns a pointer to the array */
   return rndNumbers;
}

/* **********************************************************
   Definition: displayNumbers

   This function accepts rndNumbers and numels containing the
   number of elements stored in the array as arguments. It
   displays the randomly generated numbers.
   ********************************************************** */

void displayNumbers(const int rndNumbers[], int numels)
{
   cout << "\n\tAnd here are the numbers I have generated for you:\n\n";

   for (int count = 0; count < numels; count++)
   {
      cout << "\t" << rndNumbers[count] << " \n";
   }
}

Example Output:





No comments:

Post a Comment