// Saliha Babar CS1A Chapter 9, Page 539, #10
//
/*******************************************************************************
* REVERSE ARRAY
* _____________________________________________________________________________
* This program fills an array with random integers and then creates a reversed
* copy of the array.
*
* ____________________________________________________________________________
* INPUT
* SIZE : size of an array
* maxRange : maximum range of random numbers
* OUTPUT
* pointer : variable that points towards the start of
* reversed array
* ****************************************************************************/
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int *ReverseArray (int array[],int SIZE);
int main() {
unsigned seed = time (0); // to generate random numbers
int randomNumber; // integer random number
int maxRange = 100; // INPUT - maximum range of random numbers
const int SIZE = 9; // INPUT - size of an array
int originalArray[SIZE]; // holds random numbers
int *pointer; // OUTPUT - Pointer for reversed copy
//Seed the random number generator
srand(seed);
// Fill the array with random numbers
for (int i =0 ;i < SIZE ; i ++)
{
randomNumber = 1 + rand() % maxRange;
originalArray[i] = randomNumber;
}
// Pass the original to make a copy as a reverse
pointer = ReverseArray (originalArray,SIZE);
cout << "This is the original array of ints \n";
for (int i = 0 ; i < SIZE ; i++)
{
cout << *(originalArray + i) << " ";
}
cout << "\nThis is copied and reversed array of ints \n";
for (int i = 0 ; i < SIZE ; i++)
{
cout << *(pointer + i) << " ";
}
delete [] pointer;
return 0;
}
int *ReverseArray (int array[],int SIZE)
{
// Allocate new memory for the reversed version..
int *reversePointer;
reversePointer = new int [SIZE];
// Modify the content of reverse pointer with reverse ints..
for (int i = 0; i < SIZE ; i ++)
{
*(reversePointer + SIZE - 1 - i) = *(array + i);
}
return reversePointer;
}