how to dynamically allocate an array c++

#include <iostream>

int main() {
    // Step 1: Declare a pointer variable to hold the address of the dynamically allocated array
    int *dynamicArray;

    // Step 2: Determine the size of the array
    int size;
    std::cout << "Enter the size of the array: ";
    std::cin >> size;

    // Step 3: Allocate memory for the array using the 'new' operator
    dynamicArray = new int[size];

    // Step 4: Check if memory allocation was successful
    if (dynamicArray == nullptr) {
        std::cout << "Memory allocation failed." << std::endl;
        return 1;
    }

    // Step 5: Initialize the elements of the array (optional)
    for (int i = 0; i < size; ++i) {
        dynamicArray[i] = i * 2; // Example initialization, modify as needed
    }

    // Step 6: Use the dynamically allocated array as needed

    // Step 7: Deallocate the dynamically allocated memory using the 'delete' operator
    delete[] dynamicArray;

    return 0;
}