Dynamically resizing an array

I have an assignment involving functions and pointers.
It's better to just paste what I am supposed to do:

a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, the second parameter is the size of this array, and the third parameter is the size of the larger array to be created by this function. Make sure that you allocate memory from the heap inside this function. After allocating memory for the second array the function must copy the elements from the first array into the larger array. Finally, the function must return a pointer to the new array.
b. In main, allocate an array on the heap that is just large enough to store the integers 5, 7, 3, and 1.
c. Resize the array to store 10 integers by calling the resize function created in step a. Remove the old (smaller) array from the heap. Add the numbers 4, 2, and 8 to the end of the new array.
d. Write a sort function that sorts any integer array in increasing order.
e. Use the sort function to sort the array of numbers in c above. Display the sorted numbers.

So, below is what I have so far. First of all, I'm not sure if I am doing it correctly. It probably looks messy. I don't have a lot of experience, so I am not trying to do anything advanced. It took me a while to comprehend stack and heap. I admit, I am a slow learner, and I've spent about two days on this. But the problem I have is with part c. Specifically, adding the numbers 4, 2, and 8 at the end of the new array.

It says to resize the array to store 10 integers. I did that in the function reSIZE. Then it says to remove the old array, which I call "original", from the heap. I think I did that correctly when I typed "delete[] original;". Then it says to add the numbers 4, 2, and 8 at the end of the new array. I'm just wondering if I need to add another array to do this? Or am I supposed to be using the one I have in main? Keep in mind that there is not supposed to be any input. I'm just lost when it comes to adding those numbers. I will probably figure it out eventually, but this part is bothering me.

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
65
66
67
68
69
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;

void reSIZE(int *&original, int &SIZE, const int &maxSIZE); //function prototype resize
void sortFUNC(int *&original, int &SIZE, const int &maxSIZE); //function prototype sortFUNC

int main()
{
	int SIZE = 4; //size of current array
	int maxSIZE = 10; //size of final array
	int *original = new int[SIZE] {5, 7, 3, 1}; //old(current) array

	cout << "Elements in array: "; //test output
	reSIZE(original, SIZE, maxSIZE); //call function resize
	cout << endl << endl; //blank line
	cout << "Elements in array in increasing order: "; //test output
	sortFUNC(original, SIZE, maxSIZE); //call function sortFUNC
	cout << endl << endl;
	return 0;
}

void reSIZE(int *&original, int &SIZE, const int &maxSIZE)//function definition
{
	int *temporiginal = new int[SIZE + 3]; //(final)new array

	for (int i = 0; i < SIZE; i++) //copy old array to new array
	{
		temporiginal[i] = original[i];
		cout << original[i] << setw(3);
	}

	delete[] original; //delete old array
	original = temporiginal; //point old array to new array
}

void sortFUNC(int *&original, int &SIZE, const int &maxSIZE)
{
	for (int i = 0; i < SIZE; i++)
	{
		int smallest = original[i];
		int smallestINDEX = i;

		for (int m = i; m < SIZE; m++)
		{
			if (original[m] < smallest)
			{
				smallest = original[m];
				smallestINDEX = m;
			}
		}

		swap(original[i], original[smallestINDEX]);
	}

	int *temporiginal = new int[SIZE + 3];

	for (int i = 0; i < SIZE; i++)
	{
		temporiginal[i] = original[i];
		cout << original[i] << setw(3);
	}

	delete[] original;
	original = temporiginal; 
}
Last edited on
a. ... Finally, the function must return a pointer to the new array.

That is not what your function does.


Before resize you do have
[ 5, 7, 3, 1 ]

after resize:
[ 5, 7, 3, 1, X, X, X, X, X, X ]

where X are uninitialized elements.

adding the numbers 4, 2, and 8 at the end of the new array.

Intuitively, that sounds line appending to the logical end of the array:
[ 5, 7, 3, 1, 4, 2, 8, X, X, X ]

I believe in the case of int, the initialization may be unavoidable... I did a test, and I got:
Nums:
5 7 3 1 

Resizing Nums to 10:
5 7 3 1 0 0 0 0 0 0 

Appending a few numbers:
5 7 3 1 4 2 8 0 0 0 

After sorting:
0 0 0 1 2 3 4 5 7 8 


Edit: for the resize function, you want a method signature that returns a pointer and not changes the input array in place. So something like:
1
2
// Destroys old array and returns new, resized one
int* Resize(int* old, int old_size, int new_size)
Last edited on
Let me rephrase:

http://en.cppreference.com/w/cpp/language/new
The object created by a new-expression is initialized according to the following rules:
* If type is an array type, an array of objects is initialized.
-- If initializer is absent, each element is default-initialized

http://en.cppreference.com/w/cpp/language/default_initialization
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values.

=>
They are "initialized" after all -- default-initialized -- with indeterminate values.


If you have 7 real values, then it would be logical to sort 7 values, not 10.
If you want to be more precise, add some const-ness.

To show that the function is not messing with where the pointer is pointing, make the pointer const:
1
2
3
4
5
6
7
int* Resize(int* const old, int old_size, int new_size)
{
    int k = 5;
    old = &k;  // error: assignment of read-only parameter 'old'

    // ...
}


To show that the function won't try to change the data itself, make the integer array const:
1
2
3
4
5
6
int* Resize(const int* old, int old_size, int new_size)
{
    old[2] = 99;  // error: assignment of read-only location

    // ...
}


And so together we have
int* Resize(const int* const old, int old_size, int new_size)
Thanks for the feedback.
@keskiverto -- Guess I got lucky in the online compiler implementation?

1
2
3
4
5
// Technically indeterminate, as per docs, but I experienced all 0's online (gcc).
int* arr = new int[new_size];  

// Value-initialization; guarantees all 0's
int* another = new int[new_size]();
Topic archived. No new replies allowed.