Hi! So we are trying to learn about pointers and dynamic memory allocation, but the problem is that we get "garbage values" because when we try to allocate a smaller array that is half the size of the original array, we can no longer print out 10 elements in it since it can only store 5 for example (Assuming input was 10).

The rest of the values indexes do not exist right? So that is why we get "garbage values". We can have misinterpreted the question they asked but we thought that they wanted us to print all 10 elements that the array was filled up with from the beginning, or do they want us to just print 5 elements since we have reduced the array?

Assignment instruction:
"In this exercise, create a small program which starts by asking theuser for a positive integer larger than zero. Dynamically allocate aninteger array with a size that corresponds to the inputted integer, andfill it with numbers from 0- (input - 1). Now create a loop that printsthe integers in backwards order. Once a point is reached in the loopat which half of the current array has been printed, allocate a newarray that can fit the remaining integers, copy the non-printed integersover from the old larger array into the new smaller array, delete theold array, and set the pointer to point at the new array. Continue thisprocess until all values have been printed. Do not forget to delete thearray when you are done with it."

Code:
Code:
void DisplayArray(std::string msg, int arr[], const int size) {
    std::cout << msg << std::endl;
    for (int i = size - 1; i >= 0; i--) {
        std::cout << arr[i] << std::endl;
    }
}


int main()
{
    int *currentArr;
    int input;
    std::cout << "Enter the size (larger than 0) of the array: \n";
    std::cin >> input;
    getchar();
    currentArr = new int[input];
    for (int i = 0; i < input; i++)
    {
        currentArr[i] = i;
        std::cout << currentArr[i] << '\n';
    }


    for (int i = (input-1); i > 0; i--)
    {
        if (i >= input / 2)
        {
            std::cout << "\nInside first if: ";
            std::cout << currentArr[i] << '\n';
        }
        else if (i < input / 2)
        {
            // This will shrink until 1 place left if using "i" to allocate the array
            const int halfSize = input / 2;
            int *halfArr = new int[halfSize];


            // Remember that we put all values in half array of original array
            for (int j = 0; j < halfSize; j++)
            {
                halfArr[j] = currentArr[j]; 
            }
            std::cout << "\nElse if before del: " << halfArr[i];
            
            delete[] currentArr;
            currentArr = nullptr;


            currentArr = halfArr;
            std::cout << "\nElse if: " << currentArr[i];
        }
    }


    DisplayArray("\n\nStored array", currentArr, input);


    getchar();   
}