Sunday, April 2, 2017

Programming Challenge 9.12 - Element Shifter

/* Element Shifter - 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 one element larger than the argument array. The
   first element of the new array is set to 0. Element 0 of the argument
   array is copied to element 1 of the new array, element 1 of the
   argument array is copied to element 2 of the new array, and so forth.
   The function returns a pointer to the new array. */

#include "Utility.h"

/* Function prototypes */
void getNumbers(int *, int);
void displayNumbers(const int *, const int);
int *shiftElements(const int *, int &, const int);
void displayShifted(const int *, const int);
void freeMem(int *, int *);

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

   int numels = 0,
       elemSize = 0;

   /* Ask for the number of elements the array should to hold */
   cout << "\n\tELEMENT SHIFTER\n\n"
        << "\tHow many numbers should your set contain? ";
   cin >> numels;

   /* Input Validation */
   while (numels <= 0)
   {
      cout << "\n\tInvalid Input!\n"
           << "\tHow many numbers should your set 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 numbers in their original order */
   displayNumbers(numbers, numels);

   cout << "\n\n\tNow shifting the elements by one ...\n\n";

   /* Shifts the elements in the argument array */
   shifted = shiftElements(numbers, elemSize, numels);

   /* Displays the new array with elements shifted by one
      position */
   displayShifted(shifted, elemSize);

   /* Frees the memory */
   freeMem(numbers, shifted);

   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: displayNumbers

   This function accepts numbers and numels as arguments. It
   displays the numbers in their original order.
   ********************************************************** */

void displayNumbers(const int *numbers, const int numels)
{
   cout << "\n\tThese are your numbers in their original order:\n\n";

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

/* **********************************************************
   Definition: shiftElements

   This function accepts numbers, elemSize, and numels as
   arguments. It creates a new array, one element larger than
   numbers, the first element being initialized to 0. The
   elements are shifted by one position, each time an element
   from the argument array is copied to the new array. When
   finished, a pointer to the new array is returned.
   ********************************************************** */

int *shiftElements(const int *numbers, int &elemSize, const int numels)
{
   int *elemShifter = nullptr;

   int firstElem = 0,
       nextElem = 0;

   elemSize = numels + 1;

   /* Allocates a new array, one element larger than the argument
      array, to hold the shifted elements */
   elemShifter = new int[elemSize]();

   while (firstElem < numels)
   {
      ++nextElem;

      *(elemShifter + nextElem) = *(numbers + firstElem);

      ++firstElem;
   }

   return elemShifter;
}

/* **********************************************************
   Definition: displayShifted

   This function accepts shifted and elemSize as arguments.
   It displays the numbers in the new array, after having
   been shifted by 1 position.
   ********************************************************** */

void displayShifted(const int *shifted, const int elemSize)
{
   cout << "\n\tThese are your numbers after being shifted "
        << "by 1 position:\n\n";

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

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

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

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

   numbers = nullptr;
   shifted = nullptr;
}

Example Output:




No comments:

Post a Comment