I have to take a 5 element array and use a function with pointers to make a new array that is one element bigger and shifted 1 place. I have it working so that it shifts one place but the function isn't making the array one element bigger. Any sugestions?

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int *eShift(const int *, int);                        //prototype of function that duplicates an array and shifts it 1 place
void newArray (const int[], int);                     //prototype of function that displays the array and the new array


int main(int argc, char *argv[])
{
    const int SIZE = 5;
    int array[SIZE] = {1,2,3,4,5};

    int *dup1;                                        //pointer for new array

    dup1 = eShift(array, SIZE);                       //make the new array and shifts it 1 place.

    cout << "Here is the 5 element array:\n";
    cout << endl;
    newArray(array, SIZE);                            //call to display array function
    cout << "Here is the 5 element array shifted left 1 place:\n";
    cout << endl;
    newArray(dup1, SIZE);                             //call to display new shifted array function

    delete [] dup1;                                   //free up dynamically allocated memory
    dup1 = 0; 

    system("PAUSE");
    return EXIT_SUCCESS;
}

int *eShift(const int *array, int size)               //function to make the new array and shift it 1 place
{
    int *array2;

    if (size < 0)
       return NULL;

    array2 = new int[size + 1];
    array2[0] = 0;

    for(int i = 0; i < size; i++)

    array2[i + 1] = array[i];


    return array2;
}
void newArray (const int array[], int size)           //function to display both arrays
{
     for (int i = 0; i < size; i++)

     cout << left << setw(4) << array[i];
     cout << endl;
     cout << endl;

}

Recommended Answers

All 2 Replies

What makes you think the second array isn't one bigger? Maybe if you displayed all six elements in the second array, instead of just five, you'd be able to tell if it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.