//Ryan Shahriyarpour CS1A Chapter 9 Question 12
/*****************************************************************************
*
* Shift the Elements of an Array
*
*********************
*
* This program makes a new array that is one element larger than the
* original. The new array starts with a 0 and is followed by every element
* of the original array, each moved over one position. It displays the
* original array and the shifted array.
*
* INPUT
* None (the array is set in the program)
*
* OUTPUT
* The original array and the shifted array
*
****************************************************************************/
#include <iostream>
using namespace std;
const int SIZE = 5; // Number of elements in the array
// Function prototypes
int *shiftElements(const int *array, int size);
void showArray(const int *array, int size);
int main()
{
// data dictionary
int numbers[SIZE] = {3, 6, 9, 12, 15}; // The original array
int *shifted; // Pointer to the shifted array
shifted = shiftElements(numbers, SIZE);
cout << "Original array: ";
showArray(numbers, SIZE);
cout << "Shifted array: ";
showArray(shifted, SIZE + 1);
// Free the memory
delete [] shifted;
shifted = nullptr;
return 0;
}
// Returns a pointer to a new array one element larger, with a 0 in the
// first position and the original elements shifted over by one
int *shiftElements(const int *array, int size)
{
int *newArray = new int[size + 1]; // Pointer to the new array
int count; // Loop counter
*newArray = 0;
for (count = 0; count < size; count++)
*(newArray + count + 1) = *(array + count);
return newArray;
}
// Displays the elements of an array on one line
void showArray(const int *array, int size)
{
int count; // Loop counter
for (count = 0; count < size; count++)
cout << *(array + count) << " ";
cout << endl;
}