Using Dynamic memory allocation demonstrate how to resize an array of integers in c++
To resize an array using dynamic memory allocation in C++, you need to follow these steps:
1. Declare a pointer variable of the desired data type (in this case, an integer):
```cpp
int* array;
```
2. Allocate memory for the initial array size using the `new` keyword:
```cpp
int initialSize = 10; // initial size of the array
array = new int[initialSize];
```
3. Initialize the array elements as needed.
4. Determine the new size of the array:
```cpp
int newSize = 20; // new desired size of the array
```
5. Create a new array with the desired size using the `new` keyword:
```cpp
int* newArray = new int[newSize];
```
6. Copy the elements from the old array to the new array using a loop:
```cpp
for (int i = 0; i < initialSize; i++) {
newArray[i] = array[i];
}
```
7. Delete the old array to free up the memory:
```cpp
delete[] array;
```
8. Assign the new array to the original pointer variable:
```cpp
array = newArray;
```
By following these steps, you have successfully resized the array using dynamic memory allocation.