Module: Dynamic arrays


Problem

1/1

Dynamic Array: Start

Theory Click to read/hide

Dynamic arrays
In previous courses on arrays, we looked at creating an array like this:
 
const int Nmax = 1000; int A[Nmax]; or this: intA[1000];
This method creates a static array. One disadvantage is that the size and type of array elements cannot be changed while the program is running. 
In the example above, a static array of 1000 elements is created. That is, an area is allocated in memory to store 1000 elements of type int (an array of 1000 elements of type int). The address of the beginning of the array is stored in the  A variable. If you need to process an array of more than 1000 elements, you will have to change the description and recompile the program again. When working with small arrays, most of the memory allocated for a static array will be wasted.

For efficient use of computer memory, it is necessary to allocate it dynamically. In other words, memory for objects should be allocated during program execution as needed (creating new objects).

In C++, the  new and delete operations are designed to dynamically allocate computer memory.  Operation new  allocates memory from the free memory area, and the operation delete deallocates the allocated memory. 
 
An example of creating and deleting a dynamic variable
int *A = new int; // create an object of type int *A = 15; // object initialization occurs through a pointer (*) // initialization can be done immediately // when declaring a dynamic object int *B = new int(25); cout << *A << " " << *B; // output value by pointer deleteA; // freed allocated memory delete B; The operation new creates an object of the given type, allocates memory for it, and returns a pointer of the correct type to the given memory location. If the memory cannot be allocated, for example, if there are no free areas, then a null pointer is returned, that is, the pointer will return the value 0. Memory allocation is possible for any data type: intfloat , doublechar etc.

In the same way, in C++ you can create a dynamic array that will use dynamic memory allocation. The size of such an array, which can be used while the program is running, for example, from the keyboard. to create dynamic variables.
 
In C syntax
intN; scanf("%d", &N); int *mas = malloc (sizeof(int) * N); // later to resize the array // used by realloc. ... free(mas); // release allocated memory
In C++ syntax
intN; cin>> N; int*A = new int[N]; // to change the size of the array, you can // transfer data to another array, and clear the old one ... delete []mas; // release allocated memory // square brackets indicate // that we are freeing memory from under the array

Problem

You are given a sequence of integers. Write a program that creates an array and writes a sequence to it twice in a row.
 
Input 
First given number N — the number of elements in the sequence (1<= N <= 100). Then N numbers are written separated by a space.
 
Output 
It is necessary to output an array consisting of a duplicated sequence.
 
Examples
# Input Output
1 3
1 2 3
1 2 3 1 2 3