Sunday, April 2, 2017

Programming Challenge 9.11 - Array Expander

/* Array Expander - This program contains a function that accepts an
   int array and the array's size as arguments. The function creates
   a new array that is twice the size of the argument array. The
   function copies the contents of the argument array to the new array
   and initialize the unused elements of the second array with 0. The
   function returns a pointer to the new array. */

#include "Utility.h"

/* Function prototypes */
void getNumbers(int *, int);
int *expandArray(const int *, int &, int);
void displayOriginal(const int *, const int);
void displayCopy(const int *, const int);
void freeMem(int *, int*);

int main()
{
   int *numbers = nullptr;
   int *arrExpander = nullptr;

   int numels = 0,
       expSize = 0;

   /* Get the initial number of elements the array should hold */
   cout << "\n\tARRAY EXPANDER\n\n"
        << "\tHow many numbers should your array contain? ";
   cin >> numels;

   /* Allocates a new array to hold the numbers */
   numbers = new int[numels]();

   /* Creates a set of random numbers to fill the numbers array */
   getNumbers(numbers, numels);

   /* Displays the original array and its contents */
   displayOriginal(numbers, numels);

   cout << "\n\n\tYour array will now be copied and expanded ...\n\n";

   /* Expands the array to hold twice the number of elements as its
      original */
   arrExpander = expandArray(numbers, expSize, numels);

   /* Displays the copy of the array */
   displayCopy(arrExpander, expSize);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getNumbers

   This function accepts numbers and numels as arguments. It
   fills numbers with (pseudo)random numbers in the range of
   1 through 200.
   ********************************************************** */

void getNumbers(int *numbers, int numels)
{
   const int MIN_NUM = 1,
             MAX_NUM = 200;

   srand((unsigned int) time(NULL));

   for (int index = 0; index < numels; index++)
   {
      *(numbers + index) = (rand() % (MAX_NUM - MIN_NUM + 1) + MIN_NUM);
   }
}

/* **********************************************************
   Definition: displayOriginal

   This function accepts numbers and numels as arguments. It
   displays the original array.
   ********************************************************** */

void displayOriginal(const int *numbers, const int numels)
{
   cout << "\n\tThis is how your original array looks like:\n\n";

   for (int index = 0; index < numels; index++)
   {
      cout << "\t" << *(numbers + index) << "\n";
   }

   cout << "\n\tIt holds " << numels << " elements.\n";
}


/* **********************************************************
   Definition: displayCopy

   This function accepts expanded and numels as arguments. It
   displays the content of the argument array's copy.
   ********************************************************** */

void displayCopy(const int *expanded, const int expSize)
{
   cout << "\n\tThis is how your array's copy looks like:\n\n";

   for (int index = 0; index < expSize; index++)
   {
      cout << "\t" << *(expanded + index) << "\n";
   }

   cout << "\n\tIt can now hold " << expSize << " elements!\n";
}

/* **********************************************************
   Definition: arrayExpander

   This function accepts numbers and numels as arguments. It
   creates a copy of the argument array, twice the size of
   the original. The elements of the copy are initialized to
   0, then the argument array's content is copied into the
   copy. A pointer is returned from the function pointing to
   the copy of the argument array.
   ********************************************************** */

int *expandArray(const int *numbers, int &expSize, const int numels)
{
   int *expanded = nullptr;

   int count = 0;

   expSize = numels * 2;
  
   /* Allocates memory for an array to hold twice the number
      of elements as the original */
   expanded = new int[expSize]();

   while (count < numels)
   {
      *(expanded + count) = *(numbers + count);
     
      ++count;
   }
  
   /* Returns a pointer to the copy of the argument array */
   return expanded;
}

/* **********************************************************
   Definition: freeMem

   This function accepts numbers expander as arguments. It
   frees the memory before the program exits.
   ********************************************************** */

void freeMem(int *numbers, int *arrExpander)
{
   delete[] numbers;
   delete[] arrExpander;

   numbers = nullptr;
   arrExpander = nullptr;
}

Example Output:



No comments:

Post a Comment