Recording Session


Dynamic Array is an normal array it's size being modified during the runtime.

The new Keyword

In C++, we can create a dynamic array using the new keyword. The number of items to be allocated is specified within a pair of square brackets. The type name should precede this. The requested number of items will be allocated.

Syntax:

The new keyword takes the following syntax:

pointer_variable = new data_type;
#include<iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of items:" << "\\n";
    cin >> n;

    int *arr = new int[n];
    cout << "Enter " << n << " items" << endl;

    for (int i = 0; i < n; i++)
        cin >> arr[i];

    cout << "You entered: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
}

Output

Enter the number of items:
***7***
Enter 7 items
***27 9 8 12 98 52 34***
You entered: 27 9 8 12 98 52 34