//Dione Torrevillas CS1A Chapter 9, P. 539, #12
/*
********************************************************************
ELEMENT SHIFTER
Description: Return a new array one element larger than the original.
Put 0 first and shift all original elements one place.
INPUT: Array size and the original integer values.
OUTPUT: The shifted array, beginning with 0.
********************************************************************
*/
#include <iostream>
using namespace std;
// Function prototype
int *shiftArray(const int *, int);
int main()
{
int size;
cout << "How many integers? ";
cin >> size;
while (size <= 0)
{
cout << "Enter a number greater than 0: ";
cin >> size;
}
int *numbers = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> *(numbers + i);
}
int *shifted = shiftArray(numbers, size);
cout << "Shifted array: ";
for (int i = 0; i < size + 1; i++)
cout << *(shifted + i) << " ";
cout << endl;
// Release both arrays when finished.
delete [] numbers;
delete [] shifted;
numbers = 0;
shifted = 0;
return 0;
}
// Add a zero to the beginning of a copy of the array.
int *shiftArray(const int *original, int size)
{
int *shifted = new int[size + 1];
*shifted = 0;
for (int i = 0; i < size; i++)
*(shifted + i + 1) = *(original + i);
return shifted;
}