/* Reverse Array - This program contains a function that accepts an int
array and the array's size as arguments. The function creates a copy
of the array, except that the element values are reversed in the copy.
The function returns a pointer to the new array. */
#include "Utility.h"
/* Function prototypes */
void getNumbers(int *, int);
int *reverseNumbers(const int *, const int);
void displayNumbers(const int *, const int);
void displayReverse(const int *, const int);
void freeMem(int *, int *);
int main()
{
int *numbers = nullptr;
int *revNumbers = nullptr;
int numels = 0;
/* Get the initial number of elements the array should contain */
cout << "\tREVERSE NUMBERS\n\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);
/* Creates a copy of numbers and fills the copy with the numbers
in reverse order */
revNumbers = reverseNumbers(numbers, numels);
/* Displays the numbers in reverse order */
displayReverse(revNumbers, numels);
/* Frees the memory */
freeMem(numbers, revNumbers);
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: reverseNumbers
This function accepts numbers and numels as arguments. It
creates a copy of the array, and reverses the numbers. A
pointer to the copy containing the numbers in reverse
order is returned.
********************************************************** */
int *reverseNumbers(const int *numbers, const int numels)
{
int *revNumbers = nullptr;
int firstElem = 0,
lastElem = numels;
/* Allocates a new array to hold the copy of numbers in
reverse order */
revNumbers = new int[numels]();
while (lastElem > 0)
{
--lastElem;
*(revNumbers + firstElem) = *(numbers + lastElem);
++firstElem;
}
/* Returns a pointer to the new array */
return revNumbers;
}
/* **********************************************************
Definition: displayReverse
This function accepts revNumbers and numels as arguments. It
displays the numbers in reverse order.
********************************************************** */
void displayReverse(const int *revNumbers, const int numels)
{
cout << "\n\tThese are your numbers in reverse order:\n\n";
for (int index = 0; index < numels; index++)
{
cout << "\t" << *(revNumbers + index) << " \n";
}
}
/* **********************************************************
Definition: freeMem
This function accepts numbers and numels as arguments. It
frees the memory before the program exits.
********************************************************** */
void freeMem(int *numbers, int *revNumbers)
{
delete[] numbers;
delete[] revNumbers;
numbers = nullptr;
revNumbers = nullptr;
}
Sunday, April 2, 2017
Programming Challenge 9.10 - Reverse Array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment