#include#include int main() { int a[] = {1, 2, 3, 4, 5}; int n = 5; // Resizing to a larger size n = 8; int* temp = new int[n]; for (int i = 0; i < 5; i++) { temp[i] = a[i]; } delete[] a; a = temp; // Print the resized array for (int i = 0; i < n; i++) { std::cout << a[i] << " "; } std::cout << '\n'; return 0; }
#includeIn this example, we have an array `a` of size `8` and we want to resize it to `5`. We first allocate a new memory block of size `5` using the `new` operator. We then copy the first `5` elements of the old array `a` to the new memory block using a loop. Finally, we delete the old memory block using the `delete[]` operator and assign the new memory block to `a`. Package library: No specific package library is required for this. It's a standard feature of the C++ language.#include int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; int n = 8; // Resizing to a smaller size n = 5; int* temp = new int[n]; for (int i = 0; i < n; i++) { temp[i] = a[i]; } delete[] a; a = temp; // Print the resized array for (int i = 0; i < n; i++) { std::cout << a[i] << " "; } std::cout << '\n'; return 0; }