#include <iostream>
#include <array>
using namespace std;
// Reference
// https:stackoverflow.com/questions/35079109/why-cant-i-reuse-the-name-of-a-dynamically-allocated-array-after-i-delete-it-in
// https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/
// https://www.youtube.com/watch?v=FHhcSncuHEI
int main()
{
int size;
int number = 0;
int count = 0;
cout << "enter size for array: ";
cin >> size;
int *array;
int *tempArray;
tempArray = new int[size];
array = new int[size];
// can also be declared
// int *array = new int[size];
while(number != -1)
{
cout << " enter values for array: ";
cin >> number;
if(number == -1)
{
break;
}
if(count == size)
{
delete [] tempArray;
size = size + 1;
tempArray = new int[size];
for(int i=0; i < count; i++)
{
tempArray[i] = array[i];
}
delete [] array;
array = new int[size]; // re-use array pointer
for(int i=0; i < count; i++)
{
array[i] = tempArray[i];
}
}
array[count] = number;
count++;
}
// if(number == -1)
// {
// for(int i=0; i < count; i++)
// {
// cout << &array[i];
// }
// }
cout << endl;
// proves the array is tracking in memory to the same places.
// if it goes out of bounds of memory, the cout will just
// print the memory address of the last memory address block
// of the dynamic array, the assignment out of bounds does not
// cause significant change.
// array[0] = 1;
// array[1] = 2;
// array[2] = 3;
// array[3] = 4;
// cout << &array[0];
// cout << &array[1];
// cout << &array[2];
// cout << &array[3];
// cout << sizeof(array)/sizeof(array[0]);
// cout << endl;
// cout << array[0];
// cout << array[1];
// cout << array[2];
// cout << array[3];
// cout << array[4];
// cout << array[5];
// cout << endl;
for(int i=0; i < size; i++)
{
cout << "array element " << i << ": " << array[i] << endl;
}
return 0;
}