Expanding an Array with Pointers then Shifting Elements?

Hey guys, I am doing a problem out of a c++ book. No, this isn't homework, I am doing this for myself here at home. Here is the program requirements out of the book.

Write a function that accepts an int array and the array’s size as arguments. The
function should create a new array that is one element larger than the argument array.
The first element of the new array should be set to 0. Element 0 of the argument array
should be copied to element 1 of the new array, element 1 of the argument array should
be copied to element 2 of the new array, and so forth. The function should return a
pointer to the new array.

I believe I met the requirements but I am not sure about a few things as I am still sort of new when dealing with pointers.

1) When I run the program, OriginalArray will have a large number for the last element location.

2) Am I correctly using dynamic allocation for the arrays? Am I deleting/freeing the memory correctly as well? Am I returning a pointer to the new array correctly?

3) Please add any additional tips or advice for me if necessary.

Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Element Shifter
#include <iostream>
#include <iomanip>
using namespace std;

int *ArrayElementShift (int *, int);

int main ()
{
	int *OriginalArray = nullptr;
	int elements;
	int *ExpandedArray = nullptr;

	cout << "How many elements?" << endl;
	cin >> elements;

	OriginalArray = new int[elements];
	
	cout << "Please enter integers for the elements." << endl;

	for (int x = 0; x < elements; x++)
	{
		cout << "Number " << x + 1 << ": ";
		cin >> *(OriginalArray + x);
	}

	ExpandedArray = ArrayElementShift(OriginalArray, elements);


	for (int x = 0; x < elements + 1; x++)
	{
		cout << "\nElement " << x << ": " << endl;
		cout << "OriginalArray: " << *(OriginalArray + x) << endl;
		cout << "ExpandedArray: " << *(ExpandedArray + x) << endl;
	}

	delete [] OriginalArray;
	delete [] ExpandedArray;
	OriginalArray = nullptr;
	ExpandedArray = nullptr;

	system("pause");
	return 0;
}

int *ArrayElementShift (int *Arr, int elem)
{
	int NewElem = elem + 1;
	int *NewArray;
	NewArray = new int[NewElem];
	
	*NewArray = 0;

	for (int x = 0; x < NewElem; x++)
	{
		*(NewArray + x + 1) = *(Arr + x);
	}

	return NewArray;

	delete [] NewArray;
	NewArray = nullptr;

}
Last edited on
1. The last element of OriginalArray is at index elements - 1, but the loop from line 30 is allowed to run when x == elements. OriginalArray[elements] is not a valid access. You can solve it by putting an if around line 33 that will not execute that code if x >= elements.
2. The code as it is is correct, but note that lines 61 and 62 will never run, because a return statement causes control to go back to the caller. In any case, you shouldn't be delete the array that you return, because then the caller would get back from you a dangling pointer.
Topic archived. No new replies allowed.